Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24917 > unrolled thread
| Started by | Damjan <gdamjan@gmail.com> |
|---|---|
| First post | 2012-07-05 16:10 +0200 |
| Last post | 2012-07-05 16:10 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
Confusing datetime.datetime Damjan <gdamjan@gmail.com> - 2012-07-05 16:10 +0200
| From | Damjan <gdamjan@gmail.com> |
|---|---|
| Date | 2012-07-05 16:10 +0200 |
| Subject | Confusing datetime.datetime |
| Message-ID | <mailman.1825.1341497710.4697.python-list@python.org> |
I've been struggling with an app that uses
Postgresql/Psycopg2/SQLAlchemy and I've come to this confusing
behaviour of datetime.datetime.
First of all, the "Seconds since Epoch" timestamps are always in UTC, so
shouldn't change with timezones. So I'd expect that a round trip of a
timestamp through datetime.datetime, shouldn't change it.
Now, all is good when I use a naive datetime.datetime
-- TZ=UTC python
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1341446400)
>>> dt
datetime.datetime(2012, 7, 5, 0, 0)
>>> dt.strftime('%s')
'1341446400'
-- TZ=Asia/Tokyo python
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1341446400)
>>> dt
datetime.datetime(2012, 7, 5, 9, 0)
>>> dt.strftime('%s')
'1341446400'
But when I use an timezone aware datetime.datetime objects, the
timestamp roundtrip is destroyed. I get 2 different timestamps.
Am I missing something here, I've been reading the datetime
documentation several times, but I can't understand what is the intended
behaviour.
-- TZ=UTC python
>>> from datetime import datetime
>>> import pytz
>>> tz = pytz.timezone('Europe/Skopje')
>>> dt = datetime.fromtimestamp(1341446400, tz)
>>> dt
datetime.datetime(2012, 7, 5, 2, 0, tzinfo=<DstTzInfo 'Europe/Skopje'
CEST+2:00:00 DST>)
>>> dt.strftime('%s')
'1341453600'
-- TZ=Asia/Tokyo python
>>> from datetime import datetime
>>> import pytz
>>> tz = pytz.timezone('Europe/Skopje')
>>> dt = datetime.fromtimestamp(1341446400, tz)
>>> dt
datetime.datetime(2012, 7, 5, 2, 0, tzinfo=<DstTzInfo 'Europe/Skopje'
CEST+2:00:00 DST>)
>>> dt.strftime('%s')
'1341421200'
Python 2.7.3, pytz 2012c
--
damjan
Back to top | Article view | comp.lang.python
csiph-web