Time conversion functions
tintervals provides conversion function from different time formats.
Supported formats are:
Format |
Description |
Example |
|---|---|---|
Epoch (Unix) |
Seconds from the Unix epoch |
|
MJD |
Modified Julian Date |
|
ISO |
ISO format |
|
K+K |
Format used by K+K counters |
|
(either with |
||
datetime |
Python datetime object |
If naive, ISO and datetime inputs are considered as system/local time. K+K format is always naive and interpreted as sytem/local time. ISO format can be read with microseconds but it is printed without. Conversion from Epoch time (Unix) and MJD is done by simple affine function, as both are aligned with UTC and basically ignore leap seconds.
Functions with a 2 in their name are optimized for speed and are meant to be used as converters, e.g.:
np.genfromtxt('somedata.dat', converters={0: iso2epoch}).
Function with a from in their name are numpy universal (vectorized) functions that work either on single values or on numpy arrays.
For a more general handling of time formats you should consider astropy.time.
Functions:
|
Convert a dateime string from K+K to ISO format. |
|
Convert a datetime string in ISO format to a datime object. |
|
Convert a datetime object in a datetime string in ISO format. |
Convert a datetime object to seconds from the epoch. |
|
Convert seconds from the epoch to a datetime object. |
|
|
Convert a datetime string in K+K format to seconds from the epoch. |
|
Convert a datetime string in ISO format to seconds from the epoch. |
|
Convert seconds from the epoch to a datetime string in ISO format. |
|
Convert seconds from the epoch to MJD. |
|
Convert MJD to seconds from the epoch. |
|
Convert MJD to a datetime string in ISO format. |
|
Convert a datetime string in ISO format to MJD. |
|
Convert a datetime object to seconds from the epoch. |
|
Convert seconds from the epoch to a datetime object. |
|
Convert a datetime string in K+K format to MJD. |
Same as epoch2mjd (already vectorized). |
|
|
Same as mjd2epoch (already vectorized). |
|
Vectorized version of epoch2iso. |
|
Vectorized version of mjd2iso. |
- tintervals.conversion.kk2iso(s, year_digits='20')
Convert a dateime string from K+K to ISO format.
- Parameters:
s (str) – datetime in K+K format.
year_digits (str, optional) – digits of year to be prepended to K+K format, by default ‘20’.
- Returns:
datetime in ISO format.
- Return type:
str
- tintervals.conversion.iso2datetime(s, tzinfo=None)
Convert a datetime string in ISO format to a datime object.
- Parameters:
s (str) – datetime in ISO format.
tzinfo (timezone object, optional) – if given, interprets the datetime as naive and replace the tzinfo of the result, by default None.
- Returns:
datetime object.
- Return type:
datetime
- tintervals.conversion.datetime2iso(d)
Convert a datetime object in a datetime string in ISO format.
- Parameters:
d (datetime) – datetime object
- Returns:
datetime in ISO format
- Return type:
str
Notes
Result is rounded to the nearest integer second. The ouput uses ‘Z’ for UTC datetimes.
- tintervals.conversion.datetime2epoch(d)
Convert a datetime object to seconds from the epoch.
- Parameters:
d (datetime) – datetime object.
- Returns:
seconds from the epoch.
- Return type:
float
- tintervals.conversion.epoch2datetime(t)
Convert seconds from the epoch to a datetime object.
- Parameters:
t (float) – seconds from the epoch.
- Returns:
datetime object.
- Return type:
datetime
- tintervals.conversion.kk2epoch(s, year_digits='20')
Convert a datetime string in K+K format to seconds from the epoch.
- Parameters:
s (str) – datetime in K+K format.
year_digits (str, optional) – digits of year to be prepended to K+K format, by default ‘20’
- Returns:
seconds from the epoch.
- Return type:
float
- tintervals.conversion.iso2epoch(s)
Convert a datetime string in ISO format to seconds from the epoch.
- Parameters:
s (str) – datetime in ISO format.
- Returns:
seconds from the epoch.
- Return type:
float
- tintervals.conversion.epoch2iso(t)
Convert seconds from the epoch to a datetime string in ISO format.
- Parameters:
t (float) – seconds from the epoch.
- Returns:
datetime in ISO format.
- Return type:
str
- tintervals.conversion.epoch2mjd(t)
Convert seconds from the epoch to MJD.
- Parameters:
t (float or ndarray) – seconds from the epoch.
- Returns:
MJD.
- Return type:
float or ndarray
- tintervals.conversion.mjd2epoch(d)
Convert MJD to seconds from the epoch.
- Parameters:
d (float or ndarray) – MJD.
- Returns:
seconds from the epoch.
- Return type:
float or ndarray
- tintervals.conversion.mjd2iso(mjd)
Convert MJD to a datetime string in ISO format.
- Parameters:
mjd (float) – MJD.
- Returns:
datetime in ISO format.
- Return type:
str
- tintervals.conversion.iso2mjd(s)
Convert a datetime string in ISO format to MJD.
- Parameters:
s (str) – datetime in ISO format.
- Returns:
MJD.
- Return type:
float
- tintervals.conversion.datetime2mjd(d)
Convert a datetime object to seconds from the epoch.
- Parameters:
d (datetime) – datetime object.
- Returns:
MJD.
- Return type:
float
- tintervals.conversion.mjd2datetime(d)
Convert seconds from the epoch to a datetime object.
- Parameters:
d (float) – MJD.
- Returns:
datetime object.
- Return type:
datetime
- tintervals.conversion.kk2mjd(s, year_digits='20')
Convert a datetime string in K+K format to MJD.
- Parameters:
s (str) – datetime in K+K format.
year_digits (str, optional) – digits of year to be prepended to K+K format, by default ‘20’
- Returns:
MJD.
- Return type:
float
- tintervals.conversion.mjd_from_epoch(t)
Same as epoch2mjd (already vectorized).
- tintervals.conversion.epoch_from_mjd(mjd)
Same as mjd2epoch (already vectorized).
- tintervals.conversion.iso_from_epoch(*args, **kwargs)
- Vectorized version of epoch2iso.
Convert seconds from the epoch to a datetime string in ISO format.
- Parameters:
t (float or ndarray) – seconds from the epoch.
- Returns:
datetime in ISO format.
- Return type:
str or ndarray
- tintervals.conversion.iso_from_mjd(*args, **kwargs)
- Vectorized version of mjd2iso.
Convert MJD to a datetime string in ISO format.
- Parameters:
mjd (float or ndarray) – MJD.
- Returns:
datetime in ISO format.
- Return type:
str or ndarray