Remake get_timestamp() to work properly with various timezones.

Today was the change to Daylight Savings for a part of the world, and
the get_timestamp() function broke because of this. This change fixes
this.
This commit is contained in:
Vgr E.Barry 2015-03-08 09:49:36 -04:00
parent 5a3311aa99
commit a1ef3beca2

View File

@ -6,22 +6,24 @@ import time
# since windows likes to use weird encodings by default # since windows likes to use weird encodings by default
utf8stdout = open(1, 'w', errors="replace", closefd=False) # stdout utf8stdout = open(1, 'w', errors="replace", closefd=False) # stdout
def get_timestamp(): def get_timestamp(use_utc=None, ts_format=None):
"""Returns a timestamp with timezone + offset from UTC.""" """Return a timestamp with timezone + offset from UTC."""
if botconfig.USE_UTC: if use_utc is None:
tmf = datetime.datetime.utcnow().strftime(botconfig.TIMESTAMP_FORMAT) use_utc = botconfig.USE_UTC
if tmf[-1] != " ": if ts_format is None:
tmf += " " ts_format = botconfig.TIMESTAMP_FORMAT
return tmf.format(tzname="UTC", tzoffset="+0000") if use_utc:
tmf = time.strftime(botconfig.TIMESTAMP_FORMAT) tmf = datetime.datetime.utcnow().strftime(ts_format)
if tmf[-1] != " ": tz = "UTC"
tmf += " " offset = "+0000"
tz = time.strftime("%Z") else:
utctime = datetime.datetime.utcnow().strftime("%H") tmf = time.strftime(ts_format)
nowtime = datetime.datetime.now().strftime("%H") tz = time.tzname[0]
offset = "-" if int(utctime) > int(nowtime) else "+" offset = "+"
offset += str(time.timezone // 36).zfill(4) if datetime.datetime.utcnow().hour > datetime.datetime.now().hour:
return tmf.format(tzname=tz, tzoffset=offset).upper() offset = "-"
offset += str(time.timezone // 36).zfill(4)
return tmf.format(tzname=tz, tzoffset=offset).strip().upper() + " "
def logger(file, write=True, display=True): def logger(file, write=True, display=True):
if file is not None: if file is not None: