Skip to content

Commit

Permalink
Merge pull request #9169 from OpenMined/bschell/fix-timestamp-syncing
Browse files Browse the repository at this point in the history
Fix Inconsistent use of UTC time in DateTime class
  • Loading branch information
IonesioJunior authored Aug 23, 2024
2 parents f69373a + e3f9173 commit 8ff8548
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
15 changes: 11 additions & 4 deletions packages/syft/src/syft/service/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ def _repr_html_(self) -> Any:
else:
private_data_obj = private_data_res.ok_value
if isinstance(private_data_obj, ActionObject):
df = pd.DataFrame(self.data.syft_action_data)
data_table_line = itable_template_from_df(df=private_data_obj.head(5))
if isinstance(private_data_obj.syft_action_data, ActionDataEmpty):
data_table_line = "No data"
else:
df = pd.DataFrame(self.data.syft_action_data)
data_table_line = itable_template_from_df(df=df.head(5))

elif isinstance(private_data_obj, pd.DataFrame):
data_table_line = itable_template_from_df(df=private_data_obj.head(5))
else:
Expand All @@ -146,8 +150,11 @@ def _repr_html_(self) -> Any:
data_table_line = private_data_res.ok_value

if isinstance(self.mock, ActionObject):
df = pd.DataFrame(self.mock.syft_action_data)
mock_table_line = itable_template_from_df(df=df.head(5))
if isinstance(self.mock.syft_action_data, ActionDataEmpty):
mock_table_line = "No data"
else:
df = pd.DataFrame(self.mock.syft_action_data)
mock_table_line = itable_template_from_df(df=df.head(5))
elif isinstance(self.mock, pd.DataFrame):
mock_table_line = itable_template_from_df(df=self.mock.head(5))
else:
Expand Down
18 changes: 11 additions & 7 deletions packages/syft/src/syft/types/datetime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# stdlib
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from functools import total_ordering
import re
from typing import Any
Expand Down Expand Up @@ -33,23 +34,26 @@ class DateTime(SyftObject):

@classmethod
def now(cls) -> Self:
return cls(utc_timestamp=datetime.utcnow().timestamp())
utc_datetime = datetime.now(tz=timezone.utc)
return cls(utc_timestamp=utc_datetime.timestamp())

@classmethod
def from_str(cls, datetime_str: str) -> "DateTime":
dt = datetime.strptime(datetime_str, DATETIME_FORMAT)
return cls(utc_timestamp=dt.timestamp())
utc_datetime = datetime.strptime(datetime_str, DATETIME_FORMAT).replace(
tzinfo=timezone.utc
)
return cls(utc_timestamp=utc_datetime.timestamp())

def __str__(self) -> str:
utc_datetime = datetime.utcfromtimestamp(self.utc_timestamp)
utc_datetime = datetime.fromtimestamp(self.utc_timestamp, tz=timezone.utc)
return utc_datetime.strftime(DATETIME_FORMAT)

def __hash__(self) -> int:
return hash(self.utc_timestamp)

def __sub__(self, other: "DateTime") -> "DateTime":
res = self.utc_timestamp - other.utc_timestamp
return DateTime(utc_timestamp=res)
def __sub__(self, other: "DateTime") -> timedelta:
res = self.timedelta(other)
return res

def __eq__(self, other: Any) -> bool:
if other is None:
Expand Down
12 changes: 7 additions & 5 deletions packages/syft/src/syft/types/syft_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from collections.abc import Sequence
from collections.abc import Set
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from functools import cache
from functools import total_ordering
from hashlib import sha256
Expand Down Expand Up @@ -324,18 +326,18 @@ class BaseDateTime(SyftObjectVersioned):

@classmethod
def now(cls) -> Self:
return cls(utc_timestamp=datetime.utcnow().timestamp())
return cls(utc_timestamp=datetime.now(timezone.utc).timestamp())

def __str__(self) -> str:
utc_datetime = datetime.utcfromtimestamp(self.utc_timestamp)
utc_datetime = datetime.fromtimestamp(self.utc_timestamp, tz=timezone.utc)
return utc_datetime.strftime("%Y-%m-%d %H:%M:%S")

def __hash__(self) -> int:
return hash(self.utc_timestamp)

def __sub__(self, other: Self) -> Self:
res = self.utc_timestamp - other.utc_timestamp
return BaseDateTime(utc_timestamp=res)
def __sub__(self, other: Self) -> timedelta:
res = timedelta(seconds=self.utc_timestamp - other.utc_timestamp)
return res

def __eq__(self, other: Any) -> bool:
if other is None:
Expand Down

0 comments on commit 8ff8548

Please sign in to comment.