|
32 | 32 | ) |
33 | 33 |
|
34 | 34 | ANY_ARRAY = 2277 |
| 35 | +ABSTIME = 702 |
35 | 36 | BIGINT = 20 |
36 | 37 | BIGINT_ARRAY = 1016 |
37 | 38 | BOOLEAN = 16 |
@@ -209,6 +210,33 @@ def timestamp_recv_integer(data: bytes, offset: int, length: int) -> typing.Unio |
209 | 210 | return Datetime.max |
210 | 211 |
|
211 | 212 |
|
| 213 | +def abstime_recv(data: bytes, offset: int, length: int) -> Datetime: |
| 214 | + """ |
| 215 | + Converts abstime values represented as integer, representing seconds, to Python datetime.Datetime using UTC. |
| 216 | + """ |
| 217 | + if length != 4: |
| 218 | + raise Exception("Malformed column value of type abstime received") |
| 219 | + |
| 220 | + time: float = i_unpack(data, offset)[0] |
| 221 | + time *= 1000000 # Time in micro secs |
| 222 | + secs: float = time / 1000000 |
| 223 | + nanos: int = int(time - secs * 1000000) |
| 224 | + if nanos < 0: |
| 225 | + secs -= 1 |
| 226 | + nanos += 1000000 |
| 227 | + |
| 228 | + nanos *= 1000 |
| 229 | + |
| 230 | + millis: float = secs * float(1000) |
| 231 | + |
| 232 | + total_secs: float = (millis / 1e3) + (nanos / 1e9) |
| 233 | + server_date: Datetime = Datetime.fromtimestamp(total_secs) |
| 234 | + # As of Version 3.0, times are no longer read and written using Greenwich Mean Time; |
| 235 | + # the input and output routines default to the local time zone. |
| 236 | + # Ref https://www.postgresql.org/docs/6.3/c0804.htm#abstime |
| 237 | + return server_date.astimezone(Timezone.utc) |
| 238 | + |
| 239 | + |
212 | 240 | # data is 64-bit integer representing microseconds since 2000-01-01 |
213 | 241 | def timestamp_send_integer(v: Datetime) -> bytes: |
214 | 242 | return q_pack(int((timegm(v.timetuple()) - EPOCH_SECONDS) * 1e6) + v.microsecond) |
@@ -588,6 +616,7 @@ def varbytehex_recv(data: bytes, idx: int, length: int) -> str: |
588 | 616 | pg_types: typing.DefaultDict[int, typing.Tuple[int, typing.Callable]] = defaultdict( |
589 | 617 | lambda: (FC_TEXT, text_recv), |
590 | 618 | { |
| 619 | + ABSTIME: (FC_BINARY, abstime_recv), # abstime |
591 | 620 | BOOLEAN: (FC_BINARY, bool_recv), # boolean |
592 | 621 | # 17: (FC_BINARY, bytea_recv), # bytea |
593 | 622 | NAME: (FC_BINARY, text_recv), # name type |
|
0 commit comments