from datetime import UTC, datetime, timedelta from unittest.mock import patch import pytest from dopt_basics import datetime as datetime_ from dopt_basics.datetime import TIMEZONE_CEST, TimeUnitsTimedelta def test_dt_with_UTC(): year = 2024 month = 3 day = 28 hour = 3 minute = 0 dt_target = datetime(year, month, day, hour, minute, tzinfo=UTC) dt_ret = datetime_.dt_with_tz_UTC(year, month, day, hour, minute) assert dt_target == dt_ret @pytest.mark.parametrize( "time_unit, expected", [ ("hours", timedelta(hours=2.0)), ("minutes", timedelta(minutes=2.0)), ("seconds", timedelta(seconds=2.0)), ("milliseconds", timedelta(milliseconds=2.0)), ("microseconds", timedelta(microseconds=2.0)), (TimeUnitsTimedelta.HOURS, timedelta(hours=2.0)), (TimeUnitsTimedelta.MINUTES, timedelta(minutes=2.0)), ], ) def test_timedelta_from_val_Success(time_unit, expected): val = 2.0 td = datetime_.timedelta_from_val(val, time_unit) assert td == expected def test_timedelta_from_val_FailWrongTimeUnit(): val = 2.0 time_unit = "years" with pytest.raises(ValueError): datetime_.timedelta_from_val(val, time_unit) # type: ignore def test_round_td_by_seconds(): hours = 2.0 minutes = 30.0 seconds = 30.0 microseconds = 600 td = timedelta(hours=hours, minutes=minutes, seconds=seconds, microseconds=microseconds) rounded_td = datetime_.round_td_by_seconds(td, round_to_next_seconds=1) assert rounded_td == timedelta(hours=2.0, minutes=30.0, seconds=30.0) def test_current_time_tz(): tz = datetime_.TIMEZONE_UTC mock_dt = datetime(2024, 6, 1, 12, 15, 30, 1000, tzinfo=tz) with patch("dopt_basics.datetime.Datetime") as mock_obj: mock_obj.now.return_value = mock_dt ret = datetime_.current_time_tz(cut_microseconds=False) assert ret.tzinfo is not None assert ret == mock_dt with patch("dopt_basics.datetime.Datetime") as mock_obj: mock_obj.now.return_value = mock_dt ret = datetime_.current_time_tz(cut_microseconds=True) target = datetime(2024, 6, 1, 12, 15, 30, tzinfo=tz) assert ret.tzinfo is not None assert ret == target def test_get_timestamp_WithTime(): tz = datetime_.TIMEZONE_UTC mock_dt = datetime(2024, 6, 1, 12, 15, 30, 1000, tzinfo=tz) with patch("dopt_basics.datetime.Datetime") as mock_obj: mock_obj.now.return_value = mock_dt ret = datetime_.get_timestamp(tz=tz, with_time=True) target = "2024-06-01--12-15-30" assert ret == target def test_get_timestamp_WithoutTime(): tz = datetime_.TIMEZONE_UTC mock_dt = datetime(2024, 6, 1, 12, 15, 30, 1000, tzinfo=tz) with patch("dopt_basics.datetime.Datetime") as mock_obj: mock_obj.now.return_value = mock_dt ret = datetime_.get_timestamp(tz=tz, with_time=False) target = "2024-06-01" assert ret == target def test_add_timedelta_FailWithoutTZInfo(): year = 2024 month = 3 day = 30 hour = 3 minute = 0 dt = datetime(year, month, day, hour, minute) td = timedelta(hours=2.0) with pytest.raises(ValueError): datetime_.add_timedelta_with_tz(dt, td) def test_add_timedelta_with_tz(): year = 2024 month = 3 day = 30 hour = 23 minute = 0 dt = datetime(year, month, day, hour, minute, tzinfo=TIMEZONE_CEST) td = timedelta(hours=6.0) new_dt = datetime_.add_timedelta_with_tz(dt, td) assert new_dt == datetime(2024, 3, 31, 6, 0, tzinfo=TIMEZONE_CEST) def test_validate_dt_UTC_Success(): dt = datetime(2024, 3, 30, 0, 0, tzinfo=UTC) datetime_.validate_dt_UTC(dt) def test_validate_dt_FailWrongTZInfo(): dt = datetime(2024, 3, 30, 0, 0, tzinfo=TIMEZONE_CEST) with pytest.raises(ValueError): datetime_.validate_dt_UTC(dt) def test_dt_to_timezone_Success(): dt = datetime(2024, 3, 30, 2, 0, tzinfo=UTC) new_dt = datetime_.dt_to_timezone(dt, TIMEZONE_CEST) assert new_dt == datetime(2024, 3, 30, 3, tzinfo=TIMEZONE_CEST) def test_dt_to_timezone_FailWithoutTZInfo(): dt = datetime(2024, 3, 30, 2, 0) with pytest.raises(ValueError): datetime_.dt_to_timezone(dt, TIMEZONE_CEST) def test_cut_microseconds(): dt = datetime(2024, 3, 30, 2, 0, 0, 600) new_dt = datetime_.cut_dt_microseconds(dt) assert new_dt == datetime(2024, 3, 30, 2, 0, 0, 0)