Rewrite sdate to take strftime strings.
I decided not to attempt to handle %c for locale-dependent full date strings.
This commit is contained in:
parent
d5a59d9f9e
commit
3280b423e4
1 changed files with 28 additions and 13 deletions
41
sdate.py
41
sdate.py
|
@ -1,25 +1,40 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
EPOCH = datetime.datetime(
|
EPOCH = datetime.datetime(
|
||||||
year=1993,
|
year=1993,
|
||||||
month=9,
|
month=9,
|
||||||
day=1,
|
day=1,
|
||||||
tzinfo=datetime.timezone.utc,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def sdate():
|
def strftime(format, tpl=None):
|
||||||
(day, hms) = sdate_tuple()
|
now = datetime.datetime.now()
|
||||||
return f'1993 September {day} {hms}'
|
|
||||||
|
|
||||||
def sdate_tuple():
|
|
||||||
now = datetime.datetime.now(datetime.timezone.utc)
|
|
||||||
diff = now - EPOCH
|
diff = now - EPOCH
|
||||||
day = diff.days + 1
|
|
||||||
(minutes, seconds) = divmod(diff.seconds, 60)
|
day = str(diff.days + 1)
|
||||||
(hours, minutes) = divmod(minutes, 60)
|
day_of_year = str(244 + diff.days)
|
||||||
hms = f'{hours:02}:{minutes:02}:{seconds:02}'
|
|
||||||
return (day, hms)
|
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'(?<!%)' + key
|
||||||
|
format = re.sub(key, value, format)
|
||||||
|
|
||||||
|
if tpl is not None:
|
||||||
|
return time.strftime(format, tpl)
|
||||||
|
else:
|
||||||
|
return time.strftime(format)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(sdate())
|
print(strftime('%Y-%m-%d %H:%M:%S'))
|
||||||
|
|
Loading…
Reference in a new issue