From 27bc4ebad97c59e6bab103baf3f72a283985ccda Mon Sep 17 00:00:00 2001 From: Joachim Metz Date: Fri, 15 May 2026 15:44:11 +0200 Subject: [PATCH] Added method to serialize DateTimeValues --- dfdatetime/apfs_time.py | 4 +- dfdatetime/cocoa_time.py | 21 ++++-- dfdatetime/delphi_date_time.py | 17 +++-- dfdatetime/dotnet_datetime.py | 13 +++- dfdatetime/fake_time.py | 19 ++++-- dfdatetime/fat_date_time.py | 28 ++++++-- dfdatetime/filetime.py | 13 +++- dfdatetime/golang_time.py | 26 ++++--- dfdatetime/hfs_time.py | 14 +++- dfdatetime/interface.py | 29 +++++++- dfdatetime/ole_automation_date.py | 13 +++- dfdatetime/posix_time.py | 74 ++++++++++++-------- dfdatetime/rfc2579_date_time.py | 30 ++++++++ dfdatetime/semantic_time.py | 12 ++++ dfdatetime/serializer.py | 104 +--------------------------- dfdatetime/systemtime.py | 22 +++++- dfdatetime/time_elements.py | 108 +++++++++++++++++++---------- dfdatetime/uuid_time.py | 13 +++- dfdatetime/webkit_time.py | 20 ++++-- tests/cocoa_time.py | 13 +++- tests/delphi_date_time.py | 21 +++--- tests/dotnet_datetime.py | 13 +++- tests/factory.py | 11 +++ tests/fake_time.py | 13 ++++ tests/fat_date_time.py | 24 +++++++ tests/filetime.py | 13 +++- tests/golang_time.py | 15 ++++ tests/hfs_time.py | 12 ++++ tests/ole_automation_date.py | 20 ++++-- tests/posix_time.py | 103 +++++++++++++++++++++------- tests/rfc2579_date_time.py | 20 ++++-- tests/semantic_time.py | 36 ++++++++++ tests/systemtime.py | 20 ++++-- tests/time_elements.py | 110 ++++++++++++++---------------- tests/uuid_time.py | 14 +++- tests/webkit_time.py | 12 ++++ 36 files changed, 726 insertions(+), 324 deletions(-) diff --git a/dfdatetime/apfs_time.py b/dfdatetime/apfs_time.py index ea1f5fa..fb986e5 100644 --- a/dfdatetime/apfs_time.py +++ b/dfdatetime/apfs_time.py @@ -57,7 +57,7 @@ def CopyFromDateTimeString(self, time_string): Raises: ValueError: if the date and time value is not supported. """ - super()._CopyFromDateTimeString(time_string) + super().CopyFromDateTimeString(time_string) if ( self._timestamp is None @@ -80,7 +80,7 @@ def CopyToDateTimeString(self): ): return None - return super()._CopyToDateTimeString() + return super().CopyToDateTimeString() factory.Factory.RegisterDateTimeValues(APFSTime) diff --git a/dfdatetime/cocoa_time.py b/dfdatetime/cocoa_time.py index fa1b2b3..418a8ff 100644 --- a/dfdatetime/cocoa_time.py +++ b/dfdatetime/cocoa_time.py @@ -56,9 +56,10 @@ def _GetNormalizedTimestamp(self): """Retrieves the normalized timestamp. Returns: - float: normalized timestamp, which contains the number of seconds since - January 1, 1970 00:00:00 and a fraction of second used for increased - precision, or None if the normalized timestamp cannot be determined. + decimal.Decimal: normalized timestamp, which contains the number of + seconds since January 1, 1970 00:00:00 and a fraction of second used + for increased precision, or None if the normalized timestamp cannot be + determined. """ if self._normalized_timestamp is None: if self._timestamp is not None: @@ -122,11 +123,9 @@ def CopyToDateTimeString(self): number_of_days, hours, minutes, seconds = self._GetTimeValues( int(self._timestamp) ) - year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - microseconds = int((self._timestamp % 1) * definitions.MICROSECONDS_PER_SECOND) return ( @@ -134,5 +133,17 @@ def CopyToDateTimeString(self): f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(CocoaTime) diff --git a/dfdatetime/delphi_date_time.py b/dfdatetime/delphi_date_time.py index 8771416..7065026 100644 --- a/dfdatetime/delphi_date_time.py +++ b/dfdatetime/delphi_date_time.py @@ -108,7 +108,6 @@ def CopyFromDateTimeString(self, time_string): timestamp = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds ) - timestamp = float(timestamp) / definitions.SECONDS_PER_DAY timestamp += self._DELPHI_TO_POSIX_BASE timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_DAY @@ -132,26 +131,36 @@ def CopyToDateTimeString(self): number_of_days, hours, minutes, seconds = self._GetTimeValues( int(number_of_seconds) ) - # The maximum date supported by TDateTime values is limited to: # 9999-12-31 23:59:59.999 (approximate 2958465 days since epoch). # The minimum date is unknown hence assuming it is limited to: # 0001-01-01 00:00:00.000 (approximate -693593 days since epoch). + if number_of_days < -693593 or number_of_days > 2958465: return None year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - microseconds = int( (number_of_seconds % 1) * definitions.MICROSECONDS_PER_SECOND ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(DelphiDateTime) diff --git a/dfdatetime/dotnet_datetime.py b/dfdatetime/dotnet_datetime.py index 4979c49..df2ef16 100644 --- a/dfdatetime/dotnet_datetime.py +++ b/dfdatetime/dotnet_datetime.py @@ -136,11 +136,22 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{fraction_of_second:07d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(DotNetDateTime) diff --git a/dfdatetime/fake_time.py b/dfdatetime/fake_time.py index 3965863..a0b013c 100644 --- a/dfdatetime/fake_time.py +++ b/dfdatetime/fake_time.py @@ -90,7 +90,6 @@ def CopyFromDateTimeString(self, time_string): self._number_of_seconds = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds ) - if nanoseconds is None: self._microseconds = None else: @@ -112,17 +111,29 @@ def CopyToDateTimeString(self): number_of_days, hours, minutes, seconds = self._GetTimeValues( self._number_of_seconds ) - year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - date_time_string = ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}" ) - if self._microseconds is not None: date_time_string = ".".join([date_time_string, f"{self._microseconds:06d}"]) return date_time_string + + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + normalized_timestamp = self._GetNormalizedTimestamp() + + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = int( + normalized_timestamp * definitions.MICROSECONDS_PER_SECOND + ) + return serializable_dict diff --git a/dfdatetime/fat_date_time.py b/dfdatetime/fat_date_time.py index 62e4538..5afd46a 100644 --- a/dfdatetime/fat_date_time.py +++ b/dfdatetime/fat_date_time.py @@ -82,7 +82,6 @@ def _GetNormalizedTimestamp(self): decimal.Decimal(self._number_of_seconds) + self._FAT_DATE_TO_POSIX_BASE ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 @@ -181,16 +180,26 @@ def CopyToDateTimeString(self): number_of_days, hours, minutes, seconds = self._GetTimeValues( self._number_of_seconds ) - year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["fat_date_time"] = self._fat_date_time + + return serializable_dict + class FATTimestamp(interface.DateTimeValues): """FAT timestamp. @@ -306,12 +315,23 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:02d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(FATDateTime) factory.Factory.RegisterDateTimeValues(FATTimestamp) diff --git a/dfdatetime/filetime.py b/dfdatetime/filetime.py index ccbb72c..acb5bfc 100644 --- a/dfdatetime/filetime.py +++ b/dfdatetime/filetime.py @@ -143,11 +143,22 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{fraction_of_second:07d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(Filetime) diff --git a/dfdatetime/golang_time.py b/dfdatetime/golang_time.py index 05f04d1..60e57d1 100644 --- a/dfdatetime/golang_time.py +++ b/dfdatetime/golang_time.py @@ -82,7 +82,7 @@ def __init__(self, golang_timestamp=None, precision=None): @property def golang_timestamp(self): - """int: Golang time.Time timestamp or None if not set.""" + """bytes: Golang time.Time timestamp or None if not set.""" return self._golang_timestamp def _GetNormalizedTimestamp(self): @@ -101,11 +101,9 @@ def _GetNormalizedTimestamp(self): and self._nanoseconds is not None and self._nanoseconds >= 0 ): - self._normalized_timestamp = decimal.Decimal( self._number_of_seconds - GolangTime._GOLANG_TO_POSIX_BASE ) - if self._nanoseconds is not None and self._nanoseconds >= 0: self._normalized_timestamp += ( decimal.Decimal(self._nanoseconds) @@ -147,15 +145,12 @@ def _GetNumberOfSeconds(self, golang_timestamp): number_of_seconds, nanoseconds, time_zone_offset = struct.unpack( ">qih", golang_timestamp[1:15] ) - # TODO: add support for version 2 time zone offset in seconds except (TypeError, struct.error) as exception: raise ValueError( - ( - f"Unable to unpacked Golang time.Time timestamp with error: " - f"{exception!s}" - ) + f"Unable to unpacked Golang time.Time timestamp with error: " + f"{exception!s}" ) # A time zone offset of -1 minute is a special representation for UTC. @@ -195,7 +190,6 @@ def CopyFromDateTimeString(self, time_string): seconds = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds ) - seconds += self._GOLANG_TO_POSIX_BASE self._normalized_timestamp = None @@ -216,15 +210,25 @@ def CopyToDateTimeString(self): number_of_days, hours, minutes, seconds = self._GetTimeValues( self._number_of_seconds ) - year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{self._nanoseconds:09d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + return { + "__class_name__": type(self).__name__, + "__type__": "DateTimeValues", + "golang_timestamp": self._golang_timestamp, + } + factory.Factory.RegisterDateTimeValues(GolangTime) diff --git a/dfdatetime/hfs_time.py b/dfdatetime/hfs_time.py index db25ca6..e53fd47 100644 --- a/dfdatetime/hfs_time.py +++ b/dfdatetime/hfs_time.py @@ -70,7 +70,6 @@ def _GetNormalizedTimestamp(self): self._normalized_timestamp = ( decimal.Decimal(self._timestamp) - self._HFS_TO_POSIX_BASE ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 @@ -130,11 +129,22 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(HFSTime) diff --git a/dfdatetime/interface.py b/dfdatetime/interface.py index 97e7ee3..5fa6e83 100644 --- a/dfdatetime/interface.py +++ b/dfdatetime/interface.py @@ -108,7 +108,7 @@ def __init__(self, is_delta=False, precision=None, time_zone_offset=None): self._time_zone_offset = time_zone_offset self.is_local_time = False - self.time_zone_hint = False + self.time_zone_hint = None @property def is_delta(self): @@ -516,6 +516,25 @@ def _CopyTimeFromString(self, time_string): return hours, minutes, seconds, nanoseconds, time_zone_offset + def _CreateSerializableDict(self): + """Creates a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = { + "__class_name__": type(self).__name__, + "__type__": "DateTimeValues", + } + if self.is_local_time: + serializable_dict["is_local_time"] = True + if self._time_zone_offset: + serializable_dict["time_zone_offset"] = self._time_zone_offset + if self.time_zone_hint: + serializable_dict["time_zone_hint"] = self.time_zone_hint + + return serializable_dict + def _GetDateValues( self, number_of_days, epoch_year, epoch_month, epoch_day_of_month ): @@ -979,6 +998,14 @@ def CopyToDateTimeStringISO8601(self): return date_time_string + @abc.abstractmethod + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + def GetDate(self): """Retrieves the date represented by the date and time values. diff --git a/dfdatetime/ole_automation_date.py b/dfdatetime/ole_automation_date.py index b463397..291c93b 100644 --- a/dfdatetime/ole_automation_date.py +++ b/dfdatetime/ole_automation_date.py @@ -135,7 +135,6 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - microseconds = int((timestamp % 1) * definitions.MICROSECONDS_PER_SECOND) return ( @@ -143,5 +142,17 @@ def CopyToDateTimeString(self): f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(OLEAutomationDate) diff --git a/dfdatetime/posix_time.py b/dfdatetime/posix_time.py index 536c342..310a2a3 100644 --- a/dfdatetime/posix_time.py +++ b/dfdatetime/posix_time.py @@ -112,12 +112,23 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + class PosixTimeInMilliseconds(interface.DateTimeValues): """POSIX timestamp in milliseconds. @@ -166,7 +177,6 @@ def _GetNormalizedTimestamp(self): decimal.Decimal(self._timestamp) / definitions.MILLISECONDS_PER_SECOND ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 @@ -224,12 +234,23 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + class PosixTimeInMicroseconds(interface.DateTimeValues): """POSIX timestamp in microseconds. @@ -278,7 +299,6 @@ def _GetNormalizedTimestamp(self): decimal.Decimal(self._timestamp) / definitions.MICROSECONDS_PER_SECOND ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 @@ -336,12 +356,23 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + class PosixTimeInNanoseconds(interface.DateTimeValues): """POSIX timestamp in nanoseconds. @@ -390,13 +421,12 @@ def _GetNormalizedTimestamp(self): decimal.Decimal(self._timestamp) / definitions.NANOSECONDS_PER_SECOND ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 return self._normalized_timestamp - def _CopyFromDateTimeString(self, time_string): + def CopyFromDateTimeString(self, time_string): """Copies a POSIX timestamp from a date and time string. Args: @@ -429,21 +459,7 @@ def _CopyFromDateTimeString(self, time_string): self._timestamp = timestamp self._time_zone_offset = time_zone_offset - def CopyFromDateTimeString(self, time_string): - """Copies a POSIX timestamp from a date and time string. - - Args: - time_string (str): date and time value formatted as: - YYYY-MM-DD hh:mm:ss.######[+-]##:## - - Where # are numeric digits ranging from 0 to 9 and the seconds - fraction can be either 3, 6 or 9 digits. The time of day, seconds - fraction and time zone offset are optional. The default time zone - is UTC. - """ - self._CopyFromDateTimeString(time_string) - - def _CopyToDateTimeString(self): + def CopyToDateTimeString(self): """Copies the POSIX timestamp to a date and time string. Returns: @@ -461,20 +477,22 @@ def _CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{nanoseconds:09d}" ) - def CopyToDateTimeString(self): - """Copies the POSIX timestamp to a date and time string. + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. Returns: - str: date and time value formatted as: "YYYY-MM-DD hh:mm:ss.#########" or - None if the timestamp is missing or invalid. + dict[str, object]: serializable dictionary. """ - return self._CopyToDateTimeString() + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict factory.Factory.RegisterDateTimeValues(PosixTime) diff --git a/dfdatetime/rfc2579_date_time.py b/dfdatetime/rfc2579_date_time.py index 2af1535..d63ab2b 100644 --- a/dfdatetime/rfc2579_date_time.py +++ b/dfdatetime/rfc2579_date_time.py @@ -255,5 +255,35 @@ def CopyToDateTimeString(self): f".{self._deciseconds:01d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + time_zone_hours, time_zone_minutes = divmod(self._time_zone_offset, 60) + if self._time_zone_offset < 0: + time_zone_sign = "-" + time_zone_hours *= -1 + else: + time_zone_sign = "+" + + return { + "__class_name__": type(self).__name__, + "__type__": "DateTimeValues", + "rfc2579_date_time_tuple": ( + self._year, + self._month, + self._day_of_month, + self._hours, + self._minutes, + self._seconds, + self._deciseconds, + time_zone_sign, + time_zone_hours, + time_zone_minutes, + ), + } + factory.Factory.RegisterDateTimeValues(RFC2579DateTime) diff --git a/dfdatetime/semantic_time.py b/dfdatetime/semantic_time.py index 3f39e1d..5c4b498 100644 --- a/dfdatetime/semantic_time.py +++ b/dfdatetime/semantic_time.py @@ -182,6 +182,18 @@ def CopyToDateTimeStringISO8601(self): """ return None + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + return { + "__class_name__": type(self).__name__, + "__type__": "DateTimeValues", + "string": self._string, + } + def GetPlasoTimestamp(self): """Retrieves a timestamp that is compatible with plaso. diff --git a/dfdatetime/serializer.py b/dfdatetime/serializer.py index 8efd624..26f8d5f 100644 --- a/dfdatetime/serializer.py +++ b/dfdatetime/serializer.py @@ -84,109 +84,7 @@ def ConvertDateTimeValuesToJSON(cls, date_time_values): Returns: dict[str, object]: JSON serialized objects. """ - class_name = type(date_time_values).__name__ - - json_dict = {"__class_name__": class_name, "__type__": "DateTimeValues"} - - if hasattr(date_time_values, "timestamp"): - json_dict["timestamp"] = date_time_values.timestamp - - elif hasattr(date_time_values, "string"): - json_dict["string"] = date_time_values.string - - elif class_name == "FATDateTime": - json_dict["fat_date_time"] = date_time_values.fat_date_time - - elif class_name == "GolangTime": - json_dict["golang_timestamp"] = date_time_values.golang_timestamp - - elif class_name == "RFC2579DateTime": - time_zone_hours, time_zone_minutes = divmod( - date_time_values.time_zone_offset, 60 - ) - - if date_time_values.time_zone_offset < 0: - time_zone_sign = "-" - time_zone_hours *= -1 - else: - time_zone_sign = "+" - - json_dict["rfc2579_date_time_tuple"] = ( - date_time_values.year, - date_time_values.month, - date_time_values.day_of_month, - date_time_values.hours, - date_time_values.minutes, - date_time_values.seconds, - date_time_values.deciseconds, - time_zone_sign, - time_zone_hours, - time_zone_minutes, - ) - - elif class_name == "Systemtime": - json_dict["system_time_tuple"] = ( - date_time_values.year, - date_time_values.month, - date_time_values.day_of_week, - date_time_values.day_of_month, - date_time_values.hours, - date_time_values.minutes, - date_time_values.seconds, - date_time_values.milliseconds, - ) - - elif class_name == "TimeElements": - json_dict["time_elements_tuple"] = ( - date_time_values.year, - date_time_values.month, - date_time_values.day_of_month, - date_time_values.hours, - date_time_values.minutes, - date_time_values.seconds, - ) - - elif class_name == "TimeElementsInMilliseconds": - json_dict["time_elements_tuple"] = ( - date_time_values.year, - date_time_values.month, - date_time_values.day_of_month, - date_time_values.hours, - date_time_values.minutes, - date_time_values.seconds, - date_time_values.milliseconds, - ) - - elif class_name == "TimeElementsInMicroseconds": - json_dict["time_elements_tuple"] = ( - date_time_values.year, - date_time_values.month, - date_time_values.day_of_month, - date_time_values.hours, - date_time_values.minutes, - date_time_values.seconds, - date_time_values.microseconds, - ) - - if date_time_values.time_zone_offset is not None and class_name not in ( - "GolangTime", - "RFC2579DateTime", - ): - json_dict["time_zone_offset"] = date_time_values.time_zone_offset - - if date_time_values.is_delta and class_name in ( - "TimeElements", - "TimeElementsInMilliseconds", - "TimeElementsInMicroseconds", - ): - json_dict["is_delta"] = True - - if date_time_values.is_local_time: - json_dict["is_local_time"] = True - if date_time_values.time_zone_hint: - json_dict["time_zone_hint"] = date_time_values.time_zone_hint - - return json_dict + return date_time_values.CopyToSerializableDict() @classmethod def ConvertJSONToDateTimeValues(cls, json_dict): diff --git a/dfdatetime/systemtime.py b/dfdatetime/systemtime.py index 705c990..b187f35 100644 --- a/dfdatetime/systemtime.py +++ b/dfdatetime/systemtime.py @@ -44,13 +44,13 @@ def __init__(self, precision=None, system_time_tuple=None, time_zone_offset=None precision=precision or definitions.PRECISION_1_MILLISECOND, time_zone_offset=time_zone_offset, ) - self._number_of_seconds = None self._day_of_month = None self._day_of_week = None self._hours = None self._milliseconds = None self._minutes = None self._month = None + self._number_of_seconds = None self._seconds = None self._year = None @@ -229,5 +229,25 @@ def CopyToDateTimeString(self): f".{self._milliseconds:03d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["system_time_tuple"] = ( + self._year, + self._month, + self._day_of_week, + self._day_of_month, + self._hours, + self._minutes, + self._seconds, + self._milliseconds, + ) + return serializable_dict + factory.Factory.RegisterDateTimeValues(Systemtime) diff --git a/dfdatetime/time_elements.py b/dfdatetime/time_elements.py index 7db6119..c5d6aeb 100644 --- a/dfdatetime/time_elements.py +++ b/dfdatetime/time_elements.py @@ -179,7 +179,6 @@ def _CopyDateTimeFromStringISO8601(self, time_string): hours, minutes, seconds, nanoseconds, time_zone_offset = ( self._CopyTimeFromStringISO8601(time_string[11:]) ) - date_time_values = { "year": year, "month": month, @@ -188,7 +187,6 @@ def _CopyDateTimeFromStringISO8601(self, time_string): "minutes": minutes, "seconds": seconds, } - if nanoseconds is not None: date_time_values["nanoseconds"] = nanoseconds if time_zone_offset is not None: @@ -264,7 +262,6 @@ def _CopyDateTimeFromStringRFC822(self, time_string): hours, minutes, seconds, time_zone_offset = self._CopyTimeFromStringRFC( string_segments[3], string_segments[4] ) - date_time_values = { "year": year, "month": month, @@ -273,7 +270,6 @@ def _CopyDateTimeFromStringRFC822(self, time_string): "minutes": minutes, "time_zone_offset": time_zone_offset, } - if seconds is not None: date_time_values["seconds"] = seconds @@ -345,7 +341,6 @@ def _CopyDateTimeFromStringRFC1123(self, time_string): hours, minutes, seconds, time_zone_offset = self._CopyTimeFromStringRFC( string_segments[3], string_segments[4] ) - date_time_values = { "year": year, "month": month, @@ -354,7 +349,6 @@ def _CopyDateTimeFromStringRFC1123(self, time_string): "minutes": minutes, "time_zone_offset": time_zone_offset, } - if seconds is not None: date_time_values["seconds"] = seconds @@ -695,7 +689,6 @@ def CopyFromDatetime(self, datetime_object): year, month, day_of_month, hours, minutes, seconds, _, _, _ = ( datetime_object.utctimetuple() ) - date_time_values = { "year": year, "month": month, @@ -704,7 +697,6 @@ def CopyFromDatetime(self, datetime_object): "minutes": minutes, "seconds": seconds, } - self._CopyFromDateTimeValues(date_time_values) self.is_local_time = bool(datetime_object.tzinfo is None) @@ -865,6 +857,21 @@ def CopyToDateTimeString(self): f"{hours:02d}:{minutes:02d}:{seconds:02d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["time_elements_tuple"] = self._time_elements_tuple + + if self._is_delta: + serializable_dict["is_delta"] = True + + return serializable_dict + def NewFromDeltaAndDate(self, year, month, day_of_month): """Creates a new time elements instance from a date time delta and a date. @@ -889,7 +896,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): delta_year, delta_month, delta_day_of_month, hours, minutes, seconds = ( self._time_elements_tuple ) - time_elements_tuple = ( year + delta_year, month + delta_month, @@ -898,13 +904,11 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): minutes, seconds, ) - date_time = TimeElements( precision=self._precision, time_elements_tuple=time_elements_tuple, time_zone_offset=self._time_zone_offset, ) - date_time.is_local_time = self.is_local_time return date_time @@ -991,7 +995,6 @@ def _GetNormalizedTimestamp(self): self._normalized_timestamp = ( decimal.Decimal(self._number_of_seconds) + self.fraction_of_second ) - if self._time_zone_offset: self._normalized_timestamp -= self._time_zone_offset * 60 @@ -1020,11 +1023,9 @@ def _CopyFromDateTimeValues(self, date_time_values): precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper( self._precision ) - fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( nanoseconds ) - self._normalized_timestamp = None self._number_of_seconds = self._GetNumberOfSecondsFromElements( year, month, day_of_month, hours, minutes, seconds @@ -1047,7 +1048,6 @@ def CopyFromDatetime(self, datetime_object): precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper( self._precision ) - fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( datetime_object.microsecond * definitions.NANOSECONDS_PER_MICROSECOND ) @@ -1067,10 +1067,8 @@ def CopyFromStringTuple(self, time_elements_tuple): number_of_elements = len(time_elements_tuple) if number_of_elements < 7: raise ValueError( - ( - f"Invalid time elements tuple at least 7 elements required," - f"got: {number_of_elements:d}" - ) + f"Invalid time elements tuple at least 7 elements required," + f"got: {number_of_elements:d}" ) super().CopyFromStringTuple(time_elements_tuple) @@ -1107,7 +1105,6 @@ def CopyToDateTimeString(self): precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper( self._precision ) - return precision_helper.CopyToDateTimeString( self._time_elements_tuple, self.fraction_of_second ) @@ -1137,7 +1134,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): delta_year, delta_month, delta_day_of_month, hours, minutes, seconds = ( self._time_elements_tuple ) - time_elements_tuple = ( year + delta_year, month + delta_month, @@ -1146,7 +1142,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): minutes, seconds, ) - return TimeElementsWithFractionOfSecond( fraction_of_second=self.fraction_of_second, precision=self._precision, @@ -1261,7 +1256,6 @@ def CopyFromStringTuple(self, time_elements_tuple): year, month, day_of_month, hours, minutes, seconds, milliseconds = ( time_elements_tuple ) - try: milliseconds = int(milliseconds, 10) except (TypeError, ValueError): @@ -1273,7 +1267,6 @@ def CopyFromStringTuple(self, time_elements_tuple): fraction_of_second = ( decimal.Decimal(milliseconds) / definitions.MILLISECONDS_PER_SECOND ) - time_elements_tuple = ( year, month, @@ -1283,9 +1276,26 @@ def CopyFromStringTuple(self, time_elements_tuple): seconds, str(fraction_of_second), ) - super().CopyFromStringTuple(time_elements_tuple) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["time_elements_tuple"] = ( + *self._time_elements_tuple, + self.milliseconds, + ) + + if self._is_delta: + serializable_dict["is_delta"] = True + + return serializable_dict + def NewFromDeltaAndDate(self, year, month, day_of_month): """Creates a new time elements instance from a date time delta and a date. @@ -1311,7 +1321,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): delta_year, delta_month, delta_day_of_month, hours, minutes, seconds = ( self._time_elements_tuple ) - time_elements_tuple = ( year + delta_year, month + delta_month, @@ -1321,7 +1330,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): seconds, self.milliseconds, ) - return TimeElementsInMilliseconds( precision=self._precision, time_elements_tuple=time_elements_tuple, @@ -1435,7 +1443,6 @@ def CopyFromStringTuple(self, time_elements_tuple): year, month, day_of_month, hours, minutes, seconds, microseconds = ( time_elements_tuple ) - try: microseconds = int(microseconds, 10) except (TypeError, ValueError): @@ -1447,7 +1454,6 @@ def CopyFromStringTuple(self, time_elements_tuple): fraction_of_second = ( decimal.Decimal(microseconds) / definitions.MICROSECONDS_PER_SECOND ) - time_elements_tuple = ( year, month, @@ -1457,9 +1463,26 @@ def CopyFromStringTuple(self, time_elements_tuple): seconds, str(fraction_of_second), ) - super().CopyFromStringTuple(time_elements_tuple) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["time_elements_tuple"] = ( + *self._time_elements_tuple, + self.microseconds, + ) + + if self._is_delta: + serializable_dict["is_delta"] = True + + return serializable_dict + def NewFromDeltaAndDate(self, year, month, day_of_month): """Creates a new time elements instance from a date time delta and a year. @@ -1485,7 +1508,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): delta_year, delta_month, delta_day_of_month, hours, minutes, seconds = ( self._time_elements_tuple ) - time_elements_tuple = ( year + delta_year, month + delta_month, @@ -1495,7 +1517,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): seconds, self.microseconds, ) - return TimeElementsInMicroseconds( precision=self._precision, time_elements_tuple=time_elements_tuple, @@ -1609,7 +1630,6 @@ def CopyFromStringTuple(self, time_elements_tuple): year, month, day_of_month, hours, minutes, seconds, nanoseconds = ( time_elements_tuple ) - try: nanoseconds = int(nanoseconds, 10) except (TypeError, ValueError): @@ -1621,7 +1641,6 @@ def CopyFromStringTuple(self, time_elements_tuple): fraction_of_second = ( decimal.Decimal(nanoseconds) / definitions.NANOSECONDS_PER_SECOND ) - time_elements_tuple = ( year, month, @@ -1631,9 +1650,26 @@ def CopyFromStringTuple(self, time_elements_tuple): seconds, str(fraction_of_second), ) - super().CopyFromStringTuple(time_elements_tuple) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["time_elements_tuple"] = ( + *self._time_elements_tuple, + self.nanoseconds, + ) + + if self._is_delta: + serializable_dict["is_delta"] = True + + return serializable_dict + def NewFromDeltaAndDate(self, year, month, day_of_month): """Creates a new time elements instance from a date time delta and a year. @@ -1659,7 +1695,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): delta_year, delta_month, delta_day_of_month, hours, minutes, seconds = ( self._time_elements_tuple ) - time_elements_tuple = ( year + delta_year, month + delta_month, @@ -1669,7 +1704,6 @@ def NewFromDeltaAndDate(self, year, month, day_of_month): seconds, self.nanoseconds, ) - return TimeElementsInNanoseconds( precision=self._precision, time_elements_tuple=time_elements_tuple, diff --git a/dfdatetime/uuid_time.py b/dfdatetime/uuid_time.py index dc0267f..69d9f03 100644 --- a/dfdatetime/uuid_time.py +++ b/dfdatetime/uuid_time.py @@ -149,11 +149,22 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{fraction_of_second:07d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(UUIDTime) diff --git a/dfdatetime/webkit_time.py b/dfdatetime/webkit_time.py index 7b33b3c..68912ab 100644 --- a/dfdatetime/webkit_time.py +++ b/dfdatetime/webkit_time.py @@ -55,9 +55,10 @@ def _GetNormalizedTimestamp(self): """Retrieves the normalized timestamp. Returns: - float: normalized timestamp, which contains the number of seconds since - January 1, 1970 00:00:00 and a fraction of second used for increased - precision, or None if the normalized timestamp cannot be determined. + decimal.Decimal: normalized timestamp, which contains the number of + seconds since January 1, 1970 00:00:00 and a fraction of second used + for increased precision, or None if the normalized timestamp cannot be + determined. """ if self._normalized_timestamp is None: if ( @@ -137,11 +138,22 @@ def CopyToDateTimeString(self): year, month, day_of_month = self._GetDateValuesWithEpoch( number_of_days, self._EPOCH ) - return ( f"{year:04d}-{month:02d}-{day_of_month:02d} " f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}" ) + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + serializable_dict = self._CreateSerializableDict() + + serializable_dict["timestamp"] = self._timestamp + + return serializable_dict + factory.Factory.RegisterDateTimeValues(WebKitTime) diff --git a/tests/cocoa_time.py b/tests/cocoa_time.py index 92845a7..4b20c03 100644 --- a/tests/cocoa_time.py +++ b/tests/cocoa_time.py @@ -39,7 +39,6 @@ def testGetNormalizedTimestamp(self): cocoa_time_object = cocoa_time.CocoaTime( time_zone_offset=60, timestamp=395011845.0 ) - normalized_timestamp = cocoa_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1373315445.0")) @@ -109,6 +108,18 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = cocoa_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2013-07-08T21:30:45.546875+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) + + expected_serializable_dict = { + "__class_name__": "CocoaTime", + "__type__": "DateTimeValues", + "timestamp": 395011845.546875, + } + serializable_dict = cocoa_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" cocoa_time_object = cocoa_time.CocoaTime(timestamp=395011845.546875) diff --git a/tests/delphi_date_time.py b/tests/delphi_date_time.py index fd4e344..a36043c 100644 --- a/tests/delphi_date_time.py +++ b/tests/delphi_date_time.py @@ -68,7 +68,6 @@ def testGetNormalizedTimestamp(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - expected_normalized_timestamp = decimal.Decimal("1371585000.553919887170195579") normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, expected_normalized_timestamp) @@ -76,7 +75,6 @@ def testGetNormalizedTimestamp(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( time_zone_offset=60, timestamp=41443.8263953 ) - expected_normalized_timestamp = decimal.Decimal("1371581400.553919887170195579") normalized_timestamp = delphi_date_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, expected_normalized_timestamp) @@ -137,7 +135,6 @@ def testCopyToDateTimeString(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - date_time_string = delphi_date_time_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2013-06-18 19:50:00.553919") @@ -151,7 +148,6 @@ def testCopyToDateTimeStringISO8601(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2013-06-18T19:50:00.553919+00:00") @@ -159,12 +155,24 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = delphi_date_time_object.CopyToDateTimeStringISO8601() self.assertIsNone(date_time_string) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + delphi_date_time_object = delphi_date_time.DelphiDateTime( + timestamp=41443.8263953 + ) + expected_serializable_dict = { + "__class_name__": "DelphiDateTime", + "__type__": "DateTimeValues", + "timestamp": 41443.8263953, + } + serializable_dict = delphi_date_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - date_tuple = delphi_date_time_object.GetDate() self.assertEqual(date_tuple, (2013, 6, 18)) @@ -178,7 +186,6 @@ def testGetDateWithTimeOfDay(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - date_with_time_of_day_tuple = delphi_date_time_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2013, 6, 18, 19, 50, 0)) @@ -195,7 +202,6 @@ def testGetPlasoTimestamp(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - micro_posix_timestamp = delphi_date_time_object.GetPlasoTimestamp() self.assertEqual(micro_posix_timestamp, 1371585000553920) @@ -214,7 +220,6 @@ def testGetTimeOfDay(self): delphi_date_time_object = delphi_date_time.DelphiDateTime( timestamp=41443.8263953 ) - time_of_day_tuple = delphi_date_time_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (19, 50, 0)) diff --git a/tests/dotnet_datetime.py b/tests/dotnet_datetime.py index 7faf374..346fdf6 100644 --- a/tests/dotnet_datetime.py +++ b/tests/dotnet_datetime.py @@ -41,7 +41,6 @@ def testGetNormalizedTimestamp(self): dotnet_date_time = dotnet_datetime.DotNetDateTime( time_zone_offset=60, timestamp=637433719321230000 ) - expected_normalized_timestamp = decimal.Decimal(1607771532123) / 1000 normalized_timestamp = dotnet_date_time._GetNormalizedTimestamp() @@ -102,6 +101,18 @@ def testCopyToDateTimeString(self): with self.assertRaises(ValueError): dotnet_date_time.CopyFromDateTimeString("10000-01-01") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + dotnet_date_time = dotnet_datetime.DotNetDateTime(timestamp=637433719321230000) + + expected_serializable_dict = { + "__class_name__": "DotNetDateTime", + "__type__": "DateTimeValues", + "timestamp": 637433719321230000, + } + serializable_dict = dotnet_date_time.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + if __name__ == "__main__": unittest.main() diff --git a/tests/factory.py b/tests/factory.py index db1fd0b..1be917d 100644 --- a/tests/factory.py +++ b/tests/factory.py @@ -49,6 +49,17 @@ def CopyToDateTimeString(self): """ return None + def CopyToSerializableDict(self): + """Copies the date time value to a serializable dictionary. + + Returns: + dict[str, object]: serializable dictionary. + """ + return { + "__class_name__": type(self).__name__, + "__type__": "DateTimeValues", + } + class FactoryTest(unittest.TestCase): """Tests the date and time values factory.""" diff --git a/tests/fake_time.py b/tests/fake_time.py index 5bc118c..848ba21 100644 --- a/tests/fake_time.py +++ b/tests/fake_time.py @@ -102,6 +102,19 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = fake_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T21:06:31.546875+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + fake_time_object = fake_time.FakeTime() + fake_time_object.CopyFromDateTimeString("2010-08-12 21:06:31.546875") + + expected_serializable_dict = { + "__class_name__": "FakeTime", + "__type__": "DateTimeValues", + "timestamp": 1281647191546875, + } + serializable_dict = fake_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" fake_time_object = fake_time.FakeTime() diff --git a/tests/fat_date_time.py b/tests/fat_date_time.py index 914d42a..87904e8 100644 --- a/tests/fat_date_time.py +++ b/tests/fat_date_time.py @@ -127,6 +127,18 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = fat_date_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T21:06:32+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + fat_date_time_object = fat_date_time.FATDateTime(fat_date_time=0xA8D03D0C) + + expected_serializable_dict = { + "__class_name__": "FATDateTime", + "__type__": "DateTimeValues", + "fat_date_time": 0xA8D03D0C, + } + serializable_dict = fat_date_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" fat_date_time_object = fat_date_time.FATDateTime(fat_date_time=0xA8D03D0C) @@ -266,6 +278,18 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + fat_timestamp_object = fat_date_time.FATTimestamp(timestamp=131033589024) + + expected_serializable_dict = { + "__class_name__": "FATTimestamp", + "__type__": "DateTimeValues", + "timestamp": 131033589024, + } + serializable_dict = fat_timestamp_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" fat_timestamp_object = fat_date_time.FATTimestamp(timestamp=131033589024) diff --git a/tests/filetime.py b/tests/filetime.py index dea4070..825165a 100644 --- a/tests/filetime.py +++ b/tests/filetime.py @@ -39,7 +39,6 @@ def testGetNormalizedTimestamp(self): filetime_object = filetime.Filetime( time_zone_offset=60, timestamp=0x01CB3A623D0A17CE ) - normalized_timestamp = filetime_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.546875")) @@ -153,6 +152,18 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + filetime_object = filetime.Filetime(timestamp=0x01CB3A623D0A17CE) + + expected_serializable_dict = { + "__class_name__": "Filetime", + "__type__": "DateTimeValues", + "timestamp": 0x01CB3A623D0A17CE, + } + serializable_dict = filetime_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" filetime_object = filetime.Filetime(timestamp=0x01CB3A623D0A17CE) diff --git a/tests/golang_time.py b/tests/golang_time.py index a472157..d7957c3 100644 --- a/tests/golang_time.py +++ b/tests/golang_time.py @@ -202,6 +202,21 @@ def testCopyToDateTimeString(self): date_time_string = golang_time_object.CopyToDateTimeString() self.assertIsNone(date_time_string) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + golang_timestamp = bytes.fromhex("010000000eafffe8d121d95050ffff") + golang_time_object = golang_time.GolangTime(golang_timestamp=golang_timestamp) + + expected_serializable_dict = { + "__class_name__": "GolangTime", + "__type__": "DateTimeValues", + "golang_timestamp": ( + b"\x01\x00\x00\x00\x0e\xaf\xff\xe8\xd1\x21\xd9\x50\x50\xff\xff" + ), + } + serializable_dict = golang_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + if __name__ == "__main__": unittest.main() diff --git a/tests/hfs_time.py b/tests/hfs_time.py index f9e4d6a..ce81a03 100644 --- a/tests/hfs_time.py +++ b/tests/hfs_time.py @@ -112,6 +112,18 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = hfs_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2013-08-01T15:25:28+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) + + expected_serializable_dict = { + "__class_name__": "HFSTime", + "__type__": "DateTimeValues", + "timestamp": 3458215528, + } + serializable_dict = hfs_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" hfs_time_object = hfs_time.HFSTime(timestamp=3458215528) diff --git a/tests/ole_automation_date.py b/tests/ole_automation_date.py index bbc1d35..d66268b 100644 --- a/tests/ole_automation_date.py +++ b/tests/ole_automation_date.py @@ -36,7 +36,6 @@ def testGetNormalizedTimestamp(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - expected_normalized_timestamp = decimal.Decimal("1509881520.038400194607675076") normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, expected_normalized_timestamp) @@ -44,7 +43,6 @@ def testGetNormalizedTimestamp(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( time_zone_offset=60, timestamp=43044.480556 ) - expected_normalized_timestamp = decimal.Decimal("1509877920.038400194607675076") normalized_timestamp = ole_automation_date_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, expected_normalized_timestamp) @@ -100,7 +98,6 @@ def testCopyToDateTimeString(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - date_time_string = ole_automation_date_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2017-11-05 11:32:00.038400") @@ -114,16 +111,27 @@ def testCopyToDateTimeStringISO8601(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - date_time_string = ole_automation_date_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2017-11-05T11:32:00.038400+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + ole_automation_date_object = ole_automation_date.OLEAutomationDate( + timestamp=43044.480556 + ) + expected_serializable_dict = { + "__class_name__": "OLEAutomationDate", + "__type__": "DateTimeValues", + "timestamp": 43044.480556, + } + serializable_dict = ole_automation_date_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - date_tuple = ole_automation_date_object.GetDate() self.assertEqual(date_tuple, (2017, 11, 5)) @@ -137,7 +145,6 @@ def testGetDateWithTimeOfDay(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - date_with_time_of_day_tuple = ole_automation_date_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2017, 11, 5, 11, 32, 0)) @@ -153,7 +160,6 @@ def testGetTimeOfDay(self): ole_automation_date_object = ole_automation_date.OLEAutomationDate( timestamp=43044.480556 ) - time_of_day_tuple = ole_automation_date_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (11, 32, 0)) diff --git a/tests/posix_time.py b/tests/posix_time.py index 8da7f25..da47f4c 100644 --- a/tests/posix_time.py +++ b/tests/posix_time.py @@ -39,7 +39,6 @@ def testGetNormalizedTimestamp(self): posix_time_object = posix_time.PosixTime( time_zone_offset=60, timestamp=1281643591 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.0")) @@ -132,6 +131,18 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + posix_time_object = posix_time.PosixTime(timestamp=1281643591) + + expected_serializable_dict = { + "__class_name__": "PosixTime", + "__type__": "DateTimeValues", + "timestamp": 1281643591, + } + serializable_dict = posix_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" posix_time_object = posix_time.PosixTime(timestamp=1281643591) @@ -194,7 +205,6 @@ def testGetNormalizedTimestamp(self): posix_time_object = posix_time.PosixTimeInMilliseconds( time_zone_offset=60, timestamp=1281643591546 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546")) @@ -261,7 +271,6 @@ def testCopyToDateTimeStringISO8601(self): posix_time_object = posix_time.PosixTimeInMilliseconds( timestamp=-11644468446327 ) - date_time_string = posix_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "1601-01-01T01:25:53.673+00:00") @@ -278,14 +287,13 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): posix_time_object = posix_time.PosixTimeInMilliseconds( timestamp=-11644468446327 ) - posix_timestamp, fraction_of_second = ( posix_time_object.CopyToPosixTimestampWithFractionOfSecond() ) self.assertEqual(posix_timestamp, -11644468446) self.assertEqual(fraction_of_second, 327) - posix_time_object = posix_time.PosixTime() + posix_time_object = posix_time.PosixTimeInMilliseconds() posix_timestamp, fraction_of_second = ( posix_time_object.CopyToPosixTimestampWithFractionOfSecond() @@ -293,6 +301,18 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) + + expected_serializable_dict = { + "__class_name__": "PosixTimeInMilliseconds", + "__type__": "DateTimeValues", + "timestamp": 1281643591546, + } + serializable_dict = posix_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" posix_time_object = posix_time.PosixTimeInMilliseconds(timestamp=1281643591546) @@ -352,14 +372,12 @@ def testGetNormalizedTimestamp(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.546875")) posix_time_object = posix_time.PosixTimeInMicroseconds( time_zone_offset=60, timestamp=1281643591546875 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.546875")) @@ -411,7 +429,6 @@ def testCopyToDateTimeString(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - date_time_string = posix_time_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.546875") @@ -425,14 +442,12 @@ def testCopyToDateTimeStringISO8601(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - date_time_string = posix_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.546875+00:00") posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=-11644468446327447 ) - date_time_string = posix_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "1601-01-01T01:25:53.672553+00:00") @@ -441,7 +456,6 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - posix_timestamp, fraction_of_second = ( posix_time_object.CopyToPosixTimestampWithFractionOfSecond() ) @@ -451,14 +465,13 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=-11644468446327447 ) - posix_timestamp, fraction_of_second = ( posix_time_object.CopyToPosixTimestampWithFractionOfSecond() ) self.assertEqual(posix_timestamp, -11644468446) self.assertEqual(fraction_of_second, 327447) - posix_time_object = posix_time.PosixTime() + posix_time_object = posix_time.PosixTimeInMicroseconds() posix_timestamp, fraction_of_second = ( posix_time_object.CopyToPosixTimestampWithFractionOfSecond() @@ -466,12 +479,24 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + posix_time_object = posix_time.PosixTimeInMicroseconds( + timestamp=1281643591546875 + ) + expected_serializable_dict = { + "__class_name__": "PosixTimeInMicroseconds", + "__type__": "DateTimeValues", + "timestamp": 1281643591546875, + } + serializable_dict = posix_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - date_tuple = posix_time_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -485,7 +510,6 @@ def testGetDateWithTimeOfDay(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -501,7 +525,6 @@ def testGetTimeOfDay(self): posix_time_object = posix_time.PosixTimeInMicroseconds( timestamp=1281643591546875 ) - time_of_day_tuple = posix_time_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) @@ -531,14 +554,12 @@ def testGetNormalizedTimestamp(self): posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.987654321")) posix_time_object = posix_time.PosixTimeInNanoseconds( time_zone_offset=60, timestamp=1281643591987654321 ) - normalized_timestamp = posix_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.987654321")) @@ -588,7 +609,6 @@ def testCopyToDateTimeString(self): posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - date_time_string = posix_time_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.987654321") @@ -602,16 +622,55 @@ def testCopyToDateTimeStringISO8601(self): posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - date_time_string = posix_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.987654321+00:00") + def testCopyToPosixTimestampWithFractionOfSecond(self): + """Tests the CopyToPosixTimestampWithFractionOfSecond function.""" + posix_time_object = posix_time.PosixTimeInNanoseconds( + timestamp=1281643591987654321 + ) + posix_timestamp, fraction_of_second = ( + posix_time_object.CopyToPosixTimestampWithFractionOfSecond() + ) + self.assertEqual(posix_timestamp, 1281643591) + self.assertEqual(fraction_of_second, 987654321) + + posix_time_object = posix_time.PosixTimeInNanoseconds( + timestamp=-11644468446327447321 + ) + posix_timestamp, fraction_of_second = ( + posix_time_object.CopyToPosixTimestampWithFractionOfSecond() + ) + self.assertEqual(posix_timestamp, -11644468446) + self.assertEqual(fraction_of_second, 327447321) + + posix_time_object = posix_time.PosixTimeInNanoseconds() + + posix_timestamp, fraction_of_second = ( + posix_time_object.CopyToPosixTimestampWithFractionOfSecond() + ) + self.assertIsNone(posix_timestamp) + self.assertIsNone(fraction_of_second) + + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + posix_time_object = posix_time.PosixTimeInNanoseconds( + timestamp=1281643591987654321 + ) + expected_serializable_dict = { + "__class_name__": "PosixTimeInNanoseconds", + "__type__": "DateTimeValues", + "timestamp": 1281643591987654321, + } + serializable_dict = posix_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - date_tuple = posix_time_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -625,7 +684,6 @@ def testGetDateWithTimeOfDay(self): posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - date_with_time_of_day_tuple = posix_time_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -641,7 +699,6 @@ def testGetTimeOfDay(self): posix_time_object = posix_time.PosixTimeInNanoseconds( timestamp=1281643591987654321 ) - time_of_day_tuple = posix_time_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) diff --git a/tests/rfc2579_date_time.py b/tests/rfc2579_date_time.py index f48c963..e5fa20d 100644 --- a/tests/rfc2579_date_time.py +++ b/tests/rfc2579_date_time.py @@ -127,14 +127,12 @@ def testGetNormalizedTimestamp(self): rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - normalized_timestamp = rfc2579_date_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.6")) rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 1, 0) ) - normalized_timestamp = rfc2579_date_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.6")) @@ -237,7 +235,6 @@ def testCopyToDateTimeString(self): rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - date_time_string = rfc2579_date_time_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.6") @@ -251,16 +248,27 @@ def testCopyToDateTimeStringISO8601(self): rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - date_time_string = rfc2579_date_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.6+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( + rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) + ) + expected_serializable_dict = { + "__class_name__": "RFC2579DateTime", + "__type__": "DateTimeValues", + "rfc2579_date_time_tuple": (2010, 8, 12, 20, 6, 31, 6, "+", 0, 0), + } + serializable_dict = rfc2579_date_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - date_tuple = rfc2579_date_time_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -274,7 +282,6 @@ def testGetDateWithTimeOfDay(self): rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - date_with_time_of_day_tuple = rfc2579_date_time_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -290,7 +297,6 @@ def testGetTimeOfDay(self): rfc2579_date_time_object = rfc2579_date_time.RFC2579DateTime( rfc2579_date_time_tuple=(2010, 8, 12, 20, 6, 31, 6, "+", 0, 0) ) - time_of_day_tuple = rfc2579_date_time_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) diff --git a/tests/semantic_time.py b/tests/semantic_time.py index 5d098e1..fc6f6a9 100644 --- a/tests/semantic_time.py +++ b/tests/semantic_time.py @@ -140,6 +140,18 @@ def testInitialize(self): invalid_time_object = semantic_time.InvalidTime() self.assertEqual(invalid_time_object.string, "Invalid") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + invalid_time_object = semantic_time.InvalidTime() + + expected_serializable_dict = { + "__class_name__": "InvalidTime", + "__type__": "DateTimeValues", + "string": "Invalid", + } + serializable_dict = invalid_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + class NeverTest(unittest.TestCase): """Tests for semantic time that represents never.""" @@ -199,6 +211,18 @@ def testComparison(self): self.assertTrue(never_time_object1 != 0.0) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + never_time_object = semantic_time.Never() + + expected_serializable_dict = { + "__class_name__": "Never", + "__type__": "DateTimeValues", + "string": "Never", + } + serializable_dict = never_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + class NotSetTest(unittest.TestCase): """Tests for semantic time that represents not set.""" @@ -208,6 +232,18 @@ def testInitialize(self): not_set_time_object = semantic_time.NotSet() self.assertEqual(not_set_time_object.string, "Not set") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + not_set_time_object = semantic_time.NotSet() + + expected_serializable_dict = { + "__class_name__": "NotSet", + "__type__": "DateTimeValues", + "string": "Not set", + } + serializable_dict = not_set_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + if __name__ == "__main__": unittest.main() diff --git a/tests/systemtime.py b/tests/systemtime.py index ff059a6..819873b 100644 --- a/tests/systemtime.py +++ b/tests/systemtime.py @@ -61,14 +61,12 @@ def testGetNormalizedTimestamp(self): systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - normalized_timestamp = systemtime_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.142")) systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142), time_zone_offset=60 ) - normalized_timestamp = systemtime_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.142")) @@ -165,7 +163,6 @@ def testCopyToDateTimeString(self): systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - date_time_string = systemtime_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.142") @@ -179,16 +176,27 @@ def testCopyToDateTimeStringISO8601(self): systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - date_time_string = systemtime_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.142+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + systemtime_object = systemtime.Systemtime( + system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) + ) + expected_serializable_dict = { + "__class_name__": "Systemtime", + "__type__": "DateTimeValues", + "system_time_tuple": (2010, 8, 4, 12, 20, 6, 31, 142), + } + serializable_dict = systemtime_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - date_tuple = systemtime_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -202,7 +210,6 @@ def testGetDateWithTimeOfDay(self): systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - date_with_time_of_day_tuple = systemtime_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -218,7 +225,6 @@ def testGetTimeOfDay(self): systemtime_object = systemtime.Systemtime( system_time_tuple=(2010, 8, 4, 12, 20, 6, 31, 142) ) - time_of_day_tuple = systemtime_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) diff --git a/tests/time_elements.py b/tests/time_elements.py index f9b5f2d..74b5773 100644 --- a/tests/time_elements.py +++ b/tests/time_elements.py @@ -94,14 +94,12 @@ def testGetNormalizedTimestamp(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591")) time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31), time_zone_offset=60 ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991")) @@ -900,14 +898,12 @@ def testCopyToDateTimeString(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31") time_elements_object = time_elements.TimeElements( time_elements_tuple=(0, 8, 12, 20, 6, 31) ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "0000-08-12 20:06:31") @@ -921,7 +917,6 @@ def testCopyToDateTimeStringISO8601(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31+00:00") @@ -933,14 +928,12 @@ def testCopyToDateTimeStringISO8601(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31), time_zone_offset=120 ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31+02:00") time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31), time_zone_offset=-300 ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31-05:00") @@ -949,7 +942,6 @@ def testCopyToPosixTimestamp(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - posix_timestamp = time_elements_object.CopyToPosixTimestamp() self.assertEqual(posix_timestamp, 1281643591) @@ -963,7 +955,6 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - posix_timestamp, fraction_of_second = ( time_elements_object.CopyToPosixTimestampWithFractionOfSecond() ) @@ -978,12 +969,24 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + time_elements_object = time_elements.TimeElements( + time_elements_tuple=(2010, 8, 12, 20, 6, 31) + ) + expected_serializable_dict = { + "__class_name__": "TimeElements", + "__type__": "DateTimeValues", + "time_elements_tuple": (2010, 8, 12, 20, 6, 31), + } + serializable_dict = time_elements_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - date_tuple = time_elements_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -997,7 +1000,6 @@ def testGetDateWithTimeOfDay(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - date_with_time_of_day_tuple = time_elements_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -1013,7 +1015,6 @@ def testGetTimeOfDay(self): time_elements_object = time_elements.TimeElements( time_elements_tuple=(2010, 8, 12, 20, 6, 31) ) - time_of_day_tuple = time_elements_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) @@ -1027,7 +1028,6 @@ def testNewFromDeltaAndDate(self): time_elements_object = time_elements.TimeElements( is_delta=True, time_elements_tuple=(1, 0, 0, 20, 6, 31) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndDate(2009, 1, 12) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -1050,7 +1050,6 @@ def testNewFromDeltaAndYear(self): time_elements_object = time_elements.TimeElements( is_delta=True, time_elements_tuple=(1, 8, 12, 20, 6, 31) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndYear(2009) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -1167,7 +1166,6 @@ def testCopyFromDateTimeValues(self): "nanoseconds": 123456789, "time_zone_offset": 60, } - time_elements_object = time_elements.TimeElementsWithFractionOfSecond( precision=definitions.PRECISION_10_MILLISECONDS ) @@ -1179,7 +1177,6 @@ def testCopyFromDateTimeValues(self): self.assertEqual( time_elements_object.fraction_of_second, decimal.Decimal("0.12") ) - time_elements_object = time_elements.TimeElementsWithFractionOfSecond( precision=definitions.PRECISION_1_MILLISECOND ) @@ -1187,7 +1184,6 @@ def testCopyFromDateTimeValues(self): self.assertEqual( time_elements_object.fraction_of_second, decimal.Decimal("0.123") ) - time_elements_object = time_elements.TimeElementsWithFractionOfSecond( precision=definitions.PRECISION_100_MICROSECONDS ) @@ -1239,20 +1235,17 @@ def testCopyFromStringTuple(self): time_elements_object = time_elements.TimeElementsWithFractionOfSecond( precision=definitions.PRECISION_10_MILLISECONDS ) - expected_time_elements_tuple = (2010, 8, 12, 20, 6, 31) expected_fraction_of_second = decimal.Decimal("0.46") time_elements_object.CopyFromStringTuple( time_elements_tuple=("2010", "8", "12", "20", "6", "31", "0.46") ) - self.assertEqual( time_elements_object._time_elements_tuple, expected_time_elements_tuple ) self.assertEqual( time_elements_object.fraction_of_second, expected_fraction_of_second ) - time_elements_object = time_elements.TimeElementsWithFractionOfSecond( precision=definitions.PRECISION_100_MICROSECONDS ) @@ -1262,7 +1255,6 @@ def testCopyFromStringTuple(self): self.assertEqual( time_elements_object.fraction_of_second, decimal.Decimal("0.4671") ) - with self.assertRaises(ValueError): time_elements_object.CopyFromStringTuple( time_elements_tuple=("2010", "8", "12", "20", "6", "31") @@ -1298,7 +1290,6 @@ def testCopyToDateTimeString(self): precision=definitions.PRECISION_100_MICROSECONDS, time_elements_tuple=(2010, 8, 12, 20, 6, 31), ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.8741") @@ -1310,7 +1301,6 @@ def testNewFromDeltaAndDate(self): precision=definitions.PRECISION_10_MILLISECONDS, time_elements_tuple=(1, 0, 0, 20, 6, 31), ) - new_time_elements_object = time_elements_object.NewFromDeltaAndDate(2009, 1, 12) self.assertFalse(new_time_elements_object.is_delta) self.assertEqual(new_time_elements_object.year, 2010) @@ -1319,11 +1309,9 @@ def testNewFromDeltaAndDate(self): self.assertEqual( new_time_elements_object.fraction_of_second, decimal.Decimal("0.87") ) - time_elements_object = time_elements.TimeElementsWithFractionOfSecond( time_elements_tuple=(1, 0, 0, 20, 6, 31) ) - with self.assertRaises(ValueError): time_elements_object.NewFromDeltaAndDate(2009, 1, 12) @@ -1381,14 +1369,12 @@ def testGetNormalizedTimestamp(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.429")) time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429), time_zone_offset=60 ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.429")) @@ -1634,7 +1620,6 @@ def testCopyToDateTimeString(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.429") @@ -1648,7 +1633,6 @@ def testCopyToDateTimeStringISO8601(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.429+00:00") @@ -1657,7 +1641,6 @@ def testCopyToPosixTimestamp(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - posix_timestamp = time_elements_object.CopyToPosixTimestamp() self.assertEqual(posix_timestamp, 1281643591) @@ -1671,7 +1654,6 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - posix_timestamp, fraction_of_second = ( time_elements_object.CopyToPosixTimestampWithFractionOfSecond() ) @@ -1686,12 +1668,24 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + time_elements_object = time_elements.TimeElementsInMilliseconds( + time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) + ) + expected_serializable_dict = { + "__class_name__": "TimeElementsInMilliseconds", + "__type__": "DateTimeValues", + "time_elements_tuple": (2010, 8, 12, 20, 6, 31, 429), + } + serializable_dict = time_elements_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - date_tuple = time_elements_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -1705,7 +1699,6 @@ def testGetDateWithTimeOfDay(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - date_with_time_of_day_tuple = time_elements_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -1721,7 +1714,6 @@ def testGetTimeOfDay(self): time_elements_object = time_elements.TimeElementsInMilliseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429) ) - time_of_day_tuple = time_elements_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) @@ -1735,7 +1727,6 @@ def testNewFromDeltaAndDate(self): time_elements_object = time_elements.TimeElementsInMilliseconds( is_delta=True, time_elements_tuple=(1, 0, 0, 20, 6, 31, 429) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndDate(2009, 1, 12) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -1759,7 +1750,6 @@ def testNewFromDeltaAndYear(self): time_elements_object = time_elements.TimeElementsInMilliseconds( is_delta=True, time_elements_tuple=(1, 8, 12, 20, 6, 31, 429) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndYear(2009) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -1812,14 +1802,12 @@ def testGetNormalizedTimestamp(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.429876")) time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876), time_zone_offset=60 ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.429876")) @@ -2065,7 +2053,6 @@ def testCopyToDateTimeString(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.429876") @@ -2079,7 +2066,6 @@ def testCopyToDateTimeStringISO8601(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.429876+00:00") @@ -2088,7 +2074,6 @@ def testCopyToPosixTimestamp(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - posix_timestamp = time_elements_object.CopyToPosixTimestamp() self.assertEqual(posix_timestamp, 1281643591) @@ -2102,7 +2087,6 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - posix_timestamp, fraction_of_second = ( time_elements_object.CopyToPosixTimestampWithFractionOfSecond() ) @@ -2117,12 +2101,24 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + time_elements_object = time_elements.TimeElementsInMicroseconds( + time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) + ) + expected_serializable_dict = { + "__class_name__": "TimeElementsInMicroseconds", + "__type__": "DateTimeValues", + "time_elements_tuple": (2010, 8, 12, 20, 6, 31, 429876), + } + serializable_dict = time_elements_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - date_tuple = time_elements_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -2136,7 +2132,6 @@ def testGetDateWithTimeOfDay(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - date_with_time_of_day_tuple = time_elements_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -2152,7 +2147,6 @@ def testGetTimeOfDay(self): time_elements_object = time_elements.TimeElementsInMicroseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876) ) - time_of_day_tuple = time_elements_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) @@ -2166,7 +2160,6 @@ def testNewFromDeltaAndDate(self): time_elements_object = time_elements.TimeElementsInMicroseconds( is_delta=True, time_elements_tuple=(1, 0, 0, 20, 6, 31, 429876) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndDate(2009, 1, 12) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -2190,7 +2183,6 @@ def testNewFromDeltaAndYear(self): time_elements_object = time_elements.TimeElementsInMicroseconds( is_delta=True, time_elements_tuple=(1, 8, 12, 20, 6, 31, 429876) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndYear(2009) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -2243,14 +2235,12 @@ def testGetNormalizedTimestamp(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281643591.429876301")) time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301), time_zone_offset=60 ) - normalized_timestamp = time_elements_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1281639991.429876301")) @@ -2506,7 +2496,6 @@ def testCopyToDateTimeString(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - date_time_string = time_elements_object.CopyToDateTimeString() self.assertEqual(date_time_string, "2010-08-12 20:06:31.429876301") @@ -2520,7 +2509,6 @@ def testCopyToDateTimeStringISO8601(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - date_time_string = time_elements_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T20:06:31.429876301+00:00") @@ -2529,7 +2517,6 @@ def testCopyToPosixTimestamp(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - posix_timestamp = time_elements_object.CopyToPosixTimestamp() self.assertEqual(posix_timestamp, 1281643591) @@ -2543,7 +2530,6 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - posix_timestamp, fraction_of_second = ( time_elements_object.CopyToPosixTimestampWithFractionOfSecond() ) @@ -2558,12 +2544,24 @@ def testCopyToPosixTimestampWithFractionOfSecond(self): self.assertIsNone(posix_timestamp) self.assertIsNone(fraction_of_second) + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + time_elements_object = time_elements.TimeElementsInNanoseconds( + time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) + ) + expected_serializable_dict = { + "__class_name__": "TimeElementsInNanoseconds", + "__type__": "DateTimeValues", + "time_elements_tuple": (2010, 8, 12, 20, 6, 31, 429876301), + } + serializable_dict = time_elements_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - date_tuple = time_elements_object.GetDate() self.assertEqual(date_tuple, (2010, 8, 12)) @@ -2577,7 +2575,6 @@ def testGetDateWithTimeOfDay(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - date_with_time_of_day_tuple = time_elements_object.GetDateWithTimeOfDay() self.assertEqual(date_with_time_of_day_tuple, (2010, 8, 12, 20, 6, 31)) @@ -2593,7 +2590,6 @@ def testGetTimeOfDay(self): time_elements_object = time_elements.TimeElementsInNanoseconds( time_elements_tuple=(2010, 8, 12, 20, 6, 31, 429876301) ) - time_of_day_tuple = time_elements_object.GetTimeOfDay() self.assertEqual(time_of_day_tuple, (20, 6, 31)) @@ -2607,7 +2603,6 @@ def testNewFromDeltaAndDate(self): time_elements_object = time_elements.TimeElementsInNanoseconds( is_delta=True, time_elements_tuple=(1, 0, 0, 20, 6, 31, 429876301) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndDate(2009, 1, 12) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) @@ -2631,7 +2626,6 @@ def testNewFromDeltaAndYear(self): time_elements_object = time_elements.TimeElementsInNanoseconds( is_delta=True, time_elements_tuple=(1, 8, 12, 20, 6, 31, 429876301) ) - new_time_elements_object = time_elements_object.NewFromDeltaAndYear(2009) self.assertIsNotNone(new_time_elements_object) self.assertFalse(new_time_elements_object.is_delta) diff --git a/tests/uuid_time.py b/tests/uuid_time.py index 48a46db..b8f1148 100644 --- a/tests/uuid_time.py +++ b/tests/uuid_time.py @@ -60,7 +60,6 @@ def testGetNormalizedTimestamp(self): uuid_time_object = uuid_time.UUIDTime( time_zone_offset=60, timestamp=uuid_object.time ) - normalized_timestamp = uuid_time_object._GetNormalizedTimestamp() self.assertEqual(normalized_timestamp, decimal.Decimal("1337127061.6544084")) @@ -140,6 +139,19 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = uuid_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2012-05-16T01:11:01.6544084+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") + uuid_time_object = uuid_time.UUIDTime(timestamp=uuid_object.time) + + expected_serializable_dict = { + "__class_name__": "UUIDTime", + "__type__": "DateTimeValues", + "timestamp": 135564234616544084, + } + serializable_dict = uuid_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" uuid_object = uuid.UUID("00911b54-9ef4-11e1-be53-525400123456") diff --git a/tests/webkit_time.py b/tests/webkit_time.py index f4272f7..f9721a6 100644 --- a/tests/webkit_time.py +++ b/tests/webkit_time.py @@ -106,6 +106,18 @@ def testCopyToDateTimeStringISO8601(self): date_time_string = webkit_time_object.CopyToDateTimeStringISO8601() self.assertEqual(date_time_string, "2010-08-12T21:06:31.546875+00:00") + def testCopyToSerializableDict(self): + """Test the CopyToSerializableDict function.""" + webkit_time_object = webkit_time.WebKitTime(timestamp=12926120791546875) + + expected_serializable_dict = { + "__class_name__": "WebKitTime", + "__type__": "DateTimeValues", + "timestamp": 12926120791546875, + } + serializable_dict = webkit_time_object.CopyToSerializableDict() + self.assertEqual(serializable_dict, expected_serializable_dict) + def testGetDate(self): """Tests the GetDate function.""" webkit_time_object = webkit_time.WebKitTime(timestamp=12926120791546875)