A Python package for easy conversion and handling of time values.
Project description
spTimeHelperPy
This package provides the Python module and TimeHelper class for easy conversion and handling of time values.
Python’s datetime and timestamp objects come with various methods to deal with time values. However, often additional functionality is needed, especially when dealing with databases, where time values are stored as nanoseconds since epoch (QuestDB , InfluxDB) or as string representations of a datetime (MariaDB, MySQL). Furthermore, time values need to be converted between the UTC values stored in a database and the timezone values obtained or displayed in an application.
The module’s TimeHelper class contains methods to convert between
- a datetime object with a UTC value (abbreviated dtUTC) or a target timezone value (dtTZ)
- a string representation of a datetime with a UTC value (strUTC) or a target timezone value (strTZ)
- a timestamp value (ts),
- nanoseconds since epoch (nsse)
Conversion methods are named with a uniform nomenclature using these value types, e.g. get_dtUTC_from_nsse() or get_nsse_from_dtTZ().
To simplify conversion to and from strings, a TimeHelper object can be initiated with an optional time format string, which is used as a default format. Alternatively, a time format string can be given as an optional argument for each conversion method.
Especially for time scale databases and having data stored in a text (or CSV) file, TimeHelper’s get_nsse_from_text() method allows for the text input being a UTC datetime string or a string representation of an integer (nanoseconds). This is helpful, as import routines can digest either value type from the text file.
Additional methods cover getting the target timezone’s ZoneInfo object, its name (e.g. ‘Europe/Berlin’) or a tuple with its abbreviations, e.g. ('CET', 'CEST').
For more details, see the API reference below and the demonstration code in the four files of the examples folder.
Enjoy
krokoreit
Installation
pip install spTimeHelperPy
Usage & API
TimeHelper Class
Import module:
from spTimeHelperPy import TimeHelper
TimeHelper objects use a target timezone (target_tz) for converting time values between UTC and target_tz. Additionally you can provide a time format string, e.g. '%Y-%m-%d %H:%M:%S.%f', which will be used as a default format by various methods.
As in most programs such information will be user specific and defined in a configuration file, TimeHelper uses ConfigParser to extract the values from a file, which contains entries like this:
[TimeHelper]
# target time zone and format for converting time strings
target_tz = Europe/Berlin
# mask % in format with a leading %, i.e. %Y -> %%Y
time_format = %%Y-%%m-%%d %%H:%%M:%%S.%%fZ
When using a configuration file, a TimeHelper object is created with the name of the configuration file and an optional section name (defaults to 'TimeHelper'):
th = TimeHelper('config.ini')
th = TimeHelper('config.ini', 'My TimeHelper Setting')
Alternatively, a TimeHelper object can also be created by directly providing target_tz and optionally time_string_format as keyword arguments:
th = TimeHelper(target_tz="Europe/Paris")
th = TimeHelper(target_tz="Europe/Paris", time_string_format='%Y-%m-%dT%H:%M:%S.%fZ')
API
Method Parameters
TimeHelper object's methods are used like
time_string_format = "%Y-%m-%d %H:%M:%S.%f %Z"
strUTC = "1970-01-01 04:00:00.000000 UTC"
dt = th.get_dtTZ_from_strUTC(strUTC, time_string_format)
All methods and their arguments are uniformly named with the following definitions
- dtTZ
datetime object for the target timezone. The target timezone was set when creating the TimeHelper object and datetime objects used as arguments will be treated as belonging to this timezone. - dtUTC
datetime object for UTC. - strTZ
a string representation of a datetime for a target timezone value - strUTC
a string representation of a datetime for a UTC value - time_string_format, time_string_format_TZ or time_string_format_UTC
a string with the format applied for converting datetime objects to strings and vice versa. Note that when the format string includes the '%Z' specifier for a timezone's abbreviation and it is used for parsing strUTC or strTZ values, then only 'UTC' or the abbreviations for the target timezone can be converted. All other 'unknown' abbreviations will cause an exception. - nsse
an integer value for nanoseconds since epoch - ts
a float value for a timestamp value (which represents full seconds and fractions thereof since epoch) - second_hour
an optional argument for conversion of strTZ strings. When switching from daylight saving time back to normal time in autumn, the times for one hour are occurring twice. For such ambiguous cases use second_hour to specify that strTZ is for the second occurrence.
Methods
- The datetime Methods
- The Nanoseconds since Epoch Methods
- The datetime string Methods
- The timestamp Methods
- The ZoneInfo Methods
The datetime Methods
get_dtTZ() Method
get_dtTZ()
Returns the target timezone datetime, aka now.
get_dtTZ_from_dtUTC() Method
get_dtTZ_from_dtUTC(dtUTC)
Returns a target timezone datetime from a UTC datetime.
get_dtTZ_from_nsse() Method
get_dtTZ_from_nsse(nsse)
Returns a target timezone datetime from nanoseconds since epoch.
get_dtTZ_from_strTZ() Method
get_dtTZ_from_strTZ(strTZ, time_string_format=None, second_hour=False)
Returns a target timezone datetime from a target timezone datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used. For an ambiguous case when switching back to normal time in autumn (times for one hour occurring twice), use True for second_hour to convert into datetime for the second occurrence.
get_dtTZ_from_strUTC() Method
get_dtTZ_from_strUTC(strUTC, time_string_format=None)
Returns a target timezone datetime from a UTC datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_dtTZ_from_ts() Method
get_dtTZ_from_ts(ts)
Returns a target timezone datetime from a timestamp.
get_dtUTC() Method
get_dtUTC()
Returns the current UTC datetime, aka now.
get_dtUTC_from_dtTZ() Method
get_dtUTC_from_dtTZ(dtTZ)
Returns a UTC datetime from an target timezone datetime.
get_dtUTC_from_nsse() Method
get_dtUTC_from_nsse(nsse)
Returns a UTC datetime from nanoseconds since epoch.
get_dtUTC_from_strTZ() Method
get_dtUTC_from_strTZ(strTZ, time_string_format=None, second_hour=False)
Returns a UTC datetime from a target timezone datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used. For an ambiguous case when switching back to normal time in autumn (times for one hour occurring twice), use True for second_hour to convert into datetime for the second occurrence.
get_dtUTC_from_strUTC() Method
get_dtUTC_from_strUTC(strUTC, time_string_format=None)
Returns a UTC datetime from a UTC datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_dtUTC_from_ts() Method
get_dtUTC_from_ts(ts)
Returns a UTC datetime from a timestamp.
The Nanoseconds since Epoch Methods
get_nsse() Method
get_nsse()
Returns the current nanoseconds since epoch.
get_nsse_from_dtTZ() Method
get_nsse_from_dtTZ(dtTZ)
Returns nanoseconds since epoch from an target timezone datetime.
get_nsse_from_dtUTC() Method
get_nsse_from_dtUTC(dtUTC)
Returns nanoseconds since epoch from a UTC datetime.
get_nsse_from_strTZ() Method
get_nsse_from_strTZ(strTZ, time_string_format=None, second_hour=False)
Returns nanoseconds since epoch from an timezone datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used. For an ambiguous case when switching back to normal time in autumn (times for one hour occurring twice), use True for second_hour to convert into datetime for the second occurrence.
get_nsse_from_strUTC() Method
get_nsse_from_strUTC(strUTC, time_string_format=None)
Returns nanoseconds since epoch from a UTC datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_nsse_from_text() Method
get_nsse_from_text(from_text, time_string_format=None)
Returns nanoseconds since epoch from a text given as either a UTC datetime string or a string representation of a nanoseconds integer value. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_nsse_from_ts() Method
get_nsse_from_ts(ts)
Returns nanoseconds since epoch from a timestamp.
The datetime string Methods
get_strTZ_from_dtTZ() Method
get_strTZ_from_dtTZ(dtTZ, time_string_format=None)
Returns a target timezone datetime string from a target timezone datetime. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strTZ_from_dtUTC() Method
get_strTZ_from_dtUTC(dtUTC, time_string_format=None)
Returns a target timezone datetime string from a UTC datetime. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strTZ_from_nsse() Method
get_strTZ_from_nsse(nsse, time_string_format=None)
Returns a target timezone datetime string from nanoseconds since epoch. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strTZ_from_strUTC() Method
get_strTZ_from_strUTC(strUTC, time_string_format_UTC=None, time_string_format_TZ=None)
Returns a target timezone datetime string from a UTC datetime string. Use the optional time_string_format_UTC and time_string_format_TZ arguments to specify a format for converting the datetime strings. If omitted or None, the default format of the TimeHelper object is used.
get_strTZ_from_ts() Method
get_strTZ_from_ts(ts, time_string_format=None)
Returns a target timezone datetime string from a timestamp. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strUTC_from_dtTZ() Method
get_strUTC_from_dtTZ(dtTZ, time_string_format=None)
Returns a UTC datetime string from a target timezone datetime. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strUTC_from_dtUTC() Method
get_strUTC_from_dtUTC(dtUTC, time_string_format=None)
Returns a UTC datetime string from a UTC datetime. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strUTC_from_nsse() Method
get_strUTC_from_nsse(nsse, time_string_format=None)
Returns a UTC datetime string from nanoseconds since epoch. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
get_strUTC_from_strTZ() Method
get_strUTC_from_strTZ(strTZ, time_string_format_TZ=None, time_string_format_UTC=None, second_hour=False)
Returns a UTC timezone datetime string from a target timezone datetime string. Use the optional time_string_format_UTC and time_string_format_TZ arguments to specify a format for converting the datetime strings. If omitted or None, the default format of the TimeHelper object is used. For an ambiguous case when switching back to normal time in autumn (times for one hour occurring twice), use True for second_hour to convert into datetime for the second occurrence.
get_strUTC_from_ts() Method
get_strUTC_from_ts(ts, time_string_format=None)
Returns a UTC datetime string from a timestamp. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
The timestamp Methods
get_ts_from_dtTZ() Method
get_ts_from_dtTZ(dtTZ)
Returns a timestamp from a target timezone datetime.
get_ts_from_dtUTC() Method
get_ts_from_dtUTC(dtUTC)
Returns a timestamp from a UTC datetime.
get_ts_from_nsse() Method
get_ts_from_nsse(nsse)
Returns a timestamp from nanoseconds since epoch.
get_ts_from_strTZ() Method
get_ts_from_strTZ(strTZ, time_string_format=None, second_hour=False)
Returns a timestamp from a target timezone datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used. For an ambiguous case when switching back to normal time in autumn (times for one hour occurring twice), use True for second_hour to convert into datetime for the second occurrence.
get_ts_from_strUTC() Method
get_ts_from_strUTC(strUTC, time_string_format=None)
Returns a timestamp from a UTC datetime string. Use the optional time_string_format argument to specify a format for converting the datetime string. If omitted or None, the default format of the TimeHelper object is used.
The ZoneInfo Methods
get_tz_zoneinfo() Method
get_tz_zoneinfo()
Returns the ZoneInfo object for the target timezone.
get_tz_zone_name() Method
get_tz_zone_name()
Returns the name of the target timezone, e.g. ‘Europe/Berlin’.
get_tz_zone_abbreviations() Method
get_tz_zone_abbreviations()
Returns a tuple with abbreviations for the normal and daylight saving periods of the target timezone, e.g. ('CET', 'CEST').
get_utc_zoneinfo() Method
get_utc_zoneinfo()
Returns the ZoneInfo object for UTC.
License
MIT license
Copyright © 2025 by krokoreit
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sptimehelperpy-1.0.4.tar.gz.
File metadata
- Download URL: sptimehelperpy-1.0.4.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1647fc4b88082d1f530cab1483a6ea0928dbfaa41482641757a0525f11e551ea
|
|
| MD5 |
ccdc86a4ddec40efe994a5e24aaa1e8f
|
|
| BLAKE2b-256 |
f47944b7db61105fdc514356d14cf9d46323073ac7fb6bef336ea26caf8f635e
|
File details
Details for the file sptimehelperpy-1.0.4-py3-none-any.whl.
File metadata
- Download URL: sptimehelperpy-1.0.4-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac379f3e79989f87af55ee49bd322c0991ef2fbb673d9da0bf0f1b86b5fe0d11
|
|
| MD5 |
20e55b2a750fbb658b2133402409ce84
|
|
| BLAKE2b-256 |
68f5d3bc3db1bba3fd05d735f100b0a08bdf33d84af2bee4e452956bbf085b9f
|