January 24, 2017

Python - Working With Time

When processing log, we need to express the time in its local timezone for correlation. most of the time we don’t need te fancy datetime and time is good enough for us.

Time String <=> Seconds Since Epoc

from time import (tzset,
    strptime, strftime,
    mktime,
    localtime, gmtime,
    struct_time)
from calendar import timegm

format = "%Y-%m-%dT%H:%M:%S%p"

#os.environ['TZ'] = "US/Pacific"
#tzset()

ts = "2017-01-23T11:43:40PM"
st = strptime(ts, format)

# as local time
print("%s => %d" % (ts, mktime(st)))
# 2017-01-23T11:43:40PM => 1485200620

# as utc time
print("%s => %d" % (ts, timegm(st)))
# 2017-01-23T11:43:40PM => 1485171820

epoc = 1485200620

# to local time
t = localtime(epoc)
print("%d => %s" % (epoc, strftime(format, t)))
# 1485200620 => 2017-01-23T11:43:40AM

# to utc time
t = gmtime(epoc)
print("%d => %s" % (epoc, strftime(format, t)))
# 1485200620 => 2017-01-23T19:43:40PM

# manipulate struct_time
tl = list(t)
tl[0] = 2018
t = struct_time(tl)
print("=> %s" % strftime(format, t))
#=> 2018-01-23T19:43:40PM

Working With Timezone (without 3rd party lib)

from time import localtime, tzset, gmtime, strptime
from calendar import timegm

timezones = ["Asia/Shanghai", "America/Los_Angeles", "GMT"]
for tz in timezones:
    os.environ['TZ'] = tz
    tzset()

    for time_str in (
            "2017-01-23T11:43:40PM", # daylight saving time
            "2017-04-23T11:43:40PM"
            ):
        st = strptime(time_str, "%Y-%m-%dT%H:%M:%S%p")
        as_local = mktime(st)
        as_utc = timegm(st)
        offset = as_utc - as_local

        print("%s => UTC %+03d%02d" % (tz, offset/60/60, offset/60%30))

#Asia/Shanghai => UTC +0800
#Asia/Shanghai => UTC +0800
#America/Los_Angeles => UTC -0800
#America/Los_Angeles => UTC -0700
#GMT => UTC +0000
#GMT => UTC +0000

Reference