Commit 68500fad authored by YOU's avatar YOU Committed by Dylan Trotter

Add struct_time, gmtime, localtime, mktime, strftime to lib/time (#219)

parent 348f0380
......@@ -14,7 +14,74 @@
"""Time access and conversions."""
from __go__.time import Now, Second, Sleep # pylint: disable=g-multiple-import
from __go__.time import Now, Second, Sleep, Unix, Date, UTC # pylint: disable=g-multiple-import
_strftime_directive_map = {
'%': '%',
'a': 'Mon',
'A': 'Monday',
'b': 'Jan',
'B': 'January',
'c': NotImplemented,
'd': '02',
'H': '15',
'I': '03',
'j': NotImplemented,
'L': '.000',
'm': '01',
'M': '04',
'p': 'PM',
'S': '05',
'U': NotImplemented,
'W': NotImplemented,
'w': NotImplemented,
'X': NotImplemented,
'x': NotImplemented,
'y': '06',
'Y': '2006',
'Z': 'MST',
'z': '-0700',
}
class struct_time(tuple): #pylint: disable=invalid-name,missing-docstring
def __init__(self, args):
super(struct_time, self).__init__(tuple, args)
self.tm_year = self[0]
self.tm_mon = self[1]
self.tm_mday = self[2]
self.tm_hour = self[3]
self.tm_min = self[4]
self.tm_sec = self[5]
self.tm_wday = self[6]
self.tm_yday = self[7]
self.tm_isdst = self[8]
def __repr__(self):
return ("time.struct_time(tm_year=%s, tm_mon=%s, tm_mday=%s, "
"tm_hour=%s, tm_min=%s, tm_sec=%s, tm_wday=%s, "
"tm_yday=%s, tm_isdst=%s)") % self
def __str__(self):
return repr(self)
def gmtime(seconds=None):
t = (Unix(seconds, 0) if seconds else Now()).UTC()
return struct_time((t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(),
t.Second(), (t.Weekday() + 6) % 7, t.YearDay(), 0))
def localtime(seconds=None):
t = (Unix(seconds, 0) if seconds else Now()).Local()
return struct_time((t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(),
t.Second(), (t.Weekday() + 6) % 7, t.YearDay(), 0))
def mktime(t):
return float(Date(t[0], t[1], t[2], t[3], t[4], t[5], 0, UTC).Unix())
def sleep(secs):
......@@ -23,3 +90,31 @@ def sleep(secs):
def time():
return float(Now().UnixNano()) / Second
def strftime(format, tt=None): # pylint: disable=missing-docstring,redefined-builtin
t = (Unix(int(mktime(tt)), 0) if tt else Now()).Local()
ret = []
prev, n = 0, format.find('%', 0, -1)
while n != -1:
ret.append(format[prev:n])
next_ch = format[n + 1]
c = _strftime_directive_map.get(next_ch)
if c is NotImplemented:
raise NotImplementedError('Code: %' + next_ch + ' not yet supported')
if c:
ret.append(t.Format(c))
else:
ret.append(format[n:n+2])
n += 2
prev, n = n, format.find('%', n, -1)
ret.append(format[prev:])
return ''.join(ret)
# TODO: Calculate real value for daylight saving.
daylight = 0
# TODO: Use local DST instead of ''.
tzname = (Now().Zone()[0], '')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment