OLD | NEW |
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 """POSIX timestamp implementation.""" | 2 """POSIX timestamp implementation.""" |
3 | 3 |
4 from __future__ import unicode_literals | 4 from __future__ import unicode_literals |
5 | 5 |
| 6 import decimal |
| 7 |
6 from dfdatetime import definitions | 8 from dfdatetime import definitions |
7 from dfdatetime import interface | 9 from dfdatetime import interface |
8 | 10 |
9 | 11 |
10 class PosixTimeEpoch(interface.DateTimeEpoch): | 12 class PosixTimeEpoch(interface.DateTimeEpoch): |
11 """POSIX time epoch.""" | 13 """POSIX time epoch.""" |
12 | 14 |
13 def __init__(self): | 15 def __init__(self): |
14 """Initializes a POSIX time epoch.""" | 16 """Initializes a POSIX time epoch.""" |
15 super(PosixTimeEpoch, self).__init__(1970, 1, 1) | 17 super(PosixTimeEpoch, self).__init__(1970, 1, 1) |
(...skipping 29 matching lines...) Expand all Loading... |
45 | 47 |
46 @property | 48 @property |
47 def timestamp(self): | 49 def timestamp(self): |
48 """int: POSIX timestamp or None if timestamp is not set.""" | 50 """int: POSIX timestamp or None if timestamp is not set.""" |
49 return self._timestamp | 51 return self._timestamp |
50 | 52 |
51 def _GetNormalizedTimestamp(self): | 53 def _GetNormalizedTimestamp(self): |
52 """Retrieves the normalized timestamp. | 54 """Retrieves the normalized timestamp. |
53 | 55 |
54 Returns: | 56 Returns: |
55 float: normalized timestamp, which contains the number of seconds since | 57 decimal.Decimal: normalized timestamp, which contains the number of |
56 January 1, 1970 00:00:00 and a fraction of second used for increased | 58 seconds since January 1, 1970 00:00:00 and a fraction of second used |
57 precision, or None if the normalized timestamp cannot be determined. | 59 for increased precision, or None if the normalized timestamp cannot be |
| 60 determined. |
58 """ | 61 """ |
59 if self._normalized_timestamp is None: | 62 if self._normalized_timestamp is None: |
60 if self._timestamp is not None: | 63 if self._timestamp is not None: |
61 self._normalized_timestamp = float(self._timestamp) | 64 self._SetNormalizedTimestamp(decimal.Decimal(self._timestamp)) |
62 | 65 |
63 return self._normalized_timestamp | 66 return self._normalized_timestamp |
64 | 67 |
65 def CopyFromDateTimeString(self, time_string): | 68 def CopyFromDateTimeString(self, time_string): |
66 """Copies a POSIX timestamp from a date and time string. | 69 """Copies a POSIX timestamp from a date and time string. |
67 | 70 |
68 Args: | 71 Args: |
69 time_string (str): date and time value formatted as: | 72 time_string (str): date and time value formatted as: |
70 YYYY-MM-DD hh:mm:ss.######[+-]##:## | 73 YYYY-MM-DD hh:mm:ss.######[+-]##:## |
71 | 74 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 153 |
151 @property | 154 @property |
152 def timestamp(self): | 155 def timestamp(self): |
153 """int: POSIX timestamp in microseconds or None if timestamp is not set.""" | 156 """int: POSIX timestamp in microseconds or None if timestamp is not set.""" |
154 return self._timestamp | 157 return self._timestamp |
155 | 158 |
156 def _GetNormalizedTimestamp(self): | 159 def _GetNormalizedTimestamp(self): |
157 """Retrieves the normalized timestamp. | 160 """Retrieves the normalized timestamp. |
158 | 161 |
159 Returns: | 162 Returns: |
160 float: normalized timestamp, which contains the number of seconds since | 163 decimal.Decimal: normalized timestamp, which contains the number of |
161 January 1, 1970 00:00:00 and a fraction of second used for increased | 164 seconds since January 1, 1970 00:00:00 and a fraction of second used |
162 precision, or None if the normalized timestamp cannot be determined. | 165 for increased precision, or None if the normalized timestamp cannot be |
| 166 determined. |
163 """ | 167 """ |
164 if self._normalized_timestamp is None: | 168 if self._normalized_timestamp is None: |
165 if self._timestamp is not None: | 169 if self._timestamp is not None: |
166 self._normalized_timestamp = ( | 170 self._SetNormalizedTimestamp(decimal.Decimal(self._timestamp) / |
167 float(self._timestamp) / definitions.MICROSECONDS_PER_SECOND) | 171 definitions.MICROSECONDS_PER_SECOND) |
168 | 172 |
169 return self._normalized_timestamp | 173 return self._normalized_timestamp |
170 | 174 |
171 def CopyFromDateTimeString(self, time_string): | 175 def CopyFromDateTimeString(self, time_string): |
172 """Copies a POSIX timestamp from a date and time string. | 176 """Copies a POSIX timestamp from a date and time string. |
173 | 177 |
174 Args: | 178 Args: |
175 time_string (str): date and time value formatted as: | 179 time_string (str): date and time value formatted as: |
176 YYYY-MM-DD hh:mm:ss.######[+-]##:## | 180 YYYY-MM-DD hh:mm:ss.######[+-]##:## |
177 | 181 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 if self.timestamp is None: | 232 if self.timestamp is None: |
229 return None, None, None | 233 return None, None, None |
230 | 234 |
231 try: | 235 try: |
232 timestamp, _ = divmod(self.timestamp, definitions.MICROSECONDS_PER_SECOND) | 236 timestamp, _ = divmod(self.timestamp, definitions.MICROSECONDS_PER_SECOND) |
233 number_of_days, _, _, _ = self._GetTimeValues(timestamp) | 237 number_of_days, _, _, _ = self._GetTimeValues(timestamp) |
234 return self._GetDateValuesWithEpoch(number_of_days, self._EPOCH) | 238 return self._GetDateValuesWithEpoch(number_of_days, self._EPOCH) |
235 | 239 |
236 except ValueError: | 240 except ValueError: |
237 return None, None, None | 241 return None, None, None |
OLD | NEW |