-
Notifications
You must be signed in to change notification settings - Fork 269
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
freeze_time
could accept a timezone argument
#87
Comments
Not sure if you know this, but If your suggestion was just to make it cleaner so that we could pass in 'nice' timezone names, then I could get behind that. |
Yes, I've seen |
Ah, right. I had forgotten about your second point. PR welcome. |
You don't need to put |
Just FYI: one can already pass
(prints |
AFAIK you can also pass a timezone in the string, right? with freeze_time("2015-03-09 09:00:00 PST") |
Regarding my previous comment, it turns out that dateutil's parser returns an aware datetime IFF the timezone in the string matches the default timezone of the host. In all other cases, dateutil's parser ignores the given timezone and returns a naive (and unadjusted) datetime.
Definitely fails the principle of least surprise for me, but there you have it. |
Going to close this with support for @shreevatsar 's solution |
@spulec That's a shame. While it works, it's a bit clumsy for an extremely common use case. (I, at least, have zero Python projects that can afford to ignore time zones.) |
Happy to accept a PR for it, but don't want to leave the issue open forever. |
@shreevatsar solution does not work for me. I might be doing something wrong, but definitely this is not very straightforward. |
I am also not getting the described behaviour:
Ignoring the whacky time (this is caused by most timezones not being suitable for use as the |
I got same problem with freezing time in Django tests =/ |
i can confirm problem mentioned by @spumer @fish-face and @jose-lpa from datetime import datetime
import arrow
from freezegun import freeze_time
d = datetime(2015, 8, 18, 8, 51, 50, tzinfo=arrow.now("America/Los_Angeles").tzinfo)
with freeze_time(d):
print datetime.now()
|
@spulec for me @shreevatsar 's solution doesn't work as well |
This worked for me (I already had a helper function for tests) on Django + pytest + freezegun from datetime import datetime
from django.utils.timezone import get_current_timezone
def to_datetime(str_date: str, time: bool = False) -> datetime:
str_format = "%Y-%m-%d"
if time:
str_format += "-%H:%M:%S"
return datetime.strptime(str_date, str_format).astimezone(get_current_timezone()) On the tests: with freeze_time(to_datetime("2020-01-12")):
... |
Be ware when work with aware datetimes: #348 |
Is there any technical reason why you're |
@spumer @realmhamdy thanks for pointing to that issue - very helpful! |
What do you think of having
freeze_time
accept a timezone argument:@freeze_time('2015-03-09 09:00:00', timezone='US/Pacific')
That would require adding pytz to the requirements, which should be fine since most Python projects have it.
Thoughts? I might develop it.
The text was updated successfully, but these errors were encountered: