-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeats.py
67 lines (57 loc) · 1.77 KB
/
beats.py
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
"""Module to calculate beats(Swatch Internet Time).
"""
from datetime import datetime
def internettime(hours, minutes, seconds, tzone, centibeats=False):
"""Returns time(Swatch Internet Time) in beats.
"""
itime = ((seconds + (minutes * 60) + ((hours + tzone + 1) * 3600)) / 86.4) % 1000
beats = int(itime)
cbeats = str(itime).split(".")[1][0:3]
if centibeats:
return "@{0}.{1}".format(beats, cbeats)
return "@{0}".format(beats)
def now(centibeats=False):
"""Gets the current time in beats.
"""
utc_now = datetime.utcnow()
beats = internettime(
utc_now.hour, utc_now.minute, utc_now.second, 0, centibeats
)
return beats
def main():
"""The main function.
"""
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"-c",
"--centibeats",
action="store_true",
help="Display centibeats",
default=False,
)
parser.add_argument("-t", "--time", help="Time to convert (HH:MM:SS)")
parser.add_argument(
"-z",
"--timezone",
metavar="TZ",
type=float,
help="Timezone in hours, default: UTC+01",
default=1,
)
parser.add_argument("-v", "--version", action="store_true")
args = parser.parse_args()
if args.version:
print(".beat v{0}".format(pkg_resources.get_distribution("beat").version))
return
if args.time:
hours, minutes, seconds = args.time.split(":")
print(
internettime(
int(hours), int(minutes), int(seconds), args.timezone, args.centibeats
)
)
else:
beats = now(args.centibeats)
print(beats)
if __name__ == "__main__":
main()