Skip to API documentation
timevalue()
is a class for dealing with time values (eg, 5 minutes, 2 weeks, 30 seconds) in a simple form. timevalues are cleanly used in time/datetime calculations and print statements. There's no magic here, just convenience and cleaner code.
retime()
translates an int or float resolution-seconds value into a new target resolution.
Creating a timevalue instance gives ready access to the value in seconds, and useful unit_chr
and unit_str
strings for use in printing and logging.
Using timevalues in configuration files is quite convenient.
- A service loop time param may be expressed as
5m
(5 minutes), rather than300
(hard coded for seconds), or5
(hard coded for minutes) - A failed command retry interval param my be expressed a
3s
(3 seconds), rather than3
(what are the time units again?)
Given
#!/usr/bin/env python3
# ***** timevalue_ex1.py *****
import time
from cjnfuncs.timevalue import timevalue, retime
xx = timevalue('0.5m')
print (xx)
print (f"Sleep <{xx.seconds}> seconds")
time.sleep(xx.seconds)
print()
yy = timevalue("1w")
print (f"{yy.orig_val} = {yy.seconds} seconds = {retime(yy.seconds, 'h')} hours")
Output:
$ ./timevalue_ex1.py
.orig_val : 0.5m <class 'str'>
.seconds : 30.0 <class 'float'>
.unit_char : m <class 'str'>
.unit_str : mins <class 'str'>
Sleep <30.0> seconds
1w = 604800.0 seconds = 168.0 hours
timevalue()
accepts int and float values, with assumed seconds resolution, and accepts int and float values with a case insensitive time unit suffix character:
unit_char suffix | unit_str |
---|---|
s | secs |
m | mins |
h | hours |
d | days |
w | weeks |
timevalue()
provides a convenience mechanism for working with time values and time/datetime calculations.
timevalues are generally an integer value with an attached single character time resolution, such as "5m".
Supported timevalue units are 's'econds, 'm'inutes, 'h'ours, 'd'ays, and 'w'eeks, and are case insensitive.
timevalue()
also accepts integer and float values, which are interpreted as seconds resolution. Also see retime().
orig_val
(str, int, or float)
- The original, passed-in value
- Handle to instance
- Raises ValueError if given an unsupported time unit suffix.
.orig_val
- orig_val value passed in, type str (converted to str if int or float passed in).seconds
- time value in seconds resolution, type float, useful for time calculations.unit_char
- the single character suffix unit of theorig_val
value. 's' for int and float orig_val values..unit_str
- the long-form units of theorig_val
value useful for printing/logging ("secs", "mins", "hours", "days", or "weeks")
retime()
translates a value is resolution seconds into a new target resolution
time_sec
(int or float)
- The time value in resolution seconds to be converted
unitC
(str)
- Target time resolution: "s", "m", "h", "d", or "w" (case insensitive)
time_sec
value scaled for the specifiedunitC
, type float- Raises ValueError if not given an int or float value for
time_sec
, or given an unsupported unitC time unit suffix.