-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeDiff
executable file
·248 lines (208 loc) · 8.09 KB
/
timeDiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python3
#
# timeDiff
# 2017-02-02: Written by Steven J. DeRose.
#
import sys
import argparse
import re
import math
from subprocess import check_output, CalledProcessError
import datetime
#from datetime import tzinfo
__metadata__ = {
"title" : "timeDiff",
"description" : "Show the difference between any two dates/times.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.7",
"created" : "2017-02-02",
"modified" : "2020-08-31",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__['modified']
descr = """
=Description=
Show the difference between any two dates/times.
The times may be in any form supported by the *nix C<date> command (q.v.).
If only one time is provided, the second defaults to the current time.
=Related Commands=
My C<addup>. *nix C<date>, C<w>, C<last>. Library C<strftime>().
=Known Bugs and Limitations=
Omitting the second time argument doesn't work reliably.
Does not break out time difference units larger than days,
because the Python display used (datetime.delta) doesn't either.
This would be a nice option.
Could be useful packaged as a tiny module.
=History=
* 2017-02-02: Written by Steven J. DeRose.
* Creative Commons Attribution-Share-alike 3.0 unported license.
* See http://creativecommons.org/licenses/by-sa/3.0/.
* 2017-12-03: Add --full.
* 2020-08-31: New layout.
=To do=
=Licensing=
Copyright 2015 by Steven J. DeRose. This script is licensed under a
Creative Commons Attribution-Share-alike 3.0 unported license.
See http://creativecommons.org/licenses/by-sa/3.0/ for more information.
=Options=
"""
###############################################################################
#
def processOptions():
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--full", "-F", action='store_true',
help='Make output include the start/end times, too.')
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Make output terse.')
parser.add_argument(
"--round", type=int, default=0, metavar="M",
help='Round to multiple of M minutes. Default: 0 (no rounding).')
parser.add_argument(
"--seconds", "-s", action='store_true',
help='Just show total number of seconds.')
parser.add_argument(
"--utc", action='store_true',
help='When defaulting time2, make it UTC instead of local.')
parser.add_argument(
"--verbose", "-v", action='count', default=0,
help='Add more messages (repeatable).')
parser.add_argument(
"--version", action='version', version=__version__,
help='Display version information, then exit.')
parser.add_argument(
"time1", type=str,
help='Starting time.')
parser.add_argument(
"time2", type=str, nargs='?',
help='Ending time (optional?).')
args0 = parser.parse_args()
return(args0)
###############################################################################
# Main
#
# Time formats Python knows:
#
# ABBR Python doc name Description
# =============================================================================
# TS: timestamp: seconds since epoch (float)
# C: ctime: readable string (see below)
# DT: datetime: object
# D: date: object
# T: time: object
# TT: timetuple: A time.struct_time, mostly like C commonly uses:
# [
# 0 tm_year (for example, 1993)
# 1 tm_monrange [1, 12] ### Unlike C!
# 2 tm_mdayrange [1, 31]
# 3 tm_hourrange [0, 23]
# 4 tm_minrange [0, 59]
# 5 tm_secrange [0, 61]
# 6 tm_wdayrange [0, 6], Monday is 0
# 7 tm_ydayrange [1, 366]
# 8 tm_isdst 0, 1 or -1; see below
# N/A tm_zone abbreviation of timezone name
# N/A tm_gmt offoffset east of UTC in seconds
# ]
# I8: ISO8601: yyyy-mm-dd
# PO: Proleptic ordinal Day since 01/01/0001:
#
# Conversions (assuming UTC. localtime variant in /.../]):
# ctime FROM datetime C =
# ctime FROM timetuple C = time.asctime(TT)
# ctime FROM timestamp C = /time.ctime(TT)/
# datetime FROM ctime DT = datetime.strptime(C)
# datetime FROM timetuple DT = new datetime(TT[0:5])
# datetime FROM timestamp DT = datetime.fromtimestamp(TS)
# timetuple FROM ctime TT =
# timetuple FROM datetime TT = utctimetuple() /timetuple()/
# timetuple FROM timestamp TT =
# timestamp FROM ctime TS =
# timestamp FROM datetime TS = (DT-datetime(1970,1,1)).total_seconds()
# timestamp FROM timetuple TS = time.mktime(TT)
#
# Additional conversions:
# datetime FROM date DT =
# datetime FROM time DT =
# datetime FROM date, time DT =
# time FROM datetime T =
# date FROM datetime D =
# date FROM timesstamp D = datetime.fromtimestamp(TS)
# date FROM ordinal D = date.fromordinal(ordinal)
# ordinal FROM date PO = D.toordinal()
# ctime FROM date C = D.ctime()
#
args = processOptions()
# *nix 'date' produces a ctime string.
# Get everything into that form, one way or another.
#
# ctime: Thu Feb 2 10:59:18 EST 2017
# date Thu Feb 2 14:28:22 EST 2017 (Linux)
# date -u Thu Feb 2 19:31:38 UTC 2017 (Linux and BSD)
# date -R Thu, 02 Feb 2017 14:28:49 -0500 (Linux)
# date --rfc-3339=seconds 2017-02-02 14:30:43-05:00 (Linux)
# Normal ctimeFormat = "%a %b %d %H:%M:%S %Z %Y"
ctimeFormat = "%a %b %d %H:%M:%S %Z %Y"
###############################################################################
# JUST FETCH THE TIMES ('date' returns like 'Sun Dec 3 12:59:51 EST 2017'
try:
if (args.utc):
ctime1 = check_output([ 'date', '-u', '--date', args.time1 ])
if (args.time2):
ctime2 = check_output([ 'date', '-u', '--date', args.time2 ])
else:
ctime2 = check_output([ 'date', '-u' ])
else:
ctime1 = check_output([ 'date', '--date', args.time1 ])
if (args.time2):
ctime2 = check_output([ 'date', '--date', args.time2 ])
else:
ctime2 = check_output([ 'date' ])
except CalledProcessError as e:
print("Unable to feth ctime via *nix 'date' command.")
sys.exit()
ctime1 = ctime1.strip()
ctime2 = ctime2.strip()
if (args.verbose):
print("start %s, end %s" % (ctime1, ctime2))
###############################################################################
# CONVERT THE TIMES
#
try:
ctime1 = ctime1.strip()
ctime1 = re.sub(r' ', ' 0', ctime1)
dt1 = datetime.datetime.strptime(ctime1, ctimeFormat)
ctime2 = ctime2.strip()
ctime2 = re.sub(r' ', ' 0', ctime2)
dt2 = datetime.datetime.strptime(ctime2, ctimeFormat)
except ValueError as e:
print("Unable to convert ending ctime:\n %s" % (e))
sys.exit()
if (args.verbose):
print("Normalized start: %28s -> '%s' (%s)." % (ctime1, dt1, type(dt1)))
print("Normalized end: %28s -> '%s'." % (ctime2, dt2))
delta = dt2-dt1
if (args.verbose): print("Delta: %s" % (delta))
if (args.round):
s = delta.total_seconds()
roundSec = 60 * args.round
rounded = roundSec * math.floor((s + roundSec/2.0) / roundSec)
if (args.verbose): print("Exact: %d; rounded: %d." % (s, rounded))
delta = datetime.timedelta(0, rounded)
msg = ""
if (args.full): msg = "%s - %s => " % (ctime1, ctime2)
if (args.seconds):
print(msg + delta.total_seconds())
else:
pdelta = "%s" % (delta)
pdelta = re.sub(r'\b(\d:)', '0\\1', pdelta) # Leading zero....
print(msg + pdelta)