-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalftime
executable file
·66 lines (53 loc) · 2.62 KB
/
halftime
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
#!/usr/bin/python3
"""Print half the time between now and 22:00 (10 pm), or between other specified times.
Copyright (c) 2014--17 Christian Siefkes
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
import argparse
from datetime import date, datetime, time, timedelta
def format_time(t: time) -> str:
""""Format a time or datetime as 'HH:MM:SS'."""
return t.strftime('%H:%M:%S')
def halftime(start: str = None, end: str = None) -> time:
"""
Return half the time from *start* to *end*.
Parameters:
* start: the start time in 'HH:MM' format; e.g. '09:01'; defaults to the current time
* end: the end time in 'HH:MM' format; defaults to '22:00'; if smaller than the start
time, assumed to refer to the next day (e.g. '00:00' means: start of tomorrow)
"""
if start is None:
startdate = datetime.now()
else:
startdate = datetime.combine(date.today(), datetime.strptime(start, '%H:%M').time())
if end is None:
end = '22:00'
endtime = datetime.strptime(end, '%H:%M').time()
delta = datetime.combine(date.today(), endtime) - startdate
# Add one day to a negative delta (assuming that end time refers to tomorrow)
if delta < timedelta(0):
delta += timedelta(1)
target_dt = startdate + delta / 2
return target_dt.time()
if __name__ == '__main__':
try:
parser = argparse.ArgumentParser(description='Print half the time between now and '
+ '22:00 (10 pm), or between other specified times.')
parser.add_argument('-f', '--from', dest='start',
help="the start time in 'HH:MM' format (defaults to the current time)")
parser.add_argument('-t', '--to', dest='end',
help="the end time in 'HH:MM' format (defaults to '22:00')")
args = parser.parse_args()
print(format_time(halftime(args.start, args.end)))
except Exception as e:
error_desc = '{}: {}'.format(e.__class__.__name__, e)
exit(error_desc)