edq.util.time

 1import datetime
 2import time
 3
 4PRETTY_SHORT_FORMAT: str = '%Y-%m-%d %H:%M'
 5
 6class Duration(int):
 7    """
 8    A Duration represents some length in time in milliseconds.
 9    """
10
11    def to_secs(self) -> float:
12        """ Convert the duration to float seconds. """
13
14        return self / 1000.0
15
16    def to_msecs(self) -> int:
17        """ Convert the duration to integer milliseconds. """
18
19        return self
20
21class Timestamp(int):
22    """
23    A Timestamp represent a moment in time (sometimes called "datetimes").
24    Timestamps are internally represented by the number of milliseconds since the
25    (Unix Epoch)[https://en.wikipedia.org/wiki/Unix_time].
26    This is sometimes referred to as "Unix Time".
27    Since Unix Time is in UTC, timestamps do not need to carry timestamp information with them.
28
29    Note that timestamps are just integers with some decoration,
30    so they respond to all normal int functionality.
31    """
32
33    def sub(self, other: 'Timestamp') -> Duration:
34        """ Return a new duration that is the difference of this and the given duration. """
35
36        return Duration(self - other)
37
38    def to_pytime(self, timezone: datetime.timezone = datetime.timezone.utc) -> datetime.datetime:
39        """ Convert this timestamp to a Python datetime in the given timezone (UTC by default). """
40
41        return datetime.datetime.fromtimestamp(self / 1000, timezone)
42
43    def to_local_pytime(self) -> datetime.datetime:
44        """ Convert this timestamp to a Python datetime in the system timezone. """
45
46        local_timezone = datetime.datetime.now().astimezone().tzinfo
47        if ((local_timezone is None) or (not isinstance(local_timezone, datetime.timezone))):
48            raise ValueError("Could not discover local timezone.")
49
50        return self.to_pytime(timezone = local_timezone)
51
52    def pretty(self, short: bool = False, timezone: datetime.timezone = datetime.timezone.utc) -> str:
53        """
54        Get a "pretty" string representation of this timestamp.
55        There is no guarantee that this representation can be parsed back to its original form.
56        """
57
58        pytime = self.to_pytime(timezone = timezone)
59
60        if (short):
61            return pytime.strftime(PRETTY_SHORT_FORMAT)
62
63        return pytime.isoformat(timespec = 'milliseconds')
64
65    @staticmethod
66    def from_pytime(pytime: datetime.datetime) -> 'Timestamp':
67        """ Convert a Python datetime to a timestamp. """
68
69        return Timestamp(int(pytime.timestamp() * 1000))
70
71    @staticmethod
72    def now() -> 'Timestamp':
73        """ Get a Timestamp that represents the current moment. """
74
75        return Timestamp(time.time() * 1000)
PRETTY_SHORT_FORMAT: str = '%Y-%m-%d %H:%M'
class Duration(builtins.int):
 7class Duration(int):
 8    """
 9    A Duration represents some length in time in milliseconds.
10    """
11
12    def to_secs(self) -> float:
13        """ Convert the duration to float seconds. """
14
15        return self / 1000.0
16
17    def to_msecs(self) -> int:
18        """ Convert the duration to integer milliseconds. """
19
20        return self

A Duration represents some length in time in milliseconds.

def to_secs(self) -> float:
12    def to_secs(self) -> float:
13        """ Convert the duration to float seconds. """
14
15        return self / 1000.0

Convert the duration to float seconds.

def to_msecs(self) -> int:
17    def to_msecs(self) -> int:
18        """ Convert the duration to integer milliseconds. """
19
20        return self

Convert the duration to integer milliseconds.

class Timestamp(builtins.int):
22class Timestamp(int):
23    """
24    A Timestamp represent a moment in time (sometimes called "datetimes").
25    Timestamps are internally represented by the number of milliseconds since the
26    (Unix Epoch)[https://en.wikipedia.org/wiki/Unix_time].
27    This is sometimes referred to as "Unix Time".
28    Since Unix Time is in UTC, timestamps do not need to carry timestamp information with them.
29
30    Note that timestamps are just integers with some decoration,
31    so they respond to all normal int functionality.
32    """
33
34    def sub(self, other: 'Timestamp') -> Duration:
35        """ Return a new duration that is the difference of this and the given duration. """
36
37        return Duration(self - other)
38
39    def to_pytime(self, timezone: datetime.timezone = datetime.timezone.utc) -> datetime.datetime:
40        """ Convert this timestamp to a Python datetime in the given timezone (UTC by default). """
41
42        return datetime.datetime.fromtimestamp(self / 1000, timezone)
43
44    def to_local_pytime(self) -> datetime.datetime:
45        """ Convert this timestamp to a Python datetime in the system timezone. """
46
47        local_timezone = datetime.datetime.now().astimezone().tzinfo
48        if ((local_timezone is None) or (not isinstance(local_timezone, datetime.timezone))):
49            raise ValueError("Could not discover local timezone.")
50
51        return self.to_pytime(timezone = local_timezone)
52
53    def pretty(self, short: bool = False, timezone: datetime.timezone = datetime.timezone.utc) -> str:
54        """
55        Get a "pretty" string representation of this timestamp.
56        There is no guarantee that this representation can be parsed back to its original form.
57        """
58
59        pytime = self.to_pytime(timezone = timezone)
60
61        if (short):
62            return pytime.strftime(PRETTY_SHORT_FORMAT)
63
64        return pytime.isoformat(timespec = 'milliseconds')
65
66    @staticmethod
67    def from_pytime(pytime: datetime.datetime) -> 'Timestamp':
68        """ Convert a Python datetime to a timestamp. """
69
70        return Timestamp(int(pytime.timestamp() * 1000))
71
72    @staticmethod
73    def now() -> 'Timestamp':
74        """ Get a Timestamp that represents the current moment. """
75
76        return Timestamp(time.time() * 1000)

A Timestamp represent a moment in time (sometimes called "datetimes"). Timestamps are internally represented by the number of milliseconds since the (Unix Epoch)[https://en.wikipedia.org/wiki/Unix_time]. This is sometimes referred to as "Unix Time". Since Unix Time is in UTC, timestamps do not need to carry timestamp information with them.

Note that timestamps are just integers with some decoration, so they respond to all normal int functionality.

def sub(self, other: Timestamp) -> Duration:
34    def sub(self, other: 'Timestamp') -> Duration:
35        """ Return a new duration that is the difference of this and the given duration. """
36
37        return Duration(self - other)

Return a new duration that is the difference of this and the given duration.

def to_pytime( self, timezone: datetime.timezone = datetime.timezone.utc) -> datetime.datetime:
39    def to_pytime(self, timezone: datetime.timezone = datetime.timezone.utc) -> datetime.datetime:
40        """ Convert this timestamp to a Python datetime in the given timezone (UTC by default). """
41
42        return datetime.datetime.fromtimestamp(self / 1000, timezone)

Convert this timestamp to a Python datetime in the given timezone (UTC by default).

def to_local_pytime(self) -> datetime.datetime:
44    def to_local_pytime(self) -> datetime.datetime:
45        """ Convert this timestamp to a Python datetime in the system timezone. """
46
47        local_timezone = datetime.datetime.now().astimezone().tzinfo
48        if ((local_timezone is None) or (not isinstance(local_timezone, datetime.timezone))):
49            raise ValueError("Could not discover local timezone.")
50
51        return self.to_pytime(timezone = local_timezone)

Convert this timestamp to a Python datetime in the system timezone.

def pretty( self, short: bool = False, timezone: datetime.timezone = datetime.timezone.utc) -> str:
53    def pretty(self, short: bool = False, timezone: datetime.timezone = datetime.timezone.utc) -> str:
54        """
55        Get a "pretty" string representation of this timestamp.
56        There is no guarantee that this representation can be parsed back to its original form.
57        """
58
59        pytime = self.to_pytime(timezone = timezone)
60
61        if (short):
62            return pytime.strftime(PRETTY_SHORT_FORMAT)
63
64        return pytime.isoformat(timespec = 'milliseconds')

Get a "pretty" string representation of this timestamp. There is no guarantee that this representation can be parsed back to its original form.

@staticmethod
def from_pytime(pytime: datetime.datetime) -> Timestamp:
66    @staticmethod
67    def from_pytime(pytime: datetime.datetime) -> 'Timestamp':
68        """ Convert a Python datetime to a timestamp. """
69
70        return Timestamp(int(pytime.timestamp() * 1000))

Convert a Python datetime to a timestamp.

@staticmethod
def now() -> Timestamp:
72    @staticmethod
73    def now() -> 'Timestamp':
74        """ Get a Timestamp that represents the current moment. """
75
76        return Timestamp(time.time() * 1000)

Get a Timestamp that represents the current moment.