Source code for time_of_day
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Calculate time.
:Date: 2019-06-01
.. module:: time_of_day
:platform: *nix, Windows
:synopsis: Calculate time.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
from time import struct_time, mktime
[docs]def in_seconds(time):
"""If time is `time.struct_time` convert to float seconds.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: the time in seconds
:rtype: float
"""
if isinstance(time, struct_time):
time = mktime(time)
return time
[docs]def seconds(time):
"""The seconds of the time.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: seconds, range [0, 60]
:rtype: float
"""
return in_seconds(time)%60
[docs]def seconds_norm(time):
"""The seconds normalized to 60 seconds.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: the normalized seconds, range [0, 1]
:rtype: float
"""
return seconds(time)/60
[docs]def minutes(time):
"""The minutes of the time.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: minutes, range [0, 60]
:rtype: float
"""
return in_seconds(time)/60%60
[docs]def minutes_norm(time):
"""The minutes normalized to 60 minutes.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: the normalized minutes, range [0, 1]
:rtype: float
"""
return minutes(time)/60
[docs]def hours(time):
"""The hours of the time.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: hours, range [0, 24]
:rtype: float
"""
return in_seconds(time)/60/60%24
[docs]def hours_norm(time):
"""The hours normalized to 24 hours.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: the normalized hours, range [0, 1]
:rtype: float
"""
return hours(time)/24
[docs]def days(time):
"""The days of the time (year).
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: hours, range [0, 365.2425]
:rtype: float
"""
return in_seconds(time)/60/60/24%365.2425
[docs]def days_norm(time):
"""The days normalized to 365.2425 (Gregorian, on average) days.
:param time: the time in seconds
:type time: float or `time.struct_time`
:returns: the normalized days, range [0, 1]
:rtype: float
"""
return days(time)/365.2425
if __name__ == "__main__":
from time import time, gmtime, localtime
# time in seconds
t = time()
min = minutes(t)
h = hours(t)
min_norm = minutes_norm(t)
h_norm = hours_norm(t)
print('min ', min)
print('h ', h)
print('min_norm ', min_norm)
print('h_norm ', h_norm)
x_len = 30
x_offset = -8
x_pos = transform(min_norm, x_len, x_offset)
print('m[-8,22] ', x_pos)
y_len = 20
y_offset = -10
y_pos = transform(h_norm, y_len, y_offset)
print('h[-10,10]', y_pos)