summaryrefslogtreecommitdiffstats
path: root/tests/time/test_sub.py
blob: 1a957ad7d81a22505de4ca6ee11fb76ea0f0b094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import annotations

from datetime import time
from datetime import timedelta

import pytest
import pytz

import pendulum

from pendulum import Time
from tests.conftest import assert_duration


def test_sub_hours_positive():
    assert Time(0, 0, 0).subtract(hours=1).hour == 23


def test_sub_hours_zero():
    assert Time(0, 0, 0).subtract(hours=0).hour == 0


def test_sub_hours_negative():
    assert Time(0, 0, 0).subtract(hours=-1).hour == 1


def test_sub_minutes_positive():
    assert Time(0, 0, 0).subtract(minutes=1).minute == 59


def test_sub_minutes_zero():
    assert Time(0, 0, 0).subtract(minutes=0).minute == 0


def test_sub_minutes_negative():
    assert Time(0, 0, 0).subtract(minutes=-1).minute == 1


def test_sub_seconds_positive():
    assert Time(0, 0, 0).subtract(seconds=1).second == 59


def test_sub_seconds_zero():
    assert Time(0, 0, 0).subtract(seconds=0).second == 0


def test_sub_seconds_negative():
    assert Time(0, 0, 0).subtract(seconds=-1).second == 1


def test_subtract_timedelta():
    delta = timedelta(seconds=16, microseconds=654321)
    d = Time(3, 12, 15, 777777)

    d = d.subtract_timedelta(delta)
    assert d.minute == 11
    assert d.second == 59
    assert d.microsecond == 123456

    d = Time(3, 12, 15, 777777)

    d = d - delta
    assert d.minute == 11
    assert d.second == 59
    assert d.microsecond == 123456


def test_add_timedelta_with_days():
    delta = timedelta(days=3, seconds=45, microseconds=123456)
    d = Time(3, 12, 15, 654321)

    with pytest.raises(TypeError):
        d.subtract_timedelta(delta)


def test_subtract_invalid_type():
    d = Time(0, 0, 0)

    with pytest.raises(TypeError):
        d - "ab"

    with pytest.raises(TypeError):
        "ab" - d


def test_subtract_time():
    t = Time(12, 34, 56)
    t1 = Time(1, 1, 1)
    t2 = time(1, 1, 1)
    t3 = time(1, 1, 1, tzinfo=pytz.timezone("Europe/Paris"))

    diff = t - t1
    assert isinstance(diff, pendulum.Duration)
    assert_duration(diff, 0, hours=11, minutes=33, seconds=55)

    diff = t1 - t
    assert isinstance(diff, pendulum.Duration)
    assert_duration(diff, 0, hours=-11, minutes=-33, seconds=-55)

    diff = t - t2
    assert isinstance(diff, pendulum.Duration)
    assert_duration(diff, 0, hours=11, minutes=33, seconds=55)

    diff = t2 - t
    assert isinstance(diff, pendulum.Duration)
    assert_duration(diff, 0, hours=-11, minutes=-33, seconds=-55)

    with pytest.raises(TypeError):
        t - t3

    with pytest.raises(TypeError):
        t3 - t