[Tutor] datetime, time zones, and ISO time
Kent Johnson
kent37 at tds.net
Wed Feb 17 23:10:07 CET 2010
On Wed, Feb 17, 2010 at 4:37 PM, David Perlman <dperlman at wisc.edu> wrote:
> OK, here's a function that does precisely what I want:
>
> def tzDelta():
> """by whatever means necessary, return the current offset of the local time
> from utc."""
> s=time.time()
> t,u=time.localtime(s),time.gmtime(s)
> osec=3600*(t[3]-u[3]) + 60*(t[4]-u[4]) + (t[5]-u[5])
> return datetime.timedelta(seconds=osec)
>
>
> As far as I can tell, this should always work. So wouldn't it be nice if
> there were a less convoluted way to get this??
Here is a shorter version based on the LocalTimezone example, and it
only gets the time once so there is no possible race condition:
In [5]: from datetime import timedelta
In [6]: import time
In [15]: def utcoffset():
....: if time.localtime().tm_isdst > 0:
....: return timedelta(seconds = -time.altzone)
....: return timedelta(seconds = -time.timezone)
....:
In [16]: utcoffset()
Out[16]: datetime.timedelta(-1, 68400)
Kent
More information about the Tutor
mailing list