dfdatetime 20260730__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,66 @@
1
+ """Java java.util.Date timestamp implementation."""
2
+
3
+ import decimal
4
+
5
+ from dfdatetime import definitions
6
+ from dfdatetime import factory
7
+ from dfdatetime import posix_time
8
+
9
+
10
+ class JavaTime(posix_time.PosixTimeInMilliseconds):
11
+ """Java java.util.Date timestamp.
12
+
13
+ The Java java.util.Date timestamp is a signed integer that contains the
14
+ number of milliseconds since 1970-01-01 00:00:00 (also known as the POSIX
15
+ epoch). Negative values represent date and times predating the POSIX epoch.
16
+
17
+ Also see:
18
+ https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
19
+
20
+ Attributes:
21
+ is_local_time (bool): True if the date and time value is in local time.
22
+ """
23
+
24
+ def _GetNormalizedTimestamp(self):
25
+ """Retrieves the normalized timestamp.
26
+
27
+ Returns:
28
+ decimal.Decimal: normalized timestamp, which contains the number of
29
+ seconds since January 1, 1970 00:00:00 and a fraction of second used
30
+ for increased precision, or None if the normalized timestamp cannot be
31
+ determined.
32
+ """
33
+ if self._normalized_timestamp is None:
34
+ if (
35
+ self._timestamp is not None
36
+ and self._timestamp >= self._INT64_MIN
37
+ and self._timestamp <= self._INT64_MAX
38
+ ):
39
+ self._normalized_timestamp = (
40
+ decimal.Decimal(self._timestamp)
41
+ / definitions.MILLISECONDS_PER_SECOND
42
+ )
43
+
44
+ if self._time_zone_offset:
45
+ self._normalized_timestamp -= self._time_zone_offset * 60
46
+
47
+ return self._normalized_timestamp
48
+
49
+ def CopyToDateTimeString(self):
50
+ """Copies the POSIX timestamp to a date and time string.
51
+
52
+ Returns:
53
+ str: date and time value formatted as: "YYYY-MM-DD hh:mm:ss.######" or
54
+ None if the timestamp is missing.
55
+ """
56
+ if (
57
+ self._timestamp is None
58
+ or self._timestamp < self._INT64_MIN
59
+ or self._timestamp > self._INT64_MAX
60
+ ):
61
+ return None
62
+
63
+ return super().CopyToDateTimeString()
64
+
65
+
66
+ factory.Factory.RegisterDateTimeValues(JavaTime)
@@ -0,0 +1,158 @@
1
+ """OLE automation date (or Floatingtime or Application time) implementation."""
2
+
3
+ import decimal
4
+
5
+ from dfdatetime import definitions
6
+ from dfdatetime import factory
7
+ from dfdatetime import interface
8
+
9
+
10
+ class OLEAutomationDateEpoch(interface.DateTimeEpoch):
11
+ """OLE automation date epoch."""
12
+
13
+ def __init__(self):
14
+ """Initializes a OLE automation date epoch."""
15
+ super().__init__(1899, 12, 30)
16
+
17
+
18
+ class OLEAutomationDate(interface.DateTimeValues):
19
+ """OLE Automation date.
20
+
21
+ The OLE Automation date is a floating point value that contains the number of
22
+ days since 1899-12-30 (also known as the OLE Automation date epoch), and the
23
+ fractional part represents the fraction of a day since midnight. Negative
24
+ values represent date and times predating the OLE Automation date epoch.
25
+
26
+ Also see:
27
+ https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tooadate?view=net-8.0
28
+
29
+ Attributes:
30
+ is_local_time (bool): True if the date and time value is in local time.
31
+ """
32
+
33
+ _EPOCH = OLEAutomationDateEpoch()
34
+
35
+ # The difference between December 30, 1899 and January 1, 1970 in days.
36
+ _OLE_AUTOMATION_DATE_TO_POSIX_BASE = 25569
37
+
38
+ def __init__(self, precision=None, time_zone_offset=None, timestamp=None):
39
+ """Initializes an OLE Automation date.
40
+
41
+ Args:
42
+ precision (Optional[str]): precision of the date and time value, which
43
+ should be one of the PRECISION_VALUES in definitions.
44
+ time_zone_offset (Optional[int]): time zone offset in number of minutes
45
+ from UTC or None if not set.
46
+ timestamp (Optional[float]): OLE Automation date.
47
+ """
48
+ super().__init__(
49
+ precision=precision or definitions.PRECISION_1_MICROSECOND,
50
+ time_zone_offset=time_zone_offset,
51
+ )
52
+ self._timestamp = timestamp
53
+
54
+ @property
55
+ def timestamp(self):
56
+ """float: OLE Automation date timestamp or None if not set."""
57
+ return self._timestamp
58
+
59
+ def _GetNormalizedTimestamp(self):
60
+ """Retrieves the normalized timestamp.
61
+
62
+ Returns:
63
+ decimal.Decimal: normalized timestamp, which contains the number of
64
+ seconds since January 1, 1970 00:00:00 and a fraction of second used
65
+ for increased precision, or None if the normalized timestamp cannot be
66
+ determined.
67
+ """
68
+ if self._normalized_timestamp is None:
69
+ if self._timestamp is not None:
70
+ self._normalized_timestamp = (
71
+ decimal.Decimal(self._timestamp)
72
+ - self._OLE_AUTOMATION_DATE_TO_POSIX_BASE
73
+ )
74
+ self._normalized_timestamp *= definitions.SECONDS_PER_DAY
75
+
76
+ if self._time_zone_offset:
77
+ self._normalized_timestamp -= self._time_zone_offset * 60
78
+
79
+ return self._normalized_timestamp
80
+
81
+ def CopyFromDateTimeString(self, time_string):
82
+ """Copies an OLE Automation date from a date and time string.
83
+
84
+ Args:
85
+ time_string (str): date and time value formatted as:
86
+ YYYY-MM-DD hh:mm:ss.######[+-]##:##
87
+
88
+ Where # are numeric digits ranging from 0 to 9 and the seconds
89
+ fraction can be either 3, 6 or 9 digits. The time of day, seconds
90
+ fraction and time zone offset are optional. The default time zone
91
+ is UTC.
92
+
93
+ Raises:
94
+ ValueError: if the time string is invalid or not supported.
95
+ """
96
+ date_time_values = self._CopyDateTimeFromString(time_string)
97
+
98
+ year = date_time_values.get("year", 0)
99
+ month = date_time_values.get("month", 0)
100
+ day_of_month = date_time_values.get("day_of_month", 0)
101
+ hours = date_time_values.get("hours", 0)
102
+ minutes = date_time_values.get("minutes", 0)
103
+ seconds = date_time_values.get("seconds", 0)
104
+ nanoseconds = date_time_values.get("nanoseconds", 0)
105
+ time_zone_offset = date_time_values.get("time_zone_offset")
106
+
107
+ timestamp = self._GetNumberOfSecondsFromElements(
108
+ year, month, day_of_month, hours, minutes, seconds
109
+ )
110
+
111
+ timestamp = float(timestamp)
112
+ timestamp += float(nanoseconds) / definitions.NANOSECONDS_PER_SECOND
113
+
114
+ timestamp /= definitions.SECONDS_PER_DAY
115
+ timestamp += self._OLE_AUTOMATION_DATE_TO_POSIX_BASE
116
+
117
+ self._normalized_timestamp = None
118
+ self._timestamp = timestamp
119
+ self._time_zone_offset = time_zone_offset
120
+
121
+ def CopyToDateTimeString(self):
122
+ """Copies the OLE Automation date to a date and time string.
123
+
124
+ Returns:
125
+ str: date and time value formatted as: "YYYY-MM-DD hh:mm:ss.######" or
126
+ None if the timestamp is missing.
127
+ """
128
+ if self._timestamp is None:
129
+ return None
130
+
131
+ timestamp = self._timestamp * definitions.SECONDS_PER_DAY
132
+
133
+ number_of_days, hours, minutes, seconds = self._GetTimeValues(int(timestamp))
134
+
135
+ year, month, day_of_month = self._GetDateValuesWithEpoch(
136
+ number_of_days, self._EPOCH
137
+ )
138
+ microseconds = int((timestamp % 1) * definitions.MICROSECONDS_PER_SECOND)
139
+
140
+ return (
141
+ f"{year:04d}-{month:02d}-{day_of_month:02d} "
142
+ f"{hours:02d}:{minutes:02d}:{seconds:02d}.{microseconds:06d}"
143
+ )
144
+
145
+ def CopyToSerializableDict(self):
146
+ """Copies the date time value to a serializable dictionary.
147
+
148
+ Returns:
149
+ dict[str, object]: serializable dictionary.
150
+ """
151
+ serializable_dict = self._CreateSerializableDict()
152
+
153
+ serializable_dict["timestamp"] = self._timestamp
154
+
155
+ return serializable_dict
156
+
157
+
158
+ factory.Factory.RegisterDateTimeValues(OLEAutomationDate)