Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.0 - PREFER_DATES_FROM & Timezone bug #1239

Open
Keiron-Beadle opened this issue Oct 8, 2024 · 1 comment
Open

1.2.0 - PREFER_DATES_FROM & Timezone bug #1239

Keiron-Beadle opened this issue Oct 8, 2024 · 1 comment

Comments

@Keiron-Beadle
Copy link

Keiron-Beadle commented Oct 8, 2024

It's currently 08/10/2024 23:37 pm Europe/London time. Observe the following code,

time_zone = 'Europe/London'
tz_result = dateparser.parse('at 12am', settings={'PREFER_DATES_FROM' : 'future', 'TIMEZONE': time_zone})
normal_result = dateparser.parse('at 12am', settings={'PREFER_DATES_FROM': 'future'})
print(tz_result)
print(normal_result)

I'd be expecting both results to print, 2024-10-09 00:00:00. The actual prints are,

2024-10-08 00:00:00
2024-10-09 00:00:00

this may be intended behaviour but it doesn't seem like it from my perspective.

Edit: Possibly this is user error, as using 'TO_TIMEZONE' : time_zone instead of TIMEZONE : time_zone seems to return the correct 12am for the tz_result variable. Although I'm a still little confused on why "TIMEZONE" was returning me the previous day's 12am since I've specified PREFER_DATES_FROM_FUTURE. If anyone knows why I'd love to hear it, thanks

@gutsytechster
Copy link
Collaborator

gutsytechster commented Oct 26, 2024

Hi @Keiron-Beadle

I was trying to reproduce the behavior observed by you, i.e. different output with the parameter provided & not provided, however, I couldn't do that.

At the same time, I think I discovered one of the possible bug. When we enter the value at 12 am , it is parsed with RELATIVE_TIME to be current time in UTC, while I think it should be RELATIVE TIME in the given TIMEZONE or the local timezone.

Example

>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2024, 10, 27, 1, 19, 34, 921587)
>>> dateparser.parse('at 12 am')
datetime.datetime(2024, 10, 26, 0, 0)
>>> dateparser.parse('at 12 am', settings={'PREFER_DATES_FROM': 'future'})
datetime.datetime(2024, 10, 27, 0, 0)
>>> dateparser.parse('at 12 am', settings={'PREFER_DATES_FROM': 'future', 'TIMEZONE': 'Asia/Kolkata'})
datetime.datetime(2024, 10, 27, 0, 0)
>>> dateparser.parse('at 12 am', settings={'PREFER_DATES_FROM': 'future', 'TO_TIMEZONE': 'Asia/Kolkata'})
datetime.datetime(2024, 10, 27, 0, 0)

Here, the current time is 27/10/2024 1:19 AM Asia/Kolkata. The default parsing use the current time in UTC and hence date is picked as 26/10/2024.

If I apply the PREFER_DATES_FROM to future , the date is moved one day ahead, which is correct w.r.t the UTC timezone, but incorrect if I address my local timezone. It also doesn't respect the timezone provided in the settings and only apply it after parsing.

A solution to this would be to provide explicity RELATIVE_BASE to the settings with the datetime.now() value that would pick your local timezone value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants