From 3280b423e463fcb64b9c57e0a6c0327f3b5fadb3 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 7 Jul 2020 22:17:40 -0700 Subject: [PATCH] Rewrite sdate to take strftime strings. I decided not to attempt to handle %c for locale-dependent full date strings. --- sdate.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/sdate.py b/sdate.py index f4d9fc3..df14857 100644 --- a/sdate.py +++ b/sdate.py @@ -1,25 +1,40 @@ import datetime +import re import time EPOCH = datetime.datetime( year=1993, month=9, day=1, - tzinfo=datetime.timezone.utc, ) -def sdate(): - (day, hms) = sdate_tuple() - return f'1993 September {day} {hms}' - -def sdate_tuple(): - now = datetime.datetime.now(datetime.timezone.utc) +def strftime(format, tpl=None): + now = datetime.datetime.now() diff = now - EPOCH - day = diff.days + 1 - (minutes, seconds) = divmod(diff.seconds, 60) - (hours, minutes) = divmod(minutes, 60) - hms = f'{hours:02}:{minutes:02}:{seconds:02}' - return (day, hms) + + day = str(diff.days + 1) + day_of_year = str(244 + diff.days) + + changes = { + r'%b': 'Sep', + r'%B': 'September', + r'%d': day, + r'%-d': day, + r'%j': day_of_year, + r'%-j': day_of_year, + r'%m': '09', + r'%-m': '9', + r'%Y': '1993', + r'%y': '93', + } + for (key, value) in changes.items(): + key = r'(?