blob: 590302552fd6c4bf383c87f2cd010fa580e3c6ba (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# -*- coding: utf-8 -*-
#
# SPDX-FileCopyrightText: © 2018 The glucometerutils Authors
# SPDX-License-Identifier: MIT
"""Extra classes for Construct."""
import datetime
import construct
class Timestamp(construct.Adapter):
"""Adapter for converting datetime object into timestamps.
Take two parameters: the subcon object to output the resulting timestamp as,
and an optional epoch offset to the UNIX Epoch.
"""
__slots__ = ["epoch"]
def __init__(self, subcon, epoch: int = 0) -> None:
super(Timestamp, self).__init__(subcon)
self.epoch = epoch
def _encode(self, obj: datetime.datetime, context, path) -> int:
assert isinstance(obj, datetime.datetime)
epoch_date = datetime.datetime.utcfromtimestamp(self.epoch)
delta = obj - epoch_date
return int(delta.total_seconds())
def _decode(self, obj: int, context, path) -> datetime.datetime:
return datetime.datetime.utcfromtimestamp(obj + self.epoch)
|