Commit 5b334624 by Ken Oslund Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 429099528
parent 01fd6b4f
...@@ -4,13 +4,8 @@ ...@@ -4,13 +4,8 @@
# BSD-style license that can be found in the LICENSE file. # BSD-style license that can be found in the LICENSE file.
"""Tests for absl pybind11 casters.""" """Tests for absl pybind11 casters."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import array import array
import datetime import datetime
import sys
from absl.testing import absltest from absl.testing import absltest
from absl.testing import parameterized from absl.testing import parameterized
...@@ -20,25 +15,11 @@ import numpy as np ...@@ -20,25 +15,11 @@ import numpy as np
from pybind11_abseil.tests import absl_example from pybind11_abseil.tests import absl_example
if sys.version_info.major > 2:
DateTime = datetime.datetime
else:
class DateTime(datetime.datetime):
def timestamp(self):
if self.tzinfo:
with_tz = self
else:
with_tz = self.replace(tzinfo=tz.gettz()) # pylint: disable=g-tzinfo-replace
epoch = datetime.datetime(1970, 1, 1, tzinfo=tz.tzutc()) # pylint: disable=g-tzinfo-datetime
return (with_tz - epoch).total_seconds()
class AbslTimeTest(parameterized.TestCase): class AbslTimeTest(parameterized.TestCase):
SECONDS_IN_DAY = 24 * 60 * 60 SECONDS_IN_DAY = 24 * 60 * 60
POSITIVE_SECS = 3 * SECONDS_IN_DAY + 2.5 POSITIVE_SECS = 3 * SECONDS_IN_DAY + 2.5
NEGATIVE_SECS = -3 * SECONDS_IN_DAY + 2.5 NEGATIVE_SECS = -3 * SECONDS_IN_DAY + 2.5
TEST_DATETIME = DateTime(2000, 1, 2, 3, 4, 5, int(5e5)) TEST_DATETIME = datetime.datetime(2000, 1, 2, 3, 4, 5, int(5e5))
# Linter error relevant for pytz only. # Linter error relevant for pytz only.
# pylint: disable=g-tzinfo-replace # pylint: disable=g-tzinfo-replace
TEST_DATETIME_UTC = TEST_DATETIME.replace(tzinfo=tz.tzutc()) TEST_DATETIME_UTC = TEST_DATETIME.replace(tzinfo=tz.tzutc())
...@@ -70,7 +51,7 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -70,7 +51,7 @@ class AbslTimeTest(parameterized.TestCase):
local_tz = tz.gettz() local_tz = tz.gettz()
# pylint: disable=g-tzinfo-datetime # pylint: disable=g-tzinfo-datetime
# Warning about tzinfo applies to pytz, but we are using dateutil.tz # Warning about tzinfo applies to pytz, but we are using dateutil.tz
expected_datetime = DateTime( expected_datetime = datetime.datetime(
year=self.TEST_DATETIME.year, year=self.TEST_DATETIME.year,
month=self.TEST_DATETIME.month, month=self.TEST_DATETIME.month,
day=self.TEST_DATETIME.day, day=self.TEST_DATETIME.day,
...@@ -85,8 +66,8 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -85,8 +66,8 @@ class AbslTimeTest(parameterized.TestCase):
self.assertEqual(expected_datetime, absl_example.make_datetime(secs)) self.assertEqual(expected_datetime, absl_example.make_datetime(secs))
def test_pass_date(self): def test_pass_date(self):
secs = DateTime(self.TEST_DATE.year, self.TEST_DATE.month, secs = datetime.datetime(self.TEST_DATE.year, self.TEST_DATE.month,
self.TEST_DATE.day).timestamp() self.TEST_DATE.day).timestamp()
self.assertTrue(absl_example.check_datetime(self.TEST_DATE, secs)) self.assertTrue(absl_example.check_datetime(self.TEST_DATE, secs))
def test_pass_datetime(self): def test_pass_datetime(self):
...@@ -97,7 +78,8 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -97,7 +78,8 @@ class AbslTimeTest(parameterized.TestCase):
pacific_tz = tz.gettz('America/Los_Angeles') pacific_tz = tz.gettz('America/Los_Angeles')
# pylint: disable=g-tzinfo-datetime # pylint: disable=g-tzinfo-datetime
# Warning about tzinfo applies to pytz, but we are using dateutil.tz # Warning about tzinfo applies to pytz, but we are using dateutil.tz
dt_with_tz = DateTime(year=2020, month=2, day=1, hour=20, tzinfo=pacific_tz) dt_with_tz = datetime.datetime(
year=2020, month=2, day=1, hour=20, tzinfo=pacific_tz)
# pylint: enable=g-tzinfo-datetime # pylint: enable=g-tzinfo-datetime
secs = dt_with_tz.timestamp() secs = dt_with_tz.timestamp()
self.assertTrue(absl_example.check_datetime(dt_with_tz, secs)) self.assertTrue(absl_example.check_datetime(dt_with_tz, secs))
...@@ -105,13 +87,13 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -105,13 +87,13 @@ class AbslTimeTest(parameterized.TestCase):
def test_pass_datetime_dst_with_timezone(self): def test_pass_datetime_dst_with_timezone(self):
pacific_tz = tz.gettz('America/Los_Angeles') pacific_tz = tz.gettz('America/Los_Angeles')
# pylint: disable=g-tzinfo-datetime # pylint: disable=g-tzinfo-datetime
dst_end = DateTime(2020, 11, 1, 2, 0, 0, tzinfo=pacific_tz) dst_end = datetime.datetime(2020, 11, 1, 2, 0, 0, tzinfo=pacific_tz)
# pylint: enable=g-tzinfo-datetime # pylint: enable=g-tzinfo-datetime
secs = dst_end.timestamp() secs = dst_end.timestamp()
self.assertTrue(absl_example.check_datetime(dst_end, secs)) self.assertTrue(absl_example.check_datetime(dst_end, secs))
def test_pass_datetime_dst(self): def test_pass_datetime_dst(self):
dst_end = DateTime(2020, 11, 1, 2, 0, 0) dst_end = datetime.datetime(2020, 11, 1, 2, 0, 0)
secs = dst_end.timestamp() secs = dst_end.timestamp()
self.assertTrue(absl_example.check_datetime(dst_end, secs)) self.assertTrue(absl_example.check_datetime(dst_end, secs))
...@@ -119,14 +101,14 @@ class AbslTimeTest(parameterized.TestCase): ...@@ -119,14 +101,14 @@ class AbslTimeTest(parameterized.TestCase):
def test_dst_datetime_from_timestamp(self, offs): def test_dst_datetime_from_timestamp(self, offs):
secs_flip = 1604224799 # 2020-11-01T02:00:00-08:00 secs_flip = 1604224799 # 2020-11-01T02:00:00-08:00
secs = secs_flip + offs secs = secs_flip + offs
time_utc = DateTime.fromtimestamp(secs, tz.tzutc()) time_utc = datetime.datetime.fromtimestamp(secs, tz.tzutc())
time_local_aware = time_utc.astimezone(tz.gettz()) time_local_aware = time_utc.astimezone(tz.gettz())
time_local_naive = time_local_aware.replace(tzinfo=None) time_local_naive = time_local_aware.replace(tzinfo=None)
for time in (time_utc, time_local_aware, time_local_naive): for time in (time_utc, time_local_aware, time_local_naive):
self.assertTrue(absl_example.check_datetime(time, secs)) self.assertTrue(absl_example.check_datetime(time, secs))
def test_pass_datetime_pre_unix_epoch(self): def test_pass_datetime_pre_unix_epoch(self):
dt = DateTime(1969, 7, 16, 10, 56, 7, microsecond=140) dt = datetime.datetime(1969, 7, 16, 10, 56, 7, microsecond=140)
secs = dt.timestamp() secs = dt.timestamp()
self.assertTrue(absl_example.check_datetime(dt, secs)) self.assertTrue(absl_example.check_datetime(dt, secs))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment