Skip to content
Snippets Groups Projects
Commit acef391a authored by [ACC] Elena Operation's avatar [ACC] Elena Operation
Browse files

changed format of documentation

parent a02ba74c
Branches add_time_handling
Tags v0.0.4
No related merge requests found
Pipeline #3615207 passed
...@@ -5,14 +5,16 @@ import pytz ...@@ -5,14 +5,16 @@ import pytz
import datetime import datetime
def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'): 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
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. Note, a timezone called "CERN" (default) can be used as timezone of Geneva area.
:param unixtime_ns: ns from epoch Args:
:param time_zone: desired timezone for the output (default='CERN') unixtime_ns (int): ns from epoch
:return: time_zone (string): desired timezone for the output (default='CERN')
Returns:
(datetime): datetime object representing the given timestamp
''' '''
if isinstance(time_zone, str): if isinstance(time_zone, str):
if time_zone.upper() == 'UTC': if time_zone.upper() == 'UTC':
...@@ -30,13 +32,15 @@ def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'): ...@@ -30,13 +32,15 @@ def unixtime_to_datetime(unixtime_ns, time_zone = 'CERN'):
return datetime.datetime.fromtimestamp(unixtime_ns / 1e9, tz=time_zone) return datetime.datetime.fromtimestamp(unixtime_ns / 1e9, tz=time_zone)
def datetime_to_unixtime(date_time): def datetime_to_unixtime(date_time):
''' '''Converts a datetime object into a unixtimestamp in ns
Converts a datetime object into a unixtimestamp in ns
Note that datetime objects are precise only down to the microsecond. Note that datetime objects are precise only down to the microsecond.
:param date_time: datetime object Args:
:return: ns from epoch UTC date_time (datetime): datetime object to convert
Returns:
(int): ns from epoch UTC
''' '''
if date_time.tzinfo is None: if date_time.tzinfo is None:
epoch = datetime.datetime.fromtimestamp(0, tz=None) epoch = datetime.datetime.fromtimestamp(0, tz=None)
...@@ -45,36 +49,43 @@ def datetime_to_unixtime(date_time): ...@@ -45,36 +49,43 @@ def datetime_to_unixtime(date_time):
return int((date_time-epoch).total_seconds()*1e6)*1000 return int((date_time-epoch).total_seconds()*1e6)*1000
def string_to_datetime(date_string): def string_to_datetime(date_string):
''' '''Uses standard datetime function to parse a string in the ISO format
Uses standard datetime function to parse a string in the ISO format
example: Examples:
In: string_to_datetime('2022-01-21 15:55:34.123555') >>> string_to_datetime('2022-01-21 15:55:34.123555')
Out: datetime.datetime(2022, 1, 21, 15, 55, 34, 123555, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET')) datetime.datetime(2022, 1, 21, 15, 55, 34, 123555, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'CET'))
:param date_string: Args:
:return: date_string (string): string to parse
Returns:
(datetime): datetime object with timezone representing the input string date and time
''' '''
return datetime.datetime.fromisoformat(date_string).astimezone() return datetime.datetime.fromisoformat(date_string).astimezone()
def string_to_unixtime(date_string): def string_to_unixtime(date_string):
''' '''Simple parse of ISO string and get a unix timestamp
simple parse of ISO string
:param date_string: date+time in ISO string format Args:
:return: unix timestamp in ns date_string (string): date+time in ISO string format
Returns:
(int): unix timestamp in ns
''' '''
return datetime_to_unixtime(string_to_datetime(date_string)) return datetime_to_unixtime(string_to_datetime(date_string))
def unixtime_to_string(unixtime_ns, time_zone = 'utc', format = '%Y-%m-%d %H:%M:%S.%f'): def unixtime_to_string(unixtime_ns, time_zone = 'CERN', format = '%Y-%m-%d %H:%M:%S.%f'):
''' '''
Converts a unix timestamp in ns into a string on a given timezone and format Converts a unix timestamp in ns into a string on a given timezone and format
:param unixtime_ns: Args:
:param time_zone: unixtime_ns (int): timestamp in ns to convert
:param format: time_zone (string): timezone to use for the conversion (default='CERN')
:return: string format (string): (default='%Y-%m-%d %H:%M:%S.%f')
Returns:
(string): string representation of the input timestamp
''' '''
return unixtime_to_datetime(unixtime_ns, time_zone=time_zone).strftime(format) return unixtime_to_datetime(unixtime_ns, time_zone=time_zone).strftime(format)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment