-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert.py
55 lines (46 loc) · 1.47 KB
/
convert.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""
@author: Noah Norman
"""
import datetime
from math import floor
def secs_to_day_frac(secs):
""" accepts seconds and returns TOD as fraction """
if secs:
return float(secs) / 86400
else:
return 0
def day_frac_to_secs(day_frac):
""" accepts TOD as fraction and returns seconds """
return float(day_frac) * 86400
def datetime_to_day_frac(dt):
hrs = dt.time().hour
mins = dt.time().minute
secs = dt.time().second
return secs_to_day_frac((hrs * 60 * 60) + (mins * 60) + secs)
def secs_to_hr_min_sec(secs):
""" accepts seconds and returns (h,m,s) tuple """
if secs:
hrs = int(floor(secs / 60 / 60))
mins = int(floor(secs / 60 % 60))
secs = int(floor(secs % 60))
return (hrs, mins, secs)
else:
return (0, 0, 0)
def secs_into_day():
""" returns the number of seconds since midnight """
now = datetime.datetime.now().time()
return ((now.hour * 60 * 60) + (now.minute * 60) + now.second)
def time_from_day_frac(day_frac):
""" accepts TOD as fraction and returns time string as h:m:s """
secs = day_frac_to_secs(day_frac)
c_hr, c_min, c_sec = secs_to_hr_min_sec(secs)
fmt_str = '{}:{}:{}'.format(c_hr, c_min, c_sec)
return fmt_str
def interp(start, end, frac):
return frac * (end - start) + start
def current_time():
ct = secs_to_day_frac(secs_into_day())
return ct