def test_timestamp_coverts_bytes_to_unix_time_seconds(): """ Assert that :meth:`~ulid.ulid.Timestamp.timestamp` returns the value in Unix time in seconds from epoch. """ now_ms = int(time.time()) * 1000 timestamp = ulid.Timestamp(now_ms.to_bytes(6, byteorder='big')) assert timestamp.timestamp == now_ms / 1000.0
def test_timestamp_converts_to_datetime(): """ Assert that :meth:`~ulid.ulid.Timestamp.datetime` returns the value as a :class:`~datetime.dateime` instance. """ now_ms = int(time.time()) * 1000 timestamp = ulid.Timestamp(now_ms.to_bytes(6, byteorder='big')) assert timestamp.datetime == datetime.datetime.utcfromtimestamp(now_ms / 1000.0)
def test_decode_timestamp_timestamp_returns_timestamp_instance(valid_bytes_48): """ Assert that :func:`~ulid.codec.decode_timestamp` returns a new :class:`~ulid.ulid.Timestamp` instance from the given timestamp as a :class:`~ulid.ulid.Timestamp`. """ value = ulid.Timestamp(valid_bytes_48) instance = codec.decode_timestamp(value) assert isinstance(instance, ulid.Timestamp) assert instance == value
def test_from_timestamp_timestamp_returns_ulid_instance(valid_bytes_48): """ Assert that :func:`~ulid.api.from_timestamp` returns a new :class:`~ulid.ulid.ULID` instance from the given timestamp as a :class:`~ulid.ulid.Timestamp`. """ value = ulid.Timestamp(valid_bytes_48) instance = api.from_timestamp(value) assert isinstance(instance, ulid.ULID) assert instance.timestamp() == value
def test_timestamp_converts_to_utc_aware_datetime(): """ Assert that :meth:`~ulid.ulid.Timestamp.datetime` returns the value as a :class:`~datetime.datetime` instance that is UTC aware. """ now_ms = int(time.time()) * 1000 timezone = datetime.timezone.utc timestamp = ulid.Timestamp(now_ms.to_bytes(6, byteorder='big')) assert timestamp.datetime == datetime.datetime.utcfromtimestamp( now_ms / 1000.0).replace(tzinfo=timezone)