* Added wpylib.datetime.utime_to_iso() function.

master
Wirawan Purwanto 11 years ago
parent 42babbe767
commit 0675c6c599
  1. 28
      datetime.py

@ -37,3 +37,31 @@ def shift_time(t, dt, localtime=True):
else: else:
return time.gmtime(t1) return time.gmtime(t1)
def utime_to_iso(t, local=True):
"""Converts UNIX time (seconds since Epoch) to ISO-formatted time string.
In order to ease time computation/conversion,
we will use numeric TZ shift (+/-HH:MM) instead of
letter TZ code (EDT, EST, GMT, etc).
"""
from time import localtime, gmtime, strftime, timezone
if local:
tt = localtime(t)
dtz = -timezone + tt.tm_isdst * 3600
# hopefully dtz is divisible by 60
# It would be silly for a gov't to make its time
# differ by seconds to GMT!
dsec = abs(dtz) % 60
dmin = abs(dtz // 60) % 60
dhr = dtz // 3600
if dsec == 00:
sdtz = " %+03d:%02d" % (dhr, dmin)
else:
sdtz = " %+03d:%02d:%02d" % (dhr, dmin, dsec)
else:
tt = gmtime(t)
sdtz = " +00:00"
# Numeric timezone offset is created by hand
# because I don't want to use %Z, nor do I want to use
# "%z" (which did not work in python)
return strftime("%Y-%m-%d %H:%M:%S", tt) + sdtz

Loading…
Cancel
Save