# -*- coding: utf-8 -*- """ @file timestamp.py @brief Extends the python datetime with functionality useful to the format recognized by ASF @par History - Brian Helgans, Dec 17 2019, initial version """ import datetime as pydatetime import re class datetime(pydatetime.datetime): ''' Extends datetime with fw patterned string output/input ''' def __add__(self,other): result=super(datetime,self).__add__(other) result=datetime(result.year, result.month, result.day, result.hour, result.minute, result.second, result.microsecond, result.tzinfo) return result __radd__=__add__ @property def as_fw_string(self): return as_fw_string(self) @staticmethod def from_fw_string(fw_string): ts_re = re.compile('^{year=(\d{4});month=(\d{1,2});day=(\d{1,2});hour=(\d{1,2});min=(\d{1,2});sec=(\d{1,2}.*[0-9]*)}') #print(fw_string) m = ts_re.match(fw_string).groups() seconds_f = float(m[5]) seconds = int(seconds_f) micros = int ( (seconds_f - seconds ) * 1000000 ) start_datetime = datetime(year=int(m[0]), month=int(m[1]), day=int(m[2]), hour=int(m[3]), minute=int(m[4]), second=seconds, microsecond=micros) return start_datetime def as_fw_string(datetime): ''' Outputs a datetime as a fw format string. :param datetime: :type datetime.datetime: ''' return '{year=%d;month=%d;day=%d;hour=%d;min=%d;sec=%f}' % ( datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second + int(datetime.microsecond / 100000) / 10.0)