Skip to content

Commit b641325

Browse files
committed
Add a function for formatting numeric timestamp representations
1 parent 503bad4 commit b641325

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

structa/format.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from math import log
88
from itertools import tee
9-
from datetime import datetime
9+
from datetime import datetime, timedelta
1010

1111

1212
def pairwise(iterable):
@@ -153,3 +153,29 @@ def format_sample(value):
153153
}[type(value)]()
154154
except KeyError:
155155
raise ValueError('invalid type for value {!r}'.format(value))
156+
157+
158+
def format_timestamp_numrepr(offset, scale):
159+
delta = timedelta(seconds=scale)
160+
simple = {
161+
timedelta(**{name: 1}): name
162+
for name in (
163+
'microseconds',
164+
'milliseconds',
165+
'seconds',
166+
'minutes',
167+
'hours',
168+
'days',
169+
'weeks',
170+
)
171+
}
172+
if offset % 86400:
173+
epoch = datetime.utcfromtimestamp(offset).isoformat()
174+
else:
175+
epoch = datetime.utcfromtimestamp(offset).date().isoformat()
176+
try:
177+
return '{unit} since {epoch}'.format(unit=simple[delta], epoch=epoch)
178+
except KeyError:
179+
return 'seconds since {epoch} {op} {scale}'.format(
180+
epoch=epoch, op=('*', '/')[scale >= 1],
181+
scale=scale if scale >= 1 else (1 / scale))

0 commit comments

Comments
 (0)