155 lines
3.3 KiB
Python
155 lines
3.3 KiB
Python
#!/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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
def transform(time_norm, length, offset=0):
|
|
"""Transform normalized time value to new length.
|
|
|
|
:param position_norm: the normalized time value to transform
|
|
:type position_norm: float
|
|
:param length: the transformation
|
|
:type length: float
|
|
:param offset: the offset (default = 0)
|
|
:type offset: float
|
|
|
|
:returns: the transformation value
|
|
:rtype: float
|
|
"""
|
|
return time_norm*length + offset
|
|
|
|
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)
|
|
|