propertime package¶
Submodules¶
propertime.time module¶
Time and TimeUnit classes
-
class
propertime.time.Time(value=None, *args, **kwargs)¶ Bases:
floatA Time object, as a floating point number representing the number of seconds passed after the zero on the time axis, which is set to 1st January 1970 UTC. Any other representations (as date/hours, time zones, daylight saving times) are built on top of it.
-
property
tz¶ The time zone of the time. Set it to any valid time zone (object or string representation) to change it.
-
property
offset¶ The (UTC) offset of the time. Set it to any valid number to change it.
-
dt()¶ Return time as a datetime object.
-
iso()¶ Return time as a string in ISO 8601 format.
-
conjugate()¶ Disabled. It does not make sense to use imaginary numbers with time.
-
imag()¶ Disabled. It does not make sense to use imaginary numbers with time.
-
real()¶ Disabled. It does not make sense to use imaginary numbers with time.
-
as_integer_ratio()¶ Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original float and with a positive denominator.
Raise OverflowError on infinities and a ValueError on NaNs.
>>> (10.0).as_integer_ratio() (10, 1) >>> (0.0).as_integer_ratio() (0, 1) >>> (-.25).as_integer_ratio() (-1, 4)
-
fromhex()¶ Create a floating-point number from a hexadecimal string.
>>> float.fromhex('0x1.ffffp10') 2047.984375 >>> float.fromhex('-0x1p-1074') -5e-324
-
hex()¶ Return a hexadecimal representation of a floating-point number.
>>> (-0.1).hex() '-0x1.999999999999ap-4' >>> 3.14159.hex() '0x1.921f9f01b866ep+1'
-
is_integer()¶ Return True if the float is an integer.
-
property
-
class
propertime.time.TimeUnit(value=None, years=0, weeks=0, months=0, days=0, hours=0, minutes=0, seconds=0, microseconds=0, trustme=False)¶ Bases:
objectA time unit object, that can have both fixed (physical) or variable (calendar) time length. It can handle precision up to the microsecond and can be added and subtracted with numerical values, Time and datetime objects, and other TimeUnits.
Can be initialized both using a numerical value, a string representation, or by explicitly setting years, months, weeks, days, hours, minutes, seconds and microseconds. In the string representation, the mapping is as follows:
'Y': 'years''M': 'months''W': 'weeks''D': 'days''h': 'hours''m': 'minutes''s': 'seconds''u': 'microseconds'
For example, to create a time unit of one hour, the following three are equivalent, where the first one uses the numerical value, the second the string representation, and the third explicitly sets the time component (hours in this case):
TimeUnit('1h'),TimeUnit(hours=1), orTimeUnit(3600). Not all time units can be initialized using the numerical value, in particular calendar time units which can have variable duration: a time unit of one day, orTimeUnit('1d'), can last for 23, 24 or 24 hours depending on DST changes. On the contrary, aTimeUnit('24h')will always last 24 hours and can be initialized asTimeUnit(86400)as well.- Parameters:
value – the time unit value, either as seconds (float) or string representation according to the mapping above.
years – the time unit years component.
weeks – the time unit weeks component.
months – the time unit weeks component.
days – the time unit days component.
hours – the time unit hours component.
minutes – the time unit minutes component.
seconds – the time unit seconds component.
microseconds – the time unit microseconds component.
trustme – a boolean switch to skip checks.
-
property
value¶ The value of the TimeUnit, as its string representation.
-
property
type¶ The type of the TimeUnit.
“Physical” if based on hours, minutes, seconds and microseconds, which have fixed duration.
“Calendar” if based on years, months, weeks and days, which have variable duration depending on the starting date, and their math is not always well defined (e.g. adding a month to the 30th of January does not make sense).
-
is_physical()¶ Return True if the TimeUnit type is physical, False otherwise.
-
is_calendar()¶ Return True if the TimeUnit type is calendar, False otherwise.
-
round(time, how=None)¶ Round a Time or datetime according to this TimeUnit.
-
floor(time)¶ Floor a Time or datetime according to this TimeUnit.
-
ceil(time)¶ Ceil a Time or datetime according to this TimeUnit.
-
shift(time, times=1)¶ Shift a given Time or datetime n times this TimeUnit.
-
as_seconds(start=None)¶ The duration of the TimeUnit in seconds.
Time manipulation utilities
-
propertime.utilities.timezonize(tz)¶ Convert a string representation of a time zone to its pytz object, or do nothing if the argument is already a pytz time zone or tzoffset, or None.
-
propertime.utilities.is_dt_inconsistent(dt)¶ Check that a datetieme object is consistent with its time zone (some conditions can lead to have summer time set in winter, or to end up in non-existent times as when changing DST).
-
propertime.utilities.is_dt_ambiguous_without_offset(dt)¶ Check if a datetime object is specified in an ambigous way on a given time zone
-
propertime.utilities.now_s()¶ Return the current time in epoch seconds.
-
propertime.utilities.now_dt(tz='UTC')¶ Return the current time in datetime format.
-
propertime.utilities.dt(*args, **kwargs)¶ Initialize a datetime object with the time zone in the proper way. Using the standard datetime initilization leads to various problems if setting a pytz time zone.
- Parameters:
year (int) – the year.
month (int) – the month.
day (int) – the day.
hour (int) – the hour, defaults to 0.
minute (int) – the minute, Defaults to 0.
second (int) – the second, Defaults to 0.
microsecond (int) – the microsecond, Defaults to None.
tz (tzinfo, pytz, str) – the time zone, defaults to None.
offset_s (int,float) – an optional offset, in seconds.
trustme (bool) – if to skip sanity checks. Defaults to False.
-
propertime.utilities.get_tz_offset(dt)¶ Get the time zone offset, in seconds.
-
propertime.utilities.correct_dt_dst(dt)¶ Correct the DST of a datetime object, by re-creating it.
-
propertime.utilities.as_tz(dt, tz)¶ Get a datetime object as if it was on the given time zone.
- Parameters:
dt (datetime) – the datetime object.
tz (tzinfo,pytz,str) – the time zone.
-
propertime.utilities.dt_from_s(s, tz='UTC')¶ Create a datetime object from epoch seconds. If no time zone is given, UTC is assumed.
-
propertime.utilities.s_from_dt(dt, tz=None)¶ Return the epoch seconds from a datetime object, with floating point for milliseconds/microseconds.
-
propertime.utilities.dt_from_str(string, tz=None)¶ Create a datetime object from a string.
This is a basic IS0 8601, see https://www.w3.org/TR/NOTE-datetime
- Supported formats on UTC:
YYYY-MM-DDThh:mm:ssZ
YYYY-MM-DDThh:mm:ss.{u}Z
- Supported formats with offset
YYYY-MM-DDThh:mm:ss+ZZ:ZZ
YYYY-MM-DDThh:mm:ss.{u}+ZZ:ZZ
- Other supported formats:
YYYY-MM-DDThh:mm:ss (without the trailing Z, treated as naive)
-
propertime.utilities.str_from_dt(dt)¶ Return the a string representation of a datetime object (as IS08601).
-
propertime.utilities.is_numerical(item)¶ Check if the argument is numerical.
The library logger
-
propertime.logger.setup(level='CRITICAL', force=False)¶ Setup the library logger at the given level. Checks also if such logger is already inizialized and if there are any inconsistencies between the legger levels, and force the new level if required to.
Common exceptions
-
exception
propertime.exceptions.ConsistencyError¶ Bases:
ExceptionRaised when there is an internal consistency error.