diff --git a/datascout/_timescout.py b/datascout/_timescout.py index 1d0c2872cb5cc290c043d6025bb2247f12b300f8..ffecc5f3123d064f44b0635b36d373f9a52d3e26 100644 --- a/datascout/_timescout.py +++ b/datascout/_timescout.py @@ -4,7 +4,16 @@ Implementation of sweet functions to convert time data from one format to anothe import pytz import datetime -def unixtime_to_datetime(unixtime_ns, time_zone = 'utc'): +def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'): + ''' + Converts a timestamp in ns from epoch (UTC) to a datetime on the given time_zone + + Note, a timezone called "CERN" (default) can be used as timezone of Geneva area. + + :param unixtime_ns: ns from epoch + :param time_zone: desired timezone for the output (default='CERN') + :return: + ''' if isinstance(time_zone, str): if time_zone.upper() == 'UTC': time_zone = pytz.utc diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 11689a76aef529261deab480d1dc076abc42f264..ad4f8e1b6b2042b985433ffedd76489017bfee3f 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -3,7 +3,7 @@ Usage ===== -Datascout provides a set of self-explained functions for data conversion, saving, loading. +Datascout provides a set of self-explained functions for data conversion, saving, loading, as well as timestamp conversion. A simple example of use could be the following: @@ -64,3 +64,27 @@ The loss of precision is due to the JSON data format, and not to data conversion # This will return no output if the two dictionaries contain the same data ds._compare_data(my_dict, my_dict_from_pandas) +It also provides some sweet functions to deal with timestamps (typically in ns from epoch) + +.. testcode:: + + import datascout as ds + import datetime + + reference_stamp = 1645617457640238425 + reference_time = datetime.datetime(2022, 2, 23, 12, 57, 37, 640238) + + myDateTime_utc = ds.unixtime_to_datetime(reference_stamp, time_zone = 'utc') + myDateTime_local = ds.unixtime_to_datetime(reference_stamp, time_zone = 'local') + myDateTime_cern = ds.unixtime_to_datetime(reference_stamp, time_zone = 'CERN') + myDateTime_unaware = ds.unixtime_to_datetime(reference_stamp, time_zone = None) + + reference_time_str = ds.unixtime_to_string(reference_stamp, time_zone = 'CERN') + print(reference_time_str) + +.. parsed-literal:: + + 2022-02-23 12:57:37.640238 + + +