summaryrefslogtreecommitdiffstats
path: root/tests/datetime
diff options
context:
space:
mode:
Diffstat (limited to 'tests/datetime')
-rw-r--r--tests/datetime/test_add.py20
-rw-r--r--tests/datetime/test_behavior.py8
-rw-r--r--tests/datetime/test_comparison.py2
-rw-r--r--tests/datetime/test_construct.py27
-rw-r--r--tests/datetime/test_day_of_week_modifiers.py18
-rw-r--r--tests/datetime/test_from_format.py35
-rw-r--r--tests/datetime/test_getters.py51
-rw-r--r--tests/datetime/test_start_end_of.py40
-rw-r--r--tests/datetime/test_strings.py10
9 files changed, 157 insertions, 54 deletions
diff --git a/tests/datetime/test_add.py b/tests/datetime/test_add.py
index 87ea39f..409f5bd 100644
--- a/tests/datetime/test_add.py
+++ b/tests/datetime/test_add.py
@@ -252,17 +252,29 @@ def test_add_time_to_new_transition_repeated_big():
assert not dt.is_dst()
-def test_add_interval():
+def test_add_duration_across_transition():
dt = pendulum.datetime(2017, 3, 11, 10, 45, tz="America/Los_Angeles")
new = dt + pendulum.duration(hours=24)
assert_datetime(new, 2017, 3, 12, 11, 45)
-def test_period_over_midnight_tz():
+def test_add_duration_across_transition_days():
+ dt = pendulum.datetime(2017, 3, 11, 10, 45, tz="America/Los_Angeles")
+ new = dt + pendulum.duration(days=1)
+
+ assert_datetime(new, 2017, 3, 12, 10, 45)
+
+ dt = pendulum.datetime(2023, 11, 5, 0, 0, tz="America/Chicago")
+ new = dt + pendulum.duration(days=1)
+
+ assert_datetime(new, 2023, 11, 6, 0, 0)
+
+
+def test_interval_over_midnight_tz():
start = pendulum.datetime(2018, 2, 25, tz="Europe/Paris")
end = start.add(hours=1)
- period = end - start
- new_end = start + period
+ interval = end - start
+ new_end = start + interval
assert new_end == end
diff --git a/tests/datetime/test_behavior.py b/tests/datetime/test_behavior.py
index e02323a..76de1d6 100644
--- a/tests/datetime/test_behavior.py
+++ b/tests/datetime/test_behavior.py
@@ -158,6 +158,14 @@ def test_deepcopy():
assert dt == deepcopy(dt)
+def test_deepcopy_on_transition():
+ dt = pendulum.datetime(2023, 11, 5, 1, 0, 0, tz="US/Pacific")
+ clone = deepcopy(dt)
+
+ assert dt == clone
+ assert dt.offset == clone.offset
+
+
def test_pickle_timezone():
dt1 = pendulum.timezone("Europe/Amsterdam")
s = pickle.dumps(dt1)
diff --git a/tests/datetime/test_comparison.py b/tests/datetime/test_comparison.py
index ad81e73..c3cb064 100644
--- a/tests/datetime/test_comparison.py
+++ b/tests/datetime/test_comparison.py
@@ -75,7 +75,7 @@ def test_not_equal_with_timezone_true():
def test_not_equal_to_none():
d1 = pendulum.datetime(2000, 1, 1, 1, 2, 3)
- assert d1 != None # noqa
+ assert d1 is not None
def test_greater_than_true():
diff --git a/tests/datetime/test_construct.py b/tests/datetime/test_construct.py
index 9488c08..b083cbe 100644
--- a/tests/datetime/test_construct.py
+++ b/tests/datetime/test_construct.py
@@ -5,17 +5,15 @@ import os
from datetime import datetime
import pytest
-import pytz
-
-from dateutil import tz
import pendulum
from pendulum import DateTime
-from pendulum.tz import timezone
+from pendulum import timezone
from pendulum.utils._compat import PYPY
from tests.conftest import assert_datetime
+
if not PYPY:
import time_machine
else:
@@ -82,27 +80,6 @@ def test_yesterday():
assert now.diff(yesterday, False).in_days() == -1
-def test_instance_naive_datetime_defaults_to_utc():
- now = pendulum.instance(datetime.now())
- assert now.timezone_name == "UTC"
-
-
-def test_instance_timezone_aware_datetime():
- now = pendulum.instance(datetime.now(timezone("Europe/Paris")))
- assert now.timezone_name == "Europe/Paris"
-
-
-def test_instance_timezone_aware_datetime_pytz():
- now = pendulum.instance(datetime.now(pytz.timezone("Europe/Paris")))
- assert now.timezone_name == "Europe/Paris"
-
-
-def test_instance_timezone_aware_datetime_any_tzinfo():
- dt = datetime(2016, 8, 7, 12, 34, 56, tzinfo=tz.gettz("Europe/Paris"))
- now = pendulum.instance(dt)
- assert now.timezone_name == "+02:00"
-
-
def test_now():
now = pendulum.now("America/Toronto")
in_paris = pendulum.now("Europe/Paris")
diff --git a/tests/datetime/test_day_of_week_modifiers.py b/tests/datetime/test_day_of_week_modifiers.py
index 46de84e..eb4bc4f 100644
--- a/tests/datetime/test_day_of_week_modifiers.py
+++ b/tests/datetime/test_day_of_week_modifiers.py
@@ -49,7 +49,7 @@ def test_next_monday():
def test_next_saturday():
- d = pendulum.datetime(1975, 5, 21).next(6)
+ d = pendulum.datetime(1975, 5, 21).next(5)
assert_datetime(d, 1975, 5, 24, 0, 0, 0)
@@ -79,7 +79,7 @@ def test_previous_monday():
def test_previous_saturday():
- d = pendulum.datetime(1975, 5, 21).previous(6)
+ d = pendulum.datetime(1975, 5, 21).previous(5)
assert_datetime(d, 1975, 5, 17, 0, 0, 0)
@@ -109,7 +109,7 @@ def test_first_wednesday_of_month():
def test_first_friday_of_month():
- d = pendulum.datetime(1975, 11, 21).first_of("month", 5)
+ d = pendulum.datetime(1975, 11, 21).first_of("month", 4)
assert_datetime(d, 1975, 11, 7, 0, 0, 0)
@@ -124,7 +124,7 @@ def test_last_tuesday_of_month():
def test_last_friday_of_month():
- d = pendulum.datetime(1975, 12, 5).last_of("month", 5)
+ d = pendulum.datetime(1975, 12, 5).last_of("month", 4)
assert_datetime(d, 1975, 12, 26, 0, 0, 0)
@@ -155,7 +155,7 @@ def test_2nd_monday_of_month():
def test_3rd_wednesday_of_month():
- d = pendulum.datetime(1975, 12, 5).nth_of("month", 3, 3)
+ d = pendulum.datetime(1975, 12, 5).nth_of("month", 3, 2)
assert_datetime(d, 1975, 12, 17, 0, 0, 0)
@@ -171,7 +171,7 @@ def test_first_wednesday_of_quarter():
def test_first_friday_of_quarter():
- d = pendulum.datetime(1975, 11, 21).first_of("quarter", 5)
+ d = pendulum.datetime(1975, 11, 21).first_of("quarter", 4)
assert_datetime(d, 1975, 10, 3, 0, 0, 0)
@@ -231,7 +231,7 @@ def test_2nd_monday_of_quarter():
def test_3rd_wednesday_of_quarter():
- d = pendulum.datetime(1975, 8, 5).nth_of("quarter", 3, 3)
+ d = pendulum.datetime(1975, 8, 5).nth_of("quarter", 3, 2)
assert_datetime(d, 1975, 7, 16, 0, 0, 0)
@@ -246,7 +246,7 @@ def test_first_wednesday_of_year():
def test_first_friday_of_year():
- d = pendulum.datetime(1975, 11, 21).first_of("year", 5)
+ d = pendulum.datetime(1975, 11, 21).first_of("year", 4)
assert_datetime(d, 1975, 1, 3, 0, 0, 0)
@@ -261,7 +261,7 @@ def test_last_tuesday_of_year():
def test_last_friday_of_year():
- d = pendulum.datetime(1975, 8, 5).last_of("year", 5)
+ d = pendulum.datetime(1975, 8, 5).last_of("year", 4)
assert_datetime(d, 1975, 12, 26, 0, 0, 0)
diff --git a/tests/datetime/test_from_format.py b/tests/datetime/test_from_format.py
index 10c4a23..184c0a0 100644
--- a/tests/datetime/test_from_format.py
+++ b/tests/datetime/test_from_format.py
@@ -81,9 +81,8 @@ def test_from_format_with_invalid_padded_day():
("12/02/1999", "DD/MM/YYYY", "1999-02-12T00:00:00+00:00", None),
("12_02_1999", "DD_MM_YYYY", "1999-02-12T00:00:00+00:00", None),
("12:02:1999", "DD:MM:YYYY", "1999-02-12T00:00:00+00:00", None),
- ("2-2-99", "D-M-YY", "2099-02-02T00:00:00+00:00", None),
- ("2-2-99", "D-M-YY", "1999-02-02T00:00:00+00:00", "1990-01-01"),
- ("99", "YY", "2099-01-01T00:00:00+00:00", None),
+ ("2-2-99", "D-M-YY", "1999-02-02T00:00:00+00:00", None),
+ ("99", "YY", "1999-01-01T00:00:00+00:00", None),
("300-1999", "DDD-YYYY", "1999-10-27T00:00:00+00:00", None),
("12-02-1999 2:45:10", "DD-MM-YYYY h:m:s", "1999-02-12T02:45:10+00:00", None),
("12-02-1999 12:45:10", "DD-MM-YYYY h:m:s", "1999-02-12T12:45:10+00:00", None),
@@ -100,7 +99,8 @@ def test_from_format_with_invalid_padded_day():
("Monday", "dddd", "2018-01-29T00:00:00+00:00", "2018-02-02"),
("Mon", "ddd", "2018-01-29T00:00:00+00:00", "2018-02-02"),
("Mo", "dd", "2018-01-29T00:00:00+00:00", "2018-02-02"),
- ("0", "d", "2018-02-04T00:00:00+00:00", "2018-02-02"),
+ ("0", "d", "2018-01-29T00:00:00+00:00", "2018-02-02"),
+ ("6", "d", "2018-02-04T00:00:00+00:00", "2018-02-02"),
("1", "E", "2018-01-29T00:00:00+00:00", "2018-02-02"),
("March", "MMMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
("Mar", "MMM", "2018-03-01T00:00:00+00:00", "2018-02-02"),
@@ -150,10 +150,7 @@ def test_from_format_with_invalid_padded_day():
],
)
def test_from_format(text, fmt, expected, now):
- if now is None:
- now = pendulum.datetime(2015, 11, 12)
- else:
- now = pendulum.parse(now)
+ now = pendulum.datetime(2015, 11, 12) if now is None else pendulum.parse(now)
with pendulum.travel_to(now, freeze=True):
assert pendulum.from_format(text, fmt).isoformat() == expected
@@ -201,3 +198,25 @@ def test_strptime():
assert_datetime(d, 1975, 5, 21, 22, 32, 11)
assert isinstance(d, pendulum.DateTime)
assert d.timezone_name == "UTC"
+
+
+def test_from_format_2_digit_year():
+ """
+ Complies with open group spec for 2 digit years
+ https://pubs.opengroup.org/onlinepubs/9699919799/
+
+ "If century is not specified, then values in the range [69,99] shall
+ refer to years 1969 to 1999 inclusive, and values in the
+ range [00,68] shall refer to years 2000 to 2068 inclusive."
+ """
+ d = pendulum.from_format("00", "YY")
+ assert d.year == 2000
+
+ d = pendulum.from_format("68", "YY")
+ assert d.year == 2068
+
+ d = pendulum.from_format("69", "YY")
+ assert d.year == 1969
+
+ d = pendulum.from_format("99", "YY")
+ assert d.year == 1999
diff --git a/tests/datetime/test_getters.py b/tests/datetime/test_getters.py
index 5074623..8f4c1ae 100644
--- a/tests/datetime/test_getters.py
+++ b/tests/datetime/test_getters.py
@@ -7,7 +7,7 @@ import pytest
import pendulum
from pendulum import DateTime
-from pendulum.tz import timezone
+from pendulum import timezone
from tests.conftest import assert_date
from tests.conftest import assert_time
@@ -126,7 +126,8 @@ def test_utc():
assert pendulum.datetime(2012, 1, 1, tz="UTC").is_utc()
assert pendulum.datetime(2012, 1, 1, tz=0).is_utc()
assert not pendulum.datetime(2012, 1, 1, tz=5).is_utc()
- # There is no time difference between Greenwich Mean Time and Coordinated Universal Time
+ # There is no time difference between Greenwich Mean Time
+ # and Coordinated Universal Time
assert pendulum.datetime(2012, 1, 1, tz="GMT").is_utc()
@@ -194,6 +195,14 @@ def test_week_of_year_last_week():
assert pendulum.datetime(2012, 12, 31).week_of_year == 1
+def test_week_of_month_edge_case():
+ assert pendulum.datetime(2020, 1, 1).week_of_month == 1
+ assert pendulum.datetime(2020, 1, 7).week_of_month == 2
+ assert pendulum.datetime(2020, 1, 14).week_of_month == 3
+ assert pendulum.datetime(2023, 1, 1).week_of_month == 1
+ assert pendulum.datetime(2023, 1, 31).week_of_month == 6
+
+
def test_timezone():
d = pendulum.datetime(2000, 1, 1, tz="America/Toronto")
assert d.timezone.name == "America/Toronto"
@@ -246,3 +255,41 @@ def test_time():
t = dt.time()
assert isinstance(t, pendulum.Time)
assert_time(t, 10, 40, 34, 123456)
+
+
+@pytest.mark.parametrize(
+ "date, expected",
+ [
+ (pendulum.Date(2000, 1, 1), 1),
+ (pendulum.Date(2000, 1, 3), 2),
+ (pendulum.Date(2019, 12, 29), 5),
+ (pendulum.Date(2019, 12, 30), 6),
+ (pendulum.Date(2019, 12, 31), 6),
+ (pendulum.Date(2020, 1, 7), 2),
+ (pendulum.Date(2020, 1, 14), 3),
+ (pendulum.Date(2021, 1, 1), 1),
+ (pendulum.Date(2021, 1, 2), 1),
+ (pendulum.Date(2021, 1, 9), 2),
+ (pendulum.Date(2021, 1, 10), 2),
+ (pendulum.Date(2021, 1, 11), 3),
+ (pendulum.Date(2021, 1, 15), 3),
+ (pendulum.Date(2021, 1, 16), 3),
+ (pendulum.Date(2021, 1, 17), 3),
+ (pendulum.Date(2021, 1, 23), 4),
+ (pendulum.Date(2021, 1, 31), 5),
+ (pendulum.Date(2021, 12, 19), 3),
+ (pendulum.Date(2021, 12, 25), 4),
+ (pendulum.Date(2021, 12, 26), 4),
+ (pendulum.Date(2021, 12, 29), 5),
+ (pendulum.Date(2021, 12, 30), 5),
+ (pendulum.Date(2021, 12, 31), 5),
+ (pendulum.Date(2022, 1, 1), 1),
+ (pendulum.Date(2022, 1, 3), 2),
+ (pendulum.Date(2022, 1, 10), 3),
+ (pendulum.Date(2023, 1, 1), 1),
+ (pendulum.Date(2023, 1, 2), 2),
+ (pendulum.Date(2029, 12, 31), 6),
+ ],
+)
+def test_week_of_month_negative(date, expected):
+ assert date.week_of_month == expected
diff --git a/tests/datetime/test_start_end_of.py b/tests/datetime/test_start_end_of.py
index 597dbeb..1937e74 100644
--- a/tests/datetime/test_start_end_of.py
+++ b/tests/datetime/test_start_end_of.py
@@ -277,9 +277,49 @@ def test_start_of_with_transition():
assert d.start_of("year").offset == 3600
+def test_start_of_on_date_before_transition():
+ d = pendulum.datetime(2013, 10, 27, 0, 59, 59, tz="UTC").in_timezone("Europe/Paris")
+ assert d.offset == 7200
+ assert d.start_of("minute").offset == 7200
+ assert d.start_of("hour").offset == 7200
+ assert d.start_of("day").offset == 7200
+ assert d.start_of("month").offset == 7200
+ assert d.start_of("year").offset == 3600
+
+
+def test_start_of_on_date_after_transition():
+ d = pendulum.datetime(2013, 10, 27, 1, 59, 59, tz="UTC").in_timezone("Europe/Paris")
+ assert d.offset == 3600
+ assert d.start_of("minute").offset == 3600
+ assert d.start_of("hour").offset == 3600
+ assert d.start_of("day").offset == 7200
+ assert d.start_of("month").offset == 7200
+ assert d.start_of("year").offset == 3600
+
+
def test_end_of_with_transition():
d = pendulum.datetime(2013, 3, 31, tz="Europe/Paris")
assert d.offset == 3600
assert d.end_of("month").offset == 7200
assert d.end_of("day").offset == 7200
assert d.end_of("year").offset == 3600
+
+
+def test_end_of_on_date_before_transition():
+ d = pendulum.datetime(2013, 10, 27, 0, 0, 0, tz="UTC").in_timezone("Europe/Paris")
+ assert d.offset == 7200
+ assert d.end_of("minute").offset == 7200
+ assert d.end_of("hour").offset == 7200
+ assert d.end_of("day").offset == 3600
+ assert d.end_of("month").offset == 3600
+ assert d.end_of("year").offset == 3600
+
+
+def test_end_of_on_date_after_transition():
+ d = pendulum.datetime(2013, 10, 27, 1, 0, 0, tz="UTC").in_timezone("Europe/Paris")
+ assert d.offset == 3600
+ assert d.end_of("minute").offset == 3600
+ assert d.end_of("hour").offset == 3600
+ assert d.end_of("day").offset == 3600
+ assert d.end_of("month").offset == 3600
+ assert d.end_of("year").offset == 3600
diff --git a/tests/datetime/test_strings.py b/tests/datetime/test_strings.py
index 0de340d..e78dbc8 100644
--- a/tests/datetime/test_strings.py
+++ b/tests/datetime/test_strings.py
@@ -7,9 +7,9 @@ import pendulum
def test_to_string():
d = pendulum.datetime(1975, 12, 25, 0, 0, 0, 0, tz="local")
- assert str(d) == d.to_iso8601_string()
+ assert str(d) == "1975-12-25 00:00:00-05:00"
d = pendulum.datetime(1975, 12, 25, 0, 0, 0, 123456, tz="local")
- assert str(d) == d.to_iso8601_string()
+ assert str(d) == "1975-12-25 00:00:00.123456-05:00"
def test_to_date_string():
@@ -109,11 +109,11 @@ def test_to_string_invalid():
def test_repr():
d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="local")
- expected = f"DateTime(1975, 12, 25, 14, 15, 16, tzinfo={repr(d.tzinfo)})"
+ expected = f"DateTime(1975, 12, 25, 14, 15, 16, tzinfo={d.tzinfo!r})"
assert repr(d) == expected
d = pendulum.datetime(1975, 12, 25, 14, 15, 16, 123456, tz="local")
- expected = f"DateTime(1975, 12, 25, 14, 15, 16, 123456, tzinfo={repr(d.tzinfo)})"
+ expected = f"DateTime(1975, 12, 25, 14, 15, 16, 123456, tzinfo={d.tzinfo!r})"
assert repr(d) == expected
@@ -135,7 +135,7 @@ def test_for_json():
def test_format():
d = pendulum.datetime(1975, 12, 25, 14, 15, 16, tz="Europe/Paris")
- assert f"{d}" == "1975-12-25T14:15:16+01:00"
+ assert f"{d}" == "1975-12-25 14:15:16+01:00"
assert f"{d:YYYY}" == "1975"
assert f"{d:%Y}" == "1975"
assert f"{d:%H:%M %d.%m.%Y}" == "14:15 25.12.1975"