| 1 |
"""RFC2579 date-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 RFC2579DateTime(interface.DateTimeValues): |
| 11 |
"""RFC2579 date-time. |
| 12 |
|
| 13 |
The RFC2579 date-time structure is 11 bytes of size and contains: |
| 14 |
|
| 15 |
struct { |
| 16 |
uin16_t year, |
| 17 |
uint8_t month, |
| 18 |
uint8_t day_of_month, |
| 19 |
uint8_t hours, |
| 20 |
uint8_t minutes, |
| 21 |
uint8_t seconds, |
| 22 |
uint8_t deciseconds, |
| 23 |
char direction_from_utc, |
| 24 |
uint8_t hours_from_utc, |
| 25 |
uint8_t minutes_from_utc |
| 26 |
} |
| 27 |
|
| 28 |
Also see: |
| 29 |
https://datatracker.ietf.org/doc/html/rfc2579 |
| 30 |
|
| 31 |
Attributes: |
| 32 |
year (int): year, 0 through 65536. |
| 33 |
month (int): month of year, 1 through 12. |
| 34 |
day_of_month (int): day of month, 1 through 31. |
| 35 |
hours (int): hours, 0 through 23. |
| 36 |
minutes (int): minutes, 0 through 59. |
| 37 |
seconds (int): seconds, 0 through 59, where 60 is used to represent |
| 38 |
a leap-second. |
| 39 |
deciseconds (int): deciseconds, 0 through 9. |
| 40 |
""" |
| 41 |
|
| 42 |
# TODO: make attributes read-only. |
| 43 |
|
| 44 |
# pylint: disable=missing-type-doc |
| 45 |
|
| 46 |
def __init__(self, precision=None, rfc2579_date_time_tuple=None): |
| 47 |
"""Initializes a RFC2579 date-time. |
| 48 |
|
| 49 |
Args: |
| 50 |
precision (Optional[str]): precision of the date and time value, which |
| 51 |
should be one of the PRECISION_VALUES in definitions. |
| 52 |
rfc2579_date_time_tuple: |
| 53 |
(Optional[tuple[int, int, int, int, int, int, int, str, int, int]]): |
| 54 |
RFC2579 date-time time, contains year, month, day of month, hours, |
| 55 |
minutes, seconds and deciseconds, and time zone offset in hours and |
| 56 |
minutes from UTC. |
| 57 |
|
| 58 |
Raises: |
| 59 |
ValueError: if the system time is invalid. |
| 60 |
""" |
| 61 |
super().__init__(precision=precision or definitions.PRECISION_100_MILLISECONDS) |
| 62 |
self._day_of_month = None |
| 63 |
self._deciseconds = None |
| 64 |
self._hours = None |
| 65 |
self._minutes = None |
| 66 |
self._month = None |
| 67 |
self._number_of_seconds = None |
| 68 |
self._seconds = None |
| 69 |
self._year = None |
| 70 |
|
| 71 |
if rfc2579_date_time_tuple: |
| 72 |
if len(rfc2579_date_time_tuple) < 10: |
| 73 |
raise ValueError( |
| 74 |
"Invalid RFC2579 date-time tuple 10 elements required." |
| 75 |
) |
| 76 |
|
| 77 |
if rfc2579_date_time_tuple[0] < 0 or rfc2579_date_time_tuple[0] > 65536: |
| 78 |
raise ValueError("Year value out of bounds.") |
| 79 |
|
| 80 |
if rfc2579_date_time_tuple[1] not in range(1, 13): |
| 81 |
raise ValueError("Month value out of bounds.") |
| 82 |
|
| 83 |
days_per_month = self._GetDaysPerMonth( |
| 84 |
rfc2579_date_time_tuple[0], rfc2579_date_time_tuple[1] |
| 85 |
) |
| 86 |
if ( |
| 87 |
rfc2579_date_time_tuple[2] < 1 |
| 88 |
or rfc2579_date_time_tuple[2] > days_per_month |
| 89 |
): |
| 90 |
raise ValueError("Day of month value out of bounds.") |
| 91 |
|
| 92 |
if rfc2579_date_time_tuple[3] not in range(0, 24): |
| 93 |
raise ValueError("Hours value out of bounds.") |
| 94 |
|
| 95 |
if rfc2579_date_time_tuple[4] not in range(0, 60): |
| 96 |
raise ValueError("Minutes value out of bounds.") |
| 97 |
|
| 98 |
# TODO: support a leap second? |
| 99 |
if rfc2579_date_time_tuple[5] not in range(0, 60): |
| 100 |
raise ValueError("Seconds value out of bounds.") |
| 101 |
|
| 102 |
if rfc2579_date_time_tuple[6] < 0 or rfc2579_date_time_tuple[6] > 9: |
| 103 |
raise ValueError("Deciseconds value out of bounds.") |
| 104 |
|
| 105 |
if rfc2579_date_time_tuple[7] not in ("+", "-"): |
| 106 |
raise ValueError("Direction from UTC value out of bounds.") |
| 107 |
|
| 108 |
if rfc2579_date_time_tuple[8] not in range(0, 14): |
| 109 |
raise ValueError("Hours from UTC value out of bounds.") |
| 110 |
|
| 111 |
if rfc2579_date_time_tuple[9] not in range(0, 60): |
| 112 |
raise ValueError("Minutes from UTC value out of bounds.") |
| 113 |
|
| 114 |
time_zone_offset = ( |
| 115 |
rfc2579_date_time_tuple[8] * 60 |
| 116 |
) + rfc2579_date_time_tuple[9] |
| 117 |
|
| 118 |
if rfc2579_date_time_tuple[7] == "-": |
| 119 |
time_zone_offset = -time_zone_offset |
| 120 |
|
| 121 |
self._time_zone_offset = time_zone_offset |
| 122 |
|
| 123 |
self._year = rfc2579_date_time_tuple[0] |
| 124 |
self._month = rfc2579_date_time_tuple[1] |
| 125 |
self._day_of_month = rfc2579_date_time_tuple[2] |
| 126 |
self._hours = rfc2579_date_time_tuple[3] |
| 127 |
self._minutes = rfc2579_date_time_tuple[4] |
| 128 |
self._seconds = rfc2579_date_time_tuple[5] |
| 129 |
self._deciseconds = rfc2579_date_time_tuple[6] |
| 130 |
|
| 131 |
self._number_of_seconds = self._GetNumberOfSecondsFromElements( |
| 132 |
self._year, |
| 133 |
self._month, |
| 134 |
self._day_of_month, |
| 135 |
self._hours, |
| 136 |
self._minutes, |
| 137 |
self._seconds, |
| 138 |
) |
| 139 |
|
| 140 |
def _GetNormalizedTimestamp(self): |
| 141 |
"""Retrieves the normalized timestamp. |
| 142 |
|
| 143 |
Returns: |
| 144 |
decimal.Decimal: normalized timestamp, which contains the number of |
| 145 |
seconds since January 1, 1970 00:00:00 and a fraction of second used |
| 146 |
for increased precision, or None if the normalized timestamp cannot be |
| 147 |
determined. |
| 148 |
""" |
| 149 |
if self._normalized_timestamp is None: |
| 150 |
if self._number_of_seconds is not None: |
| 151 |
self._normalized_timestamp = ( |
| 152 |
decimal.Decimal(self._deciseconds) |
| 153 |
/ definitions.DECISECONDS_PER_SECOND |
| 154 |
) |
| 155 |
self._normalized_timestamp += decimal.Decimal(self._number_of_seconds) |
| 156 |
|
| 157 |
if self._time_zone_offset: |
| 158 |
self._normalized_timestamp -= self._time_zone_offset * 60 |
| 159 |
|
| 160 |
return self._normalized_timestamp |
| 161 |
|
| 162 |
@property |
| 163 |
def deciseconds(self): |
| 164 |
"""int: number of deciseconds or None if not set.""" |
| 165 |
return self._deciseconds |
| 166 |
|
| 167 |
@property |
| 168 |
def day_of_month(self): |
| 169 |
"""int: day of month or None if not set.""" |
| 170 |
return self._day_of_month |
| 171 |
|
| 172 |
@property |
| 173 |
def hours(self): |
| 174 |
"""int: number of hours or None if not set.""" |
| 175 |
return self._hours |
| 176 |
|
| 177 |
@property |
| 178 |
def minutes(self): |
| 179 |
"""int: number of minutes or None if not set.""" |
| 180 |
return self._minutes |
| 181 |
|
| 182 |
@property |
| 183 |
def month(self): |
| 184 |
"""int: month or None if not set.""" |
| 185 |
return self._month |
| 186 |
|
| 187 |
@property |
| 188 |
def seconds(self): |
| 189 |
"""int: number of seconds or None if not set.""" |
| 190 |
return self._seconds |
| 191 |
|
| 192 |
@property |
| 193 |
def year(self): |
| 194 |
"""int: year or None if not set.""" |
| 195 |
return self._year |
| 196 |
|
| 197 |
def CopyFromDateTimeString(self, time_string): |
| 198 |
"""Copies a RFC2579 date-time from a date and time string. |
| 199 |
|
| 200 |
Args: |
| 201 |
time_string (str): date and time value formatted as: |
| 202 |
YYYY-MM-DD hh:mm:ss.######[+-]##:## |
| 203 |
|
| 204 |
Where # are numeric digits ranging from 0 to 9 and the seconds |
| 205 |
fraction can be either 3, 6 or 9 digits. The time of day, seconds |
| 206 |
fraction and time zone offset are optional. The default time zone |
| 207 |
is UTC. |
| 208 |
|
| 209 |
Raises: |
| 210 |
ValueError: if the date string is invalid or not supported. |
| 211 |
""" |
| 212 |
date_time_values = self._CopyDateTimeFromString(time_string) |
| 213 |
|
| 214 |
year = date_time_values.get("year", 0) |
| 215 |
month = date_time_values.get("month", 0) |
| 216 |
day_of_month = date_time_values.get("day_of_month", 0) |
| 217 |
hours = date_time_values.get("hours", 0) |
| 218 |
minutes = date_time_values.get("minutes", 0) |
| 219 |
seconds = date_time_values.get("seconds", 0) |
| 220 |
nanoseconds = date_time_values.get("nanoseconds", 0) |
| 221 |
time_zone_offset = date_time_values.get("time_zone_offset") |
| 222 |
|
| 223 |
deciseconds, _ = divmod(nanoseconds, definitions.NANOSECONDS_PER_DECISECOND) |
| 224 |
|
| 225 |
if year < 0 or year > 65536: |
| 226 |
raise ValueError(f"Unsupported year value: {year:d}.") |
| 227 |
|
| 228 |
self._normalized_timestamp = None |
| 229 |
self._number_of_seconds = self._GetNumberOfSecondsFromElements( |
| 230 |
year, month, day_of_month, hours, minutes, seconds |
| 231 |
) |
| 232 |
self._time_zone_offset = time_zone_offset |
| 233 |
|
| 234 |
self._year = year |
| 235 |
self._month = month |
| 236 |
self._day_of_month = day_of_month |
| 237 |
self._hours = hours |
| 238 |
self._minutes = minutes |
| 239 |
self._seconds = seconds |
| 240 |
self._deciseconds = deciseconds |
| 241 |
|
| 242 |
def CopyToDateTimeString(self): |
| 243 |
"""Copies the RFC2579 date-time to a date and time string. |
| 244 |
|
| 245 |
Returns: |
| 246 |
str: date and time value formatted as: "YYYY-MM-DD hh:mm:ss.#" or |
| 247 |
None if the number of seconds is missing. |
| 248 |
""" |
| 249 |
if self._number_of_seconds is None: |
| 250 |
return None |
| 251 |
|
| 252 |
return ( |
| 253 |
f"{self._year:04d}-{self._month:02d}-{self._day_of_month:02d} " |
| 254 |
f"{self._hours:02d}:{self._minutes:02d}:{self._seconds:02d}" |
| 255 |
f".{self._deciseconds:01d}" |
| 256 |
) |
| 257 |
|
| 258 |
def CopyToSerializableDict(self): |
| 259 |
"""Copies the date time value to a serializable dictionary. |
| 260 |
|
| 261 |
Returns: |
| 262 |
dict[str, object]: serializable dictionary. |
| 263 |
""" |
| 264 |
time_zone_hours, time_zone_minutes = divmod(self._time_zone_offset, 60) |
| 265 |
if self._time_zone_offset < 0: |
| 266 |
time_zone_sign = "-" |
| 267 |
time_zone_hours *= -1 |
| 268 |
else: |
| 269 |
time_zone_sign = "+" |
| 270 |
|
| 271 |
return { |
| 272 |
"__class_name__": type(self).__name__, |
| 273 |
"__type__": "DateTimeValues", |
| 274 |
"rfc2579_date_time_tuple": ( |
| 275 |
self._year, |
| 276 |
self._month, |
| 277 |
self._day_of_month, |
| 278 |
self._hours, |
| 279 |
self._minutes, |
| 280 |
self._seconds, |
| 281 |
self._deciseconds, |
| 282 |
time_zone_sign, |
| 283 |
time_zone_hours, |
| 284 |
time_zone_minutes, |
| 285 |
), |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
factory.Factory.RegisterDateTimeValues(RFC2579DateTime) |