-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ccpgames/feature/milliseconds-since-epoc
Version 1.2.0 - "Instance" support
- Loading branch information
Showing
11 changed files
with
198 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = '1.1.0' | ||
__version__ = '1.2.0' | ||
|
||
__author__ = 'Thordur Matthiasson <[email protected]>' | ||
__license__ = 'MIT License' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from ._filetime import * | ||
from ._string import * | ||
from ._timestamp import * | ||
from ._instant import * | ||
from ._any import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
__all__ = [ | ||
'instant_to_datetime', | ||
'datetime_to_instant', | ||
] | ||
from ccptools.dtu.structs import * | ||
from ._timestamp import * | ||
|
||
|
||
def instant_to_datetime(milliseconds_since_epoch: T_NUMBER, minmax_on_fail: bool = False) -> Datetime: | ||
"""Converts an integer representing milliseconds since the Unix epoch | ||
(January 1, 1970) to a Python datetime object. | ||
:param milliseconds_since_epoch: Milliseconds since Unix epoch (January 1, 1970). | ||
:param minmax_on_fail: If True, will return the minimum or maximum possible | ||
value of Datetime in case of overflow (positive or | ||
negative) | ||
:return: A Python Datetime | ||
""" | ||
return timestamp_to_datetime(milliseconds_since_epoch / 1000., minmax_on_fail) | ||
|
||
|
||
def datetime_to_instant(dt: T_DATE_VALUE) -> int: | ||
"""Converts a Python datetime object to the number of milliseconds since | ||
Unix epoch (January 1, 1970). | ||
If given a date only, it will assume a time of 00:00:00.000000. | ||
:param dt: Python datetime (or date). | ||
:return: Number of milliseconds since Unix epoch (January 1, 1970) | ||
""" | ||
return int(datetime_to_timestamp(dt) * 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
__all__ = [ | ||
'timestamp_to_datetime', | ||
'datetime_to_timestamp', | ||
] | ||
from ccptools.dtu.structs import * | ||
import calendar | ||
|
||
|
||
def timestamp_to_datetime(seconds_since_epoch: T_NUMBER, minmax_on_fail: bool = False) -> Datetime: | ||
"""Converts an int or float representing seconds since the Unix epoch | ||
(January 1, 1970) to a Python datetime object. | ||
:param seconds_since_epoch: Seconds since Unix epoch (January 1, 1970). | ||
:param minmax_on_fail: If True, will return the minimum or maximum possible | ||
value of Datetime in case of overflow (positive or | ||
negative) | ||
:return: A Python Datetime | ||
""" | ||
try: | ||
return Datetime.fromtimestamp(seconds_since_epoch) | ||
except OSError: | ||
try: | ||
return Datetime(1970, 1, 1, 0, 0, 0, 0) + TimeDelta(seconds=seconds_since_epoch) | ||
except OverflowError: | ||
if minmax_on_fail: | ||
if seconds_since_epoch > 0: | ||
return Datetime.max | ||
else: | ||
return Datetime.min | ||
else: | ||
raise | ||
|
||
|
||
def datetime_to_timestamp(dt: T_DATE_VALUE) -> float: | ||
"""Converts a Python datetime object to the number of seconds since | ||
Unix epoch (January 1, 1970) as a float, including fractional seconds. | ||
If given a date only, it will assume a time of 00:00:00.000000. | ||
:param dt: Python datetime (or date). | ||
:return: Number of seconds since Unix epoch (January 1, 1970) | ||
""" | ||
if not isinstance(dt, Datetime) and isinstance(dt, Date): | ||
dt = Datetime.combine(dt, Time(0, 0, 0, 0)) | ||
# TODO([email protected]>) 2024-05-22: HANDLE SECONDS!!! | ||
|
||
int_part = float(calendar.timegm(dt.utctimetuple())) | ||
if dt.microsecond: | ||
int_part += dt.microsecond / 1000000.0 | ||
return int_part |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,7 @@ | |
import dataclasses | ||
import decimal | ||
import enum | ||
import logging | ||
import re | ||
import time | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import unittest | ||
from ccptools.dtu.structs import * | ||
from ccptools.dtu.casting import * | ||
|
||
|
||
class TestInstant(unittest.TestCase): | ||
def test_instant_to_datetime(self): | ||
_dt = Datetime(2024, 5, 22, 10, 37, 54, 123000) | ||
self.assertEqual(_dt, instant_to_datetime(1716374274123)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters