pytz

Python datetime difference between .localize and tzinfo

混江龙づ霸主 提交于 2020-01-03 15:22:16
问题 Why do these two lines produce different results? >>> import pytz >>> from datetime ipmort datetime >>> local_tz = pytz.timezone("America/Los_Angeles") >>> d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1 >>> d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) # line 2 >>> d1 == d2 False What's the reason for the difference, and which should I use to localize a datetime? 回答1: When you create d2=datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) in this way. It does not handle daylight

Python datetime difference between .localize and tzinfo

泄露秘密 提交于 2020-01-03 15:20:34
问题 Why do these two lines produce different results? >>> import pytz >>> from datetime ipmort datetime >>> local_tz = pytz.timezone("America/Los_Angeles") >>> d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1 >>> d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) # line 2 >>> d1 == d2 False What's the reason for the difference, and which should I use to localize a datetime? 回答1: When you create d2=datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) in this way. It does not handle daylight

Python pytz timezone conversion returns values that differ from timezone offset for different dates

浪子不回头ぞ 提交于 2020-01-03 02:24:12
问题 I was trying to convert US/Eastern timezone dates to UTC to upload to a website that only accepts UTC times, but displays time in local timezone. I have the following code example where when I convert a recent date, there is no issue with the offset (5:00 or 4:00), but when I convert a date such as 1900-01-01, the offset becomes something around 4:56 etc. import pytz import tzlocal from datetime import datetime usest=tzlocal.get_localzone()#My local zone is US/Eastern. I could directly use

Python datetime not including DST when using pytz timezone

杀马特。学长 韩版系。学妹 提交于 2020-01-01 04:17:24
问题 If I convert a UTC datetime to swedish format, summertime is included (CEST). However, while creating a datetime with sweden as the timezone, it gets CET instead of CEST. Why is this? >>> # Modified for readability >>> import pytz >>> import datetime >>> sweden = pytz.timezone('Europe/Stockholm') >>> >>> datetime.datetime(2010, 4, 20, 16, 20, tzinfo=pytz.utc).astimezone(sweden) datetime(2010, 4, 20, 18, 20, tzinfo=<... 'Europe/Stockholm' CEST+2:00:00 DST>) >>> >>> datetime.datetime(2010, 4,

Django IPython sqlite complains about naive datetime

巧了我就是萌 提交于 2019-12-31 12:41:46
问题 I have a new project in Django 1.4, using sqlite db. Also using django_extenstions' shell_plus with no problems. When I installed IPython, both shell and shell_plus started to complain about: /path/to/my/virtualenv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:50: RuntimeWarning: SQLite received a naive datetime (2012-07-29 13:15:45.229464) while time zone support is active. It seems IPython itself uses unaware datetimes. How can this be fixed? EDIT: I don't want to disable

How can I convert windows timezones to timezones pytz understands?

痴心易碎 提交于 2019-12-30 06:15:10
问题 In a windows python environment I can get the local timezone like this, but it's not usable with pytz: >>> import win32timezone >>> win32timezone.TimeZoneInfo.local() TimeZoneInfo(u'US Mountain Standard Time', True) >>> win32timezone.TimeZoneInfo.local().timeZoneName u'US Mountain Standard Time' >>> tz = pytz.timezone(win32timezone.TimeZoneInfo.local().timeZoneName) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\pytz\__init__.py",

From a timezone and a UTC time, get the difference in seconds vs local time at that point in time

試著忘記壹切 提交于 2019-12-29 01:26:06
问题 This should be very simple, but I can't quite figure it out in Python. I want to have a function which takes two arguments, a UTC time in seconds and a zoneinfo name like 'Europe/Vienna' and returns the offset in seconds from local time and UTC for that point in time. In C it would be: /* ... code to to set local time to the time zone I want to compare against, not shown here. Then call function below to get difference vs localtime. Hardly an ideal solution, but just to demonstrate what I

Why does it make a difference if I add 1 hour to the UTC time and localize or 1 hour to the local time?

爱⌒轻易说出口 提交于 2019-12-25 18:47:14
问题 Example Code from datetime import datetime, timezone, timedelta import pytz t11 = datetime(1918, 4, 15, 0, 0, tzinfo=timezone.utc).astimezone(pytz.timezone('Europe/Berlin')) t12 = t11 + timedelta(hours=1) t2 = datetime(1918, 4, 15, 1, 0, tzinfo=timezone.utc).astimezone(pytz.timezone('Europe/Berlin')) print(t12) print(t2) Observed 1918-04-15 02:00:00+01:00 1918-04-15 03:00:00+02:00 Expected I expected both to be what I see for t2 . The crucial difference is t2.hour vs t12.hour . For a timezone

Strange pytz import issue — pytz.UnknownTimeZoneError with valid timezone

♀尐吖头ヾ 提交于 2019-12-25 02:02:02
问题 I am having trouble importing a valid timezone on a pytz install. When I run this: python -c "import pytz; print pytz.timezone('America/New_York')" In my home folder I have this output: $ python -c "import pytz; print pytz.timezone('America/New_York')" America/New_York But if I cd into a different folder off of my home directory, I get this output: $ cd webapp/ $ python -c "import pytz; print pytz.timezone('America/New_York')" Traceback (most recent call last): File "<string>", line 1, in

python datetime.astimezone behavior incorrect?

爷,独闯天下 提交于 2019-12-23 16:32:08
问题 Here is the code which first parses time from string in IST and then converts that to UTC. So when it 4:00 pm in India the time in GMT / UTC is 10:30 am. While the following code prints it as 9:30 pm. So instead of subtracting the offset it is adding the offset. From the python documentation https://docs.python.org/2/library/datetime.html#datetime.datetime.astimezone the sample implementation of astimezone, it does appear that it would add the offset if it is negative but it seems contrary to