-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
tzinfo isn't considered #17
Comments
Some 95% of Crontab's purpose was to be the cron scheduling library for my other project, Rpqueue. Rpqueue doesn't do timezones, so I didn't build Crontab to do timezones either. Not to say I can't (turns out it's really easy to add), I'm just explaining why it is the way it is. That said, I get the same result as you when I run your code, but your code is partly wrong:
Compare that to the below from the pytz docs:
With the "correct" datetime, I get 9 hours instead of 4:
That's not terribly helpful, because the actual timing should be 8 hours due to the DST change, but it turns out that the fix is easy:
That function will get you going for now, and I'll add a method that does the same soon-ish. |
... the reason I say "soon-ish" is because the current library uses |
Yeah, my result is different from yours because I'm assuming the CronTab schedule is in localtime. However, I don't and can't use tzinfo (for various reasons), so next_with_timezone won't work for folks that use alternative tzinfo implmentations (localize isn't defined by datetime.tzinfo). However, I do the following instead:
The caveat of this approach is that it is not sensitive to any future (but highly unlikely) gmtoffset changes and the future is computed twice here (in CronTab.next and in next_normalized). You might prefer next_normalized as it avoids any dependency on pytz (or maybe not, as I assume most people aren't constrained to not using pytz unlike me 😄 ) |
I should have read your comment before pushing 0.21.0 with support for pytz. :p In any case, this should be a lot cleaner:
That is using 0.21.0 semantics. |
Actually, I suppose I need to release a 0.21.1 to remove the pytz requirement, as it is only required for testing. |
Okay, pushed to remove the pytz requirement. Can you point me to whatever timezone package you are using? |
I'm using an in-house timezone package that adheres to this tzinfo description. |
Okay, 0.21.2 offers support for tz libs that only offer Let me know if you have issues with this one. |
Works fine. Thank you. |
I read the comments made in #10 about handling timezones and DST transitions. The solutions given is is inadequate, because parse-crontab, after calculating the future datetime, computes the timedelta between the future and the passed in now (tz-aware) datetime.
To see why consider the following example:
The output is 4 hours, instead of 5 hours, because next() does the following sub operation (line 390 of master)
That operation does not return the expected delay because, according to the python docs:
Which makes sense, because, I think, the intent was to have datetime arithmetic operations and timedelta objects have the Group property (i.e. behave similar to regular integers). If tzinfo attributes were considered when calling sub, then datetime2 + t would not equal datetime1.
But I don't think parse-crontab should behave similarly, because the result of a next() isn't a timedelta, but the number of seconds between the datetime passed in and the schedule.
Right now, i can get around this by adding the number of seconds returned to the passed in dt argument of next(), normalize the resulting future datetime and then compute the difference, again, to get the true number of seconds till the schedule. But I think the onus of applying the tzinfo to datetime for computations should be on parse-crontab and not the client.
The text was updated successfully, but these errors were encountered: