File tree Expand file tree Collapse file tree 2 files changed +56
-6
lines changed
Expand file tree Collapse file tree 2 files changed +56
-6
lines changed Original file line number Diff line number Diff line change 1+ import datetime as dt
2+ from typing import Optional
13
24def bytes (size ):
35
@@ -15,3 +17,52 @@ def bytes(size):
1517 size /= HUMANRADIX
1618
1719 return HUMANFMT .format (size , UNITS [- 1 ])
20+
21+ def _seconds (s : float ):
22+ seconds = int (s )
23+ yield 'seconds' , seconds
24+
25+ minutes = seconds // 60
26+ yield 'minutes' , minutes
27+
28+ hours = minutes // 60
29+ yield 'hours' , hours
30+
31+ days = hours // 24
32+ yield 'days' , days
33+
34+ weeks = days // 7
35+ yield 'weeks' , weeks
36+
37+ months = days // 30
38+ yield 'months' , months
39+
40+ years = days // 365
41+ yield 'years' , years
42+
43+
44+ def _duration (unit : str , count : int ):
45+ return '{count} {unit}' .format (
46+ count = count ,
47+ unit = unit if abs (count ) > 1 else unit [:- 1 ]
48+ )
49+
50+ def seconds (value : float ):
51+ for unit , count in list (_seconds (value ))[::- 1 ]:
52+ if count == 0 : continue
53+ return _duration (unit , count )
54+
55+ return 'now'
56+
57+ def timedelta (delta : dt .timedelta ):
58+ return seconds (delta .total_seconds ())
59+
60+ def datetime (x : dt .datetime , relative_to : Optional [dt .datetime ]= None ):
61+ if relative_to is None :
62+ relative_to = dt .datetime .now (x .tzinfo )
63+
64+ delta = x - relative_to
65+ s = delta .total_seconds ()
66+
67+ suffix = '' if s == 0 else ' ago' if s < 0 else ' from now'
68+ return seconds (abs (s )) + suffix
Original file line number Diff line number Diff line change 1- import arrow
2- import human
3- humanbytes = human .bytes
1+ from datetime import datetime
2+ from human import bytes as humanbytes , datetime as humandatetime
43
54def formatdoc (doc ):
65
@@ -32,9 +31,9 @@ def formatkeys(doc):
3231 hint = '/' .join (map (lambda x : x [:2 ], path .split ('/' )[3 :- 1 ]))
3332
3433 try :
35- timestamp = arrow . get ( doc .mtime )
36- htime = timestamp . humanize ( )
37- hdate = timestamp .format ( 'YYYY-MM-DDTHH:mm:ssZZ' )
34+ timestamp = datetime . fromtimestamp ( int ( doc .mtime ) )
35+ htime = humandatetime ( timestamp )
36+ hdate = timestamp .isoformat ( )
3837 except Exception :
3938 htime = 'unknown'
4039 hdate = 'unknown'
You can’t perform that action at this time.
0 commit comments