Timestamp represents the Timestamp extension type in msgpack. When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`. When using pure-Python msgpack, :func:`to_bytes` and :func:`from_bytes` are used to pack and unpack `Timestamp`. This class is immutable: Do
| 17 | |
| 18 | |
| 19 | class Timestamp: |
| 20 | """Timestamp represents the Timestamp extension type in msgpack. |
| 21 | |
| 22 | When built with Cython, msgpack uses C methods to pack and unpack `Timestamp`. |
| 23 | When using pure-Python msgpack, :func:`to_bytes` and :func:`from_bytes` are used to pack and |
| 24 | unpack `Timestamp`. |
| 25 | |
| 26 | This class is immutable: Do not override seconds and nanoseconds. |
| 27 | """ |
| 28 | |
| 29 | __slots__ = ["seconds", "nanoseconds"] |
| 30 | |
| 31 | def __init__(self, seconds, nanoseconds=0): |
| 32 | """Initialize a Timestamp object. |
| 33 | |
| 34 | :param int seconds: |
| 35 | Number of seconds since the UNIX epoch (00:00:00 UTC Jan 1 1970, minus leap seconds). |
| 36 | May be negative. |
| 37 | |
| 38 | :param int nanoseconds: |
| 39 | Number of nanoseconds to add to `seconds` to get fractional time. |
| 40 | Maximum is 999_999_999. Default is 0. |
| 41 | |
| 42 | Note: Negative times (before the UNIX epoch) are represented as neg. seconds + pos. ns. |
| 43 | """ |
| 44 | if not isinstance(seconds, int): |
| 45 | raise TypeError("seconds must be an integer") |
| 46 | if not isinstance(nanoseconds, int): |
| 47 | raise TypeError("nanoseconds must be an integer") |
| 48 | if not (0 <= nanoseconds < 10**9): |
| 49 | raise ValueError("nanoseconds must be a non-negative integer less than 999999999.") |
| 50 | self.seconds = seconds |
| 51 | self.nanoseconds = nanoseconds |
| 52 | |
| 53 | def __repr__(self): |
| 54 | """String representation of Timestamp.""" |
| 55 | return f"Timestamp(seconds={self.seconds}, nanoseconds={self.nanoseconds})" |
| 56 | |
| 57 | def __eq__(self, other): |
| 58 | """Check for equality with another Timestamp object""" |
| 59 | if type(other) is self.__class__: |
| 60 | return self.seconds == other.seconds and self.nanoseconds == other.nanoseconds |
| 61 | return False |
| 62 | |
| 63 | def __ne__(self, other): |
| 64 | """not-equals method (see :func:`__eq__()`)""" |
| 65 | return not self.__eq__(other) |
| 66 | |
| 67 | def __hash__(self): |
| 68 | return hash((self.seconds, self.nanoseconds)) |
| 69 | |
| 70 | @staticmethod |
| 71 | def from_bytes(b): |
| 72 | """Unpack bytes into a `Timestamp` object. |
| 73 | |
| 74 | Used for pure-Python msgpack unpacking. |
| 75 | |
| 76 | :param b: Payload from msgpack ext message with code -1 |
no outgoing calls
no test coverage detected
searching dependent graphs…