def test_as_datetime(self): datetime = DateTime() datetime.year = 123 datetime.month = 2 datetime.day = 1 datetime.hour = 10 datetime.minute = 20 datetime.sec = 10 datetime.microsec = 100 value = ttypes.Value(dtVal=datetime) value_wrapper = ValueWrapper(value) assert value_wrapper.is_datetime() datetime_val = value_wrapper.as_datetime() datetime_val.set_timezone_offset(28800) assert isinstance(datetime_val, DateTimeWrapper) assert datetime_val.get_hour() == 10 assert datetime_val.get_minute() == 20 assert datetime_val.get_sec() == 10 assert datetime_val.get_microsec() == 100 assert 'utc datetime: 123-02-01T10:20:10.000100, timezone_offset: 28800' == str( datetime_val) assert '123-02-01T18:20:10.000100' == datetime_val.get_local_datetime_str( ) new_datetime = copy.deepcopy(datetime) new_datetime.hour = 18 assert new_datetime == datetime_val.get_local_datetime() new_datetime_2 = copy.deepcopy(datetime) new_datetime_2.hour = 12 assert new_datetime_2 == datetime_val.get_local_datetime_by_timezone_offset( 7200)
def date_time_convert_with_timezone(date_time: DateTime, timezone_offset: int): """the function to convert utc date_time to local date_time :param date_time: the utc date_time :param timezone_offset: the timezone offset :return: the date_time with timezone """ native_date_time = datetime(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.sec, date_time.microsec, pytz.timezone("utc")) local_date_time = native_date_time.astimezone(timezone(timedelta(seconds=timezone_offset))) new_date_time = DateTime() new_date_time.year = local_date_time.year new_date_time.month = local_date_time.month new_date_time.day = local_date_time.day new_date_time.hour = local_date_time.hour new_date_time.minute = local_date_time.minute new_date_time.sec = local_date_time.second new_date_time.microsec = local_date_time.microsecond return new_date_time
def date_time_convert_with_timezone(date_time: DateTime, timezone_offset: int): native_date_time = datetime(date_time.year, date_time.month, date_time.day, date_time.hour, date_time.minute, date_time.sec, date_time.microsec, pytz.timezone("utc")) local_date_time = native_date_time.astimezone( timezone(timedelta(seconds=timezone_offset))) new_date_time = DateTime() new_date_time.year = local_date_time.year new_date_time.month = local_date_time.month new_date_time.day = local_date_time.day new_date_time.hour = local_date_time.hour new_date_time.minute = local_date_time.minute new_date_time.sec = local_date_time.second new_date_time.microsec = local_date_time.microsecond return new_date_time
def test_as_datetime(self): datetime = DateTime() datetime.year = 123 datetime.month = 2 datetime.day = 1 datetime.hour = 10 datetime.minute = 20 datetime.sec = 10 datetime.microsec = 100 value = ttypes.Value(dtVal=datetime) value_wrapper = ValueWrapper(value) assert value_wrapper.is_datetime() datetime_val = value_wrapper.as_datetime() assert isinstance(datetime_val, DateTimeWrapper) assert datetime_val.get_hour() == 10 assert datetime_val.get_minute() == 20 assert datetime_val.get_sec() == 10 assert datetime_val.get_microsec() == 100 assert '123-02-01T10:20:10.000100' == str(datetime_val)