summaryrefslogtreecommitdiffstats
path: root/tests/date/test_diff.py
blob: 56358cc8305ec4c0a1e679e2433f002bd16be396 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from __future__ import annotations

from datetime import date

import pytest

import pendulum


@pytest.fixture
def today():
    return pendulum.today().date()


def test_diff_in_years_positive():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(years=1)).in_years() == 1


def test_diff_in_years_negative_with_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1), False).in_years() == -1


def test_diff_in_years_negative_no_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1)).in_years() == 1


def test_diff_in_years_vs_default_now(today):
    assert today.subtract(years=1).diff().in_years() == 1


def test_diff_in_years_ensure_is_truncated():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(years=1).add(months=7)).in_years() == 1


def test_diff_in_months_positive():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(years=1).add(months=1)).in_months() == 13


def test_diff_in_months_negative_with_sign():
    dt = pendulum.date(2000, 1, 1)

    assert dt.diff(dt.subtract(years=1).add(months=1), False).in_months() == -11


def test_diff_in_months_negative_no_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1).add(months=1)).in_months() == 11


def test_diff_in_months_vs_default_now(today):
    assert today.subtract(years=1).diff().in_months() == 12


def test_diff_in_months_ensure_is_truncated():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(months=1).add(days=16)).in_months() == 1


def test_diff_in_days_positive():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(years=1)).in_days() == 366


def test_diff_in_days_negative_with_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1), False).in_days() == -365


def test_diff_in_days_negative_no_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1)).in_days() == 365


def test_diff_in_days_vs_default_now(today):
    assert today.subtract(weeks=1).diff().in_days() == 7


def test_diff_in_weeks_positive():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(years=1)).in_weeks() == 52


def test_diff_in_weeks_negative_with_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1), False).in_weeks() == -52


def test_diff_in_weeks_negative_no_sign():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.subtract(years=1)).in_weeks() == 52


def test_diff_in_weeks_vs_default_now(today):
    assert today.subtract(weeks=1).diff().in_weeks() == 1


def test_diff_in_weeks_ensure_is_truncated():
    dt = pendulum.date(2000, 1, 1)
    assert dt.diff(dt.add(weeks=1).subtract(days=1)).in_weeks() == 0


def test_diff_for_humans_now_and_day(today):
    assert today.subtract(days=1).diff_for_humans() == "1 day ago"


def test_diff_for_humans_now_and_days(today):
    assert today.subtract(days=2).diff_for_humans() == "2 days ago"


def test_diff_for_humans_now_and_nearly_week(today):
    assert today.subtract(days=6).diff_for_humans() == "6 days ago"


def test_diff_for_humans_now_and_week(today):
    assert today.subtract(weeks=1).diff_for_humans() == "1 week ago"


def test_diff_for_humans_now_and_weeks(today):
    assert today.subtract(weeks=2).diff_for_humans() == "2 weeks ago"


def test_diff_for_humans_now_and_nearly_month(today):
    assert today.subtract(weeks=3).diff_for_humans() == "3 weeks ago"


def test_diff_for_humans_now_and_month():
    with pendulum.travel_to(pendulum.datetime(2016, 4, 1)):
        today = pendulum.today().date()

        assert today.subtract(weeks=4).diff_for_humans() == "4 weeks ago"
        assert today.subtract(months=1).diff_for_humans() == "1 month ago"

    with pendulum.travel_to(pendulum.datetime(2017, 3, 1)):
        today = pendulum.today().date()

        assert today.subtract(weeks=4).diff_for_humans() == "1 month ago"


def test_diff_for_humans_now_and_months(today):
    assert today.subtract(months=2).diff_for_humans() == "2 months ago"


def test_diff_for_humans_now_and_nearly_year(today):
    assert today.subtract(months=11).diff_for_humans() == "11 months ago"


def test_diff_for_humans_now_and_year(today):
    assert today.subtract(years=1).diff_for_humans() == "1 year ago"


def test_diff_for_humans_now_and_years(today):
    assert today.subtract(years=2).diff_for_humans() == "2 years ago"


def test_diff_for_humans_now_and_future_day(today):
    assert today.add(days=1).diff_for_humans() == "in 1 day"


def test_diff_for_humans_now_and_future_days(today):
    assert today.add(days=2).diff_for_humans() == "in 2 days"


def test_diff_for_humans_now_and_nearly_future_week(today):
    assert today.add(days=6).diff_for_humans() == "in 6 days"


def test_diff_for_humans_now_and_future_week(today):
    assert today.add(weeks=1).diff_for_humans() == "in 1 week"


def test_diff_for_humans_now_and_future_weeks(today):
    assert today.add(weeks=2).diff_for_humans() == "in 2 weeks"


def test_diff_for_humans_now_and_nearly_future_month(today):
    assert today.add(weeks=3).diff_for_humans() == "in 3 weeks"


def test_diff_for_humans_now_and_future_month():
    with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
        today = pendulum.today("UTC").date()

        assert today.add(weeks=4).diff_for_humans() == "in 4 weeks"
        assert today.add(months=1).diff_for_humans() == "in 1 month"

    with pendulum.travel_to(pendulum.datetime(2017, 3, 31)):
        today = pendulum.today("UTC").date()

        assert today.add(months=1).diff_for_humans() == "in 1 month"

    with pendulum.travel_to(pendulum.datetime(2017, 4, 30)):
        today = pendulum.today("UTC").date()

        assert today.add(months=1).diff_for_humans() == "in 1 month"

    with pendulum.travel_to(pendulum.datetime(2017, 1, 31)):
        today = pendulum.today("UTC").date()

        assert today.add(weeks=4).diff_for_humans() == "in 1 month"


def test_diff_for_humans_now_and_future_months(today):
    assert today.add(months=2).diff_for_humans() == "in 2 months"


def test_diff_for_humans_now_and_nearly_future_year(today):
    assert today.add(months=11).diff_for_humans() == "in 11 months"


def test_diff_for_humans_now_and_future_year(today):
    assert today.add(years=1).diff_for_humans() == "in 1 year"


def test_diff_for_humans_now_and_future_years(today):
    assert today.add(years=2).diff_for_humans() == "in 2 years"


def test_diff_for_humans_other_and_day(today):
    assert today.diff_for_humans(today.add(days=1)) == "1 day before"


def test_diff_for_humans_other_and_days(today):
    assert today.diff_for_humans(today.add(days=2)) == "2 days before"


def test_diff_for_humans_other_and_nearly_week(today):
    assert today.diff_for_humans(today.add(days=6)) == "6 days before"


def test_diff_for_humans_other_and_week(today):
    assert today.diff_for_humans(today.add(weeks=1)) == "1 week before"


def test_diff_for_humans_other_and_weeks(today):
    assert today.diff_for_humans(today.add(weeks=2)) == "2 weeks before"


def test_diff_for_humans_other_and_nearly_month(today):
    assert today.diff_for_humans(today.add(weeks=3)) == "3 weeks before"


def test_diff_for_humans_other_and_month():
    with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.add(weeks=4)) == "4 weeks before"
        assert today.diff_for_humans(today.add(months=1)) == "1 month before"

    with pendulum.travel_to(pendulum.datetime(2017, 3, 31)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.add(months=1)) == "1 month before"

    with pendulum.travel_to(pendulum.datetime(2017, 4, 30)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.add(months=1)) == "1 month before"

    with pendulum.travel_to(pendulum.datetime(2017, 1, 31)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.add(weeks=4)) == "1 month before"


def test_diff_for_humans_other_and_months(today):
    assert today.diff_for_humans(today.add(months=2)) == "2 months before"


def test_diff_for_humans_other_and_nearly_year(today):
    assert today.diff_for_humans(today.add(months=11)) == "11 months before"


def test_diff_for_humans_other_and_year(today):
    assert today.diff_for_humans(today.add(years=1)) == "1 year before"


def test_diff_for_humans_other_and_years(today):
    assert today.diff_for_humans(today.add(years=2)) == "2 years before"


def test_diff_for_humans_other_and_future_day(today):
    assert today.diff_for_humans(today.subtract(days=1)) == "1 day after"


def test_diff_for_humans_other_and_future_days(today):
    assert today.diff_for_humans(today.subtract(days=2)) == "2 days after"


def test_diff_for_humans_other_and_nearly_future_week(today):
    assert today.diff_for_humans(today.subtract(days=6)) == "6 days after"


def test_diff_for_humans_other_and_future_week(today):
    assert today.diff_for_humans(today.subtract(weeks=1)) == "1 week after"


def test_diff_for_humans_other_and_future_weeks(today):
    assert today.diff_for_humans(today.subtract(weeks=2)) == "2 weeks after"


def test_diff_for_humans_other_and_nearly_future_month(today):
    assert today.diff_for_humans(today.subtract(weeks=3)) == "3 weeks after"


def test_diff_for_humans_other_and_future_month():
    with pendulum.travel_to(pendulum.datetime(2016, 3, 1)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.subtract(weeks=4)) == "4 weeks after"
        assert today.diff_for_humans(today.subtract(months=1)) == "1 month after"

    with pendulum.travel_to(pendulum.datetime(2017, 2, 28)):
        today = pendulum.today().date()

        assert today.diff_for_humans(today.subtract(weeks=4)) == "1 month after"


def test_diff_for_humans_other_and_future_months(today):
    assert today.diff_for_humans(today.subtract(months=2)) == "2 months after"


def test_diff_for_humans_other_and_nearly_future_year(today):
    assert today.diff_for_humans(today.subtract(months=11)) == "11 months after"


def test_diff_for_humans_other_and_future_year(today):
    assert today.diff_for_humans(today.subtract(years=1)) == "1 year after"


def test_diff_for_humans_other_and_future_years(today):
    assert today.diff_for_humans(today.subtract(years=2)) == "2 years after"


def test_diff_for_humans_absolute_days(today):
    assert today.diff_for_humans(today.subtract(days=2), True) == "2 days"
    assert today.diff_for_humans(today.add(days=2), True) == "2 days"


def test_diff_for_humans_absolute_weeks(today):
    assert today.diff_for_humans(today.subtract(weeks=2), True) == "2 weeks"
    assert today.diff_for_humans(today.add(weeks=2), True) == "2 weeks"


def test_diff_for_humans_absolute_months(today):
    assert today.diff_for_humans(today.subtract(months=2), True) == "2 months"
    assert today.diff_for_humans(today.add(months=2), True) == "2 months"


def test_diff_for_humans_absolute_years(today):
    assert today.diff_for_humans(today.subtract(years=1), True) == "1 year"
    assert today.diff_for_humans(today.add(years=1), True) == "1 year"


def test_subtraction():
    d = pendulum.date(2016, 7, 5)
    future_dt = date(2016, 7, 6)
    future = d.add(days=1)

    assert (future - d).total_seconds() == 86400
    assert (future_dt - d).total_seconds() == 86400