Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lark_oapi/ws/pb/google/protobuf/internal/well_known_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def Is(self, descriptor):
return '/' in self.type_url and self.TypeName() == descriptor.full_name


_EPOCH_DATETIME_NAIVE = datetime.datetime.utcfromtimestamp(0)
_EPOCH_DATETIME_NAIVE = datetime.datetime(1970, 1, 1)
_EPOCH_DATETIME_AWARE = datetime.datetime.fromtimestamp(
0, tz=datetime.timezone.utc)

Expand Down Expand Up @@ -192,7 +192,7 @@ def FromJsonString(self, value):

def GetCurrentTime(self):
"""Get the current UTC into Timestamp."""
self.FromDatetime(datetime.datetime.utcnow())
self.FromDatetime(datetime.datetime.now(tz=datetime.timezone.utc))

def ToNanoseconds(self):
"""Converts Timestamp to nanoseconds since epoch."""
Expand Down
32 changes: 32 additions & 0 deletions lark_oapi/ws/tests/test_well_known_types_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import datetime
import time
import warnings

from lark_oapi.ws.pb.google.protobuf import timestamp_pb2


def test_timestamp_get_current_time_no_deprecation_warning():
"""GetCurrentTime must not emit DeprecationWarning (issue #145)."""
ts = timestamp_pb2.Timestamp()
with warnings.catch_warnings():
warnings.simplefilter("error", DeprecationWarning)
ts.GetCurrentTime()
assert abs(ts.ToSeconds() - time.time()) < 5


def test_timestamp_from_to_datetime_roundtrip_naive():
ts = timestamp_pb2.Timestamp()
ts.FromDatetime(datetime.datetime(2024, 1, 2, 3, 4, 5, 123456))
dt = ts.ToDatetime()
assert dt == datetime.datetime(2024, 1, 2, 3, 4, 5, 123456)


def test_timestamp_from_to_datetime_roundtrip_aware():
ts = timestamp_pb2.Timestamp()
ts.FromDatetime(
datetime.datetime(2024, 1, 2, 3, 4, 5, 123456, tzinfo=datetime.timezone.utc)
)
dt = ts.ToDatetime(tzinfo=datetime.timezone.utc)
assert dt == datetime.datetime(
2024, 1, 2, 3, 4, 5, 123456, tzinfo=datetime.timezone.utc
)