-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.7: fixed sml time and added a workaround for devices that report te…
…xts with a scaler
- Loading branch information
-
committed
Aug 24, 2021
1 parent
cf33071
commit dd1b7d2
Showing
6 changed files
with
62 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.6' | ||
__version__ = '0.7' |
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,30 +1,27 @@ | ||
from datetime import datetime, timedelta, tzinfo | ||
from typing import Optional | ||
from datetime import datetime, timedelta | ||
from typing import Union | ||
|
||
|
||
class SmlTzOffset(tzinfo): | ||
def __init__(self, utc_offset: int, dst_offset: int): | ||
self._utc_offset = timedelta(minutes=utc_offset) | ||
self._dst_offset = timedelta(minutes=dst_offset) | ||
|
||
def tzname(self, dt: Optional[datetime]) -> Optional[str]: | ||
return 'SmlTzOffset' | ||
|
||
def utcoffset(self, dt: Optional[datetime]) -> Optional[timedelta]: | ||
return self._utc_offset | ||
class SmlTime: | ||
HINT = Union[None, int, datetime] | ||
|
||
def dst(self, dt: Optional[datetime]) -> Optional[timedelta]: | ||
return self._dst_offset | ||
@staticmethod | ||
def from_list(_in): | ||
if _in is None: | ||
return _in | ||
|
||
# This is a workaround for times that are not reported according to specification | ||
# Instead of a choice list these devices report just the timestamp - however I am unsure about it. | ||
if isinstance(_in, int): | ||
return _in | ||
|
||
class SmlTime: | ||
@classmethod | ||
def from_list(cls, _in): | ||
_t = _in[0] | ||
_v = _in[1] | ||
if _t == 1: | ||
return _v | ||
if _t == 2: | ||
return datetime.fromtimestamp(_v) | ||
if _t == 3: | ||
return datetime.fromtimestamp(_v[0], tz=SmlTzOffset(_v[1], _v[2])) | ||
return datetime.fromtimestamp(_v[0]) + timedelta(minutes=_v[1]) + timedelta(minutes=_v[2]) | ||
|
||
raise ValueError(f'Can not build SmlTime from {_in}') |
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
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,13 @@ | ||
from datetime import datetime | ||
|
||
from smllib.sml import SmlTime | ||
|
||
|
||
def test_sml_time(): | ||
assert SmlTime.from_list(None) is None | ||
assert SmlTime.from_list([1, 253]) == 253 | ||
assert SmlTime.from_list([2, 1609466461]) == datetime(2021, 1, 1, 3, 1, 1) | ||
|
||
assert SmlTime.from_list([3, [1609466461, 120, 0]]) == datetime(2021, 1, 1, 5, 1, 1) | ||
assert SmlTime.from_list([3, [1622509261, 120, 60]]) == datetime(2021, 6, 1, 6, 1, 1) | ||
assert SmlTime.from_list([3, [1622509261, 120, 30]]) == datetime(2021, 6, 1, 5, 31, 1) |