From a19a216bc60160c162e616145ef091dd18ce4e61 Mon Sep 17 00:00:00 2001 From: Skip Montanaro Date: Tue, 16 Feb 2021 14:40:46 -0600 Subject: Python 0.9.1 as posted in alt.sources --- lib/localtime.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/localtime.py (limited to 'lib/localtime.py') diff --git a/lib/localtime.py b/lib/localtime.py new file mode 100644 index 0000000..5ce6529 --- /dev/null +++ b/lib/localtime.py @@ -0,0 +1,53 @@ +# module localtime -- Time conversions + +import posix + +epoch = 1970 # 1 jan 00:00:00, UCT +day0 = 4 # day 0 was a thursday + +day_names = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat') + +month_names = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun') +month_names = month_names + ('Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec') + +month_sizes = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) + +def isleap(year): + return year % 4 = 0 and (year % 100 <> 0 or year % 400 = 0) + +def gmtime(secs): # decode time into UCT + mins, secs = divmod(secs, 60) + hours, mins = divmod(mins, 60) + days, hours = divmod(hours, 24) + wday = (day0 + days) % 7 + year = epoch + lp = isleap(year) + dpy = 365 + lp + while days >= dpy: + days = days - dpy + year = year + 1 + lp = isleap(year) + dpy = 365 + lp + yday = days + month = 0 + dpm = month_sizes[month] + (lp and month = 1) + while days >= dpm: + days = days - dpm + month = month + 1 + dpm = month_sizes[month] + (lp and month = 1) + return (year, month, days+1, hours, mins, secs, yday, wday) + +def dd(x): + s = `x` + while len(s) < 2: s = '0' + s + return s + +def zd(x): + s = `x` + while len(s) < 2: s = ' ' + s + return s + +def format(year, month, days, hours, mins, secs, yday, wday): + s = day_names[wday] + ' ' + zd(days) + ' ' + month_names[month] + ' ' + s = s + dd(hours) + ':' + dd(mins) + ':' + dd(secs) + return s -- cgit v1.2.3