Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Jan 7, 2025
1 parent add2c71 commit 1833257
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/HABApp/openhab/items/datetime_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,29 @@ class DatetimeItem(OpenhabItem):
:ivar Mapping[str, MetaData] metadata: |oh_item_desc_metadata|
"""

@staticmethod
def _state_from_oh_str(state: str):
# see implementation im map_values.py
if PYTHON_311:
if PYTHON_311:
@staticmethod
def _state_from_oh_str(state: str) -> datetime:
# see implementation im map_values.py
dt = datetime.fromisoformat(state)
else:

# all datetime objs from openHAB have a timezone set so we can't easily compare them
# --> TypeError: can't compare offset-naive and offset-aware datetime
dt = dt.astimezone(tz=None) # Changes datetime object so it uses system timezone
value = dt.replace(tzinfo=None) # Removes timezone awareness
return value
else:
@staticmethod
def _state_from_oh_str(state: str) -> datetime:
pos_dot = state.find('.')
if (pos_plus := state.rfind('+')) == -1:
pos_plus = state.rfind('-')
if pos_plus - pos_dot > 6:
state = state[:pos_dot + 7] + state[pos_plus:]
dt = datetime.strptime(state, '%Y-%m-%dT%H:%M:%S.%f%z')

# all datetime objs from openHAB have a timezone set so we can't easily compare them
# --> TypeError: can't compare offset-naive and offset-aware datetime
dt = dt.astimezone(tz=None) # Changes datetime object so it uses system timezone
value = dt.replace(tzinfo=None) # Removes timezone awareness
return value
# all datetime objs from openHAB have a timezone set so we can't easily compare them
# --> TypeError: can't compare offset-naive and offset-aware datetime
dt = dt.astimezone(tz=None) # Changes datetime object so it uses system timezone
value = dt.replace(tzinfo=None) # Removes timezone awareness
return value

0 comments on commit 1833257

Please sign in to comment.