Skip to content

Commit

Permalink
fix: aware date sometimes get 0-0-0 dates (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyKh committed Mar 3, 2024
1 parent 2667600 commit 5ec1b1e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
18 changes: 15 additions & 3 deletions iec_api/models/invoice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass, field
from datetime import datetime

import pytz
from mashumaro import DataClassDictMixin, field_options
from mashumaro.codecs import BasicDecoder

Expand Down Expand Up @@ -81,9 +82,20 @@ class Invoice(DataClassDictMixin):

@classmethod
def __post_deserialize__(cls, obj: "Invoice") -> "Invoice":
obj.full_date = TIMEZONE.localize(obj.full_date)
obj.from_date = TIMEZONE.localize(obj.from_date)
obj.to_date = TIMEZONE.localize(obj.to_date)
if obj.full_date.year > 2000: # Fix '0001-01-01T00:00:00' values
obj.full_date = TIMEZONE.localize(obj.full_date)
else:
obj.full_date = obj.full_date.replace(tzinfo=pytz.utc)

if obj.from_date.year > 2000: # Fix '0001-01-01T00:00:00' values
obj.from_date = TIMEZONE.localize(obj.from_date)
else:
obj.from_date = obj.from_date.replace(tzinfo=pytz.utc)

if obj.to_date.year > 2000: # Fix '0001-01-01T00:00:00' values
obj.to_date = TIMEZONE.localize(obj.to_date)
else:
obj.to_date = obj.to_date.replace(tzinfo=pytz.utc)
return obj


Expand Down
7 changes: 5 additions & 2 deletions iec_api/models/meter_reading.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
""" Meter Reading model. """

from dataclasses import dataclass, field
from datetime import datetime

import pytz
from mashumaro import DataClassDictMixin, field_options
from mashumaro.codecs import BasicDecoder

Expand Down Expand Up @@ -51,7 +51,10 @@ class MeterReading(DataClassDictMixin):

@classmethod
def __post_deserialize__(cls, obj: "MeterReading") -> "MeterReading":
obj.reading_date = TIMEZONE.localize(obj.reading_date)
if obj.reading_date.year > 2000: # Fix '0001-01-01T00:00:00' values
obj.reading_date = TIMEZONE.localize(obj.reading_date)
else:
obj.reading_date = obj.reading_date.replace(tzinfo=pytz.utc)
return obj


Expand Down
7 changes: 5 additions & 2 deletions iec_api/models/remote_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
# "totalImport": 35.273,
# "data": [{ "status": 8192, "date": "2023-07-20T00:00:00.000000", "value": 0 }]
# }

from dataclasses import dataclass, field
from datetime import date, datetime
from enum import IntEnum
from typing import Optional

import pytz
from mashumaro import DataClassDictMixin, field_options
from mashumaro.config import BaseConfig

Expand Down Expand Up @@ -79,7 +79,10 @@ class RemoteReading(DataClassDictMixin):

@classmethod
def __post_deserialize__(cls, obj: "RemoteReading") -> "RemoteReading":
obj.date = TIMEZONE.localize(obj.date)
if obj.date.year > 2000: # Fix '0001-01-01T00:00:00' values
obj.date = TIMEZONE.localize(obj.date)
else:
obj.date = obj.date.replace(tzinfo=pytz.utc)
return obj


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "iec-api"
version = "0.2.4"
version = "0.2.5"
description = "A Python wrapper for Israel Electric Company API"
authors = ["GuyKh"]
license = "MIT"
Expand Down

0 comments on commit 5ec1b1e

Please sign in to comment.