Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dfdatetime/apfs_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -80,7 +80,7 @@ def CopyToDateTimeString(self):
):
return None

return super()._CopyToDateTimeString()
return super().CopyToDateTimeString()


factory.Factory.RegisterDateTimeValues(APFSTime)
21 changes: 16 additions & 5 deletions dfdatetime/cocoa_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -122,17 +123,27 @@ 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 (
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(CocoaTime)
17 changes: 13 additions & 4 deletions dfdatetime/delphi_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
13 changes: 12 additions & 1 deletion dfdatetime/dotnet_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 15 additions & 4 deletions dfdatetime/fake_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
28 changes: 24 additions & 4 deletions dfdatetime/fat_date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
13 changes: 12 additions & 1 deletion dfdatetime/filetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 15 additions & 11 deletions dfdatetime/golang_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
14 changes: 12 additions & 2 deletions dfdatetime/hfs_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Loading
Loading