0

I have a web app that captures date / time via a JS script and calculates seconds since epoch in UTC format - ex 134250270000. In the backend we have a Python script that fetches data from DB but has date / time stored in number of seconds since epoch in PST format. There is always a difference of seconds between UTC and PST if counted from epoch.

Is there any method by which I can convert the UTC seconds since to epoch to PST seconds since epoch? We need to take a note of daylight changes in PST timezone also?

EDIT::

I have the seconds since epoch in UTC format:

1342502700

I found that I get the sum in seconds between UTC and local standard time via:

>>> time.timezone / 3600
8

So If I add 1342502700 to time.timezone:

>>> print 1342502700 + time.timezone
1344153600

Will it always give me PDT / PST times correctly?

EDIT::

Maybe this is the correct one:

>>> import time
>>> offset = time.timezone if (time.daylight == 0) else time.altzone
>>> offset / 60 / 60
7

time.daylight will be non-zero if daylight savings is currently in effect.

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 2
    I could be wrong, but I'm pretty sure that 'seconds since epoch' is always measured according to UTC. The number of seconds since the epoch is the same no matter where you are on the planet, but how that translates into an hour of the day based your local time zone is different. – DaoWen Aug 13 '12 at 08:31
  • I am not sure either - cause 134250270000 in UTC format prints one value and 134250270000 in PST formats prints other? If so please let me know how I convert this seconds since epoch 134250270000 to PST one. – Programmer Aug 13 '12 at 08:36
  • 1
    That sounds like the correct behavior to me. UTC is the same as GMT, so printing the seconds-since-epoch in UTC format should be a few hours off from printing the same value in PST format. – DaoWen Aug 13 '12 at 08:42
  • 1
    You can check how many hours off PST is from UTC [on Wikipedia](http://en.wikipedia.org/wiki/PST). Interestingly, the PST page lists time zones for US Pacific Time, the Philippines and Pakistan! – DaoWen Aug 13 '12 at 08:44
  • Thanks. The issue is that when I convert a UTC date / time say 08/03/2012 00:00:00 to seconds since epoch and convert back to UTC / PST format - they are different. Is there a way that the seconds calculated since epoch calculated using UTC date / time gives me PST date / time? Also that there are different timezones - PDT / PST - so how can I find the exact time in PST / PDT format when I have seconds since epoch using a UTC date / time? – Programmer Aug 13 '12 at 08:49

1 Answers1

0

I think somewhere in your conversions there's an implied time zone that's messing things up. Regardless, I think what you're looking for is time.altzone:

>>> from time import *
>>> ctime(time())
'Mon Aug 13 16:54:02 2012'
>>> ctime(time()+altzone)
'Mon Aug 13 08:54:04 2012'

I'm on Taipei time right now, so my local time (the first one) is later than UTC.

EDIT: I missed the line in the documentation that says only to use altzone if daylight is non-zero. Apparently it's a bit more complicated than I thought if you live somewhere with Daylight Savings Time. Refer to this post if you need to deal with that:

Python: get timezone based on DST for a timestamp

Community
  • 1
  • 1
DaoWen
  • 32,589
  • 6
  • 74
  • 101