aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test_datetime_helpers.py
blob: 5f5470a681461d1ed0230ba00f97fd19c5952ee8 (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright 2017, Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import calendar
import datetime

import pytest

from google.api_core import datetime_helpers
from google.protobuf import timestamp_pb2


ONE_MINUTE_IN_MICROSECONDS = 60 * 1e6


def test_utcnow():
    result = datetime_helpers.utcnow()
    assert isinstance(result, datetime.datetime)


def test_to_milliseconds():
    dt = datetime.datetime(1970, 1, 1, 0, 0, 1, tzinfo=datetime.timezone.utc)
    assert datetime_helpers.to_milliseconds(dt) == 1000


def test_to_microseconds():
    microseconds = 314159
    dt = datetime.datetime(1970, 1, 1, 0, 0, 0, microsecond=microseconds)
    assert datetime_helpers.to_microseconds(dt) == microseconds


def test_to_microseconds_non_utc():
    zone = datetime.timezone(datetime.timedelta(minutes=-1))
    dt = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=zone)
    assert datetime_helpers.to_microseconds(dt) == ONE_MINUTE_IN_MICROSECONDS


def test_to_microseconds_naive():
    microseconds = 314159
    dt = datetime.datetime(1970, 1, 1, 0, 0, 0, microsecond=microseconds, tzinfo=None)
    assert datetime_helpers.to_microseconds(dt) == microseconds


def test_from_microseconds():
    five_mins_from_epoch_in_microseconds = 5 * ONE_MINUTE_IN_MICROSECONDS
    five_mins_from_epoch_datetime = datetime.datetime(
        1970, 1, 1, 0, 5, 0, tzinfo=datetime.timezone.utc
    )

    result = datetime_helpers.from_microseconds(five_mins_from_epoch_in_microseconds)

    assert result == five_mins_from_epoch_datetime


def test_from_iso8601_date():
    today = datetime.date.today()
    iso_8601_today = today.strftime("%Y-%m-%d")

    assert datetime_helpers.from_iso8601_date(iso_8601_today) == today


def test_from_iso8601_time():
    assert datetime_helpers.from_iso8601_time("12:09:42") == datetime.time(12, 9, 42)


def test_from_rfc3339():
    value = "2009-12-17T12:44:32.123456Z"
    assert datetime_helpers.from_rfc3339(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, 123456, datetime.timezone.utc
    )


def test_from_rfc3339_nanos():
    value = "2009-12-17T12:44:32.123456Z"
    assert datetime_helpers.from_rfc3339_nanos(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, 123456, datetime.timezone.utc
    )


def test_from_rfc3339_without_nanos():
    value = "2009-12-17T12:44:32Z"
    assert datetime_helpers.from_rfc3339(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, 0, datetime.timezone.utc
    )


def test_from_rfc3339_nanos_without_nanos():
    value = "2009-12-17T12:44:32Z"
    assert datetime_helpers.from_rfc3339_nanos(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, 0, datetime.timezone.utc
    )


@pytest.mark.parametrize(
    "truncated, micros",
    [
        ("12345678", 123456),
        ("1234567", 123456),
        ("123456", 123456),
        ("12345", 123450),
        ("1234", 123400),
        ("123", 123000),
        ("12", 120000),
        ("1", 100000),
    ],
)
def test_from_rfc3339_with_truncated_nanos(truncated, micros):
    value = "2009-12-17T12:44:32.{}Z".format(truncated)
    assert datetime_helpers.from_rfc3339(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, micros, datetime.timezone.utc
    )


def test_from_rfc3339_nanos_is_deprecated():
    value = "2009-12-17T12:44:32.123456Z"

    result = datetime_helpers.from_rfc3339(value)
    result_nanos = datetime_helpers.from_rfc3339_nanos(value)

    assert result == result_nanos


@pytest.mark.parametrize(
    "truncated, micros",
    [
        ("12345678", 123456),
        ("1234567", 123456),
        ("123456", 123456),
        ("12345", 123450),
        ("1234", 123400),
        ("123", 123000),
        ("12", 120000),
        ("1", 100000),
    ],
)
def test_from_rfc3339_nanos_with_truncated_nanos(truncated, micros):
    value = "2009-12-17T12:44:32.{}Z".format(truncated)
    assert datetime_helpers.from_rfc3339_nanos(value) == datetime.datetime(
        2009, 12, 17, 12, 44, 32, micros, datetime.timezone.utc
    )


def test_from_rfc3339_wo_nanos_raise_exception():
    value = "2009-12-17T12:44:32"
    with pytest.raises(ValueError):
        datetime_helpers.from_rfc3339(value)


def test_from_rfc3339_w_nanos_raise_exception():
    value = "2009-12-17T12:44:32.123456"
    with pytest.raises(ValueError):
        datetime_helpers.from_rfc3339(value)


def test_to_rfc3339():
    value = datetime.datetime(2016, 4, 5, 13, 30, 0)
    expected = "2016-04-05T13:30:00.000000Z"
    assert datetime_helpers.to_rfc3339(value) == expected


def test_to_rfc3339_with_utc():
    value = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=datetime.timezone.utc)
    expected = "2016-04-05T13:30:00.000000Z"
    assert datetime_helpers.to_rfc3339(value, ignore_zone=False) == expected


def test_to_rfc3339_with_non_utc():
    zone = datetime.timezone(datetime.timedelta(minutes=-60))
    value = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=zone)
    expected = "2016-04-05T14:30:00.000000Z"
    assert datetime_helpers.to_rfc3339(value, ignore_zone=False) == expected


def test_to_rfc3339_with_non_utc_ignore_zone():
    zone = datetime.timezone(datetime.timedelta(minutes=-60))
    value = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=zone)
    expected = "2016-04-05T13:30:00.000000Z"
    assert datetime_helpers.to_rfc3339(value, ignore_zone=True) == expected


class Test_DateTimeWithNanos(object):
    @staticmethod
    def test_ctor_wo_nanos():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, 123456
        )
        assert stamp.year == 2016
        assert stamp.month == 12
        assert stamp.day == 20
        assert stamp.hour == 21
        assert stamp.minute == 13
        assert stamp.second == 47
        assert stamp.microsecond == 123456
        assert stamp.nanosecond == 0

    @staticmethod
    def test_ctor_w_nanos():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=123456789
        )
        assert stamp.year == 2016
        assert stamp.month == 12
        assert stamp.day == 20
        assert stamp.hour == 21
        assert stamp.minute == 13
        assert stamp.second == 47
        assert stamp.microsecond == 123456
        assert stamp.nanosecond == 123456789

    @staticmethod
    def test_ctor_w_micros_positional_and_nanos():
        with pytest.raises(TypeError):
            datetime_helpers.DatetimeWithNanoseconds(
                2016, 12, 20, 21, 13, 47, 123456, nanosecond=123456789
            )

    @staticmethod
    def test_ctor_w_micros_keyword_and_nanos():
        with pytest.raises(TypeError):
            datetime_helpers.DatetimeWithNanoseconds(
                2016, 12, 20, 21, 13, 47, microsecond=123456, nanosecond=123456789
            )

    @staticmethod
    def test_rfc3339_wo_nanos():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, 123456
        )
        assert stamp.rfc3339() == "2016-12-20T21:13:47.123456Z"

    @staticmethod
    def test_rfc3339_wo_nanos_w_leading_zero():
        stamp = datetime_helpers.DatetimeWithNanoseconds(2016, 12, 20, 21, 13, 47, 1234)
        assert stamp.rfc3339() == "2016-12-20T21:13:47.001234Z"

    @staticmethod
    def test_rfc3339_w_nanos():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=123456789
        )
        assert stamp.rfc3339() == "2016-12-20T21:13:47.123456789Z"

    @staticmethod
    def test_rfc3339_w_nanos_w_leading_zero():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=1234567
        )
        assert stamp.rfc3339() == "2016-12-20T21:13:47.001234567Z"

    @staticmethod
    def test_rfc3339_w_nanos_no_trailing_zeroes():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=100000000
        )
        assert stamp.rfc3339() == "2016-12-20T21:13:47.1Z"

    @staticmethod
    def test_rfc3339_w_nanos_w_leading_zero_and_no_trailing_zeros():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=1234500
        )
        assert stamp.rfc3339() == "2016-12-20T21:13:47.0012345Z"

    @staticmethod
    def test_from_rfc3339_w_invalid():
        stamp = "2016-12-20T21:13:47"
        with pytest.raises(ValueError):
            datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(stamp)

    @staticmethod
    def test_from_rfc3339_wo_fraction():
        timestamp = "2016-12-20T21:13:47Z"
        expected = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, tzinfo=datetime.timezone.utc
        )
        stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp)
        assert stamp == expected

    @staticmethod
    def test_from_rfc3339_w_partial_precision():
        timestamp = "2016-12-20T21:13:47.1Z"
        expected = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, microsecond=100000, tzinfo=datetime.timezone.utc
        )
        stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp)
        assert stamp == expected

    @staticmethod
    def test_from_rfc3339_w_full_precision():
        timestamp = "2016-12-20T21:13:47.123456789Z"
        expected = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=123456789, tzinfo=datetime.timezone.utc
        )
        stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp)
        assert stamp == expected

    @staticmethod
    @pytest.mark.parametrize(
        "fractional, nanos",
        [
            ("12345678", 123456780),
            ("1234567", 123456700),
            ("123456", 123456000),
            ("12345", 123450000),
            ("1234", 123400000),
            ("123", 123000000),
            ("12", 120000000),
            ("1", 100000000),
        ],
    )
    def test_from_rfc3339_test_nanoseconds(fractional, nanos):
        value = "2009-12-17T12:44:32.{}Z".format(fractional)
        assert (
            datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(value).nanosecond
            == nanos
        )

    @staticmethod
    def test_timestamp_pb_wo_nanos_naive():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, 123456
        )
        delta = (
            stamp.replace(tzinfo=datetime.timezone.utc) - datetime_helpers._UTC_EPOCH
        )
        seconds = int(delta.total_seconds())
        nanos = 123456000
        timestamp = timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos)
        assert stamp.timestamp_pb() == timestamp

    @staticmethod
    def test_timestamp_pb_w_nanos():
        stamp = datetime_helpers.DatetimeWithNanoseconds(
            2016, 12, 20, 21, 13, 47, nanosecond=123456789, tzinfo=datetime.timezone.utc
        )
        delta = stamp - datetime_helpers._UTC_EPOCH
        timestamp = timestamp_pb2.Timestamp(
            seconds=int(delta.total_seconds()), nanos=123456789
        )
        assert stamp.timestamp_pb() == timestamp

    @staticmethod
    def test_from_timestamp_pb_wo_nanos():
        when = datetime.datetime(
            2016, 12, 20, 21, 13, 47, 123456, tzinfo=datetime.timezone.utc
        )
        delta = when - datetime_helpers._UTC_EPOCH
        seconds = int(delta.total_seconds())
        timestamp = timestamp_pb2.Timestamp(seconds=seconds)

        stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(timestamp)

        assert _to_seconds(when) == _to_seconds(stamp)
        assert stamp.microsecond == 0
        assert stamp.nanosecond == 0
        assert stamp.tzinfo == datetime.timezone.utc

    @staticmethod
    def test_from_timestamp_pb_w_nanos():
        when = datetime.datetime(
            2016, 12, 20, 21, 13, 47, 123456, tzinfo=datetime.timezone.utc
        )
        delta = when - datetime_helpers._UTC_EPOCH
        seconds = int(delta.total_seconds())
        timestamp = timestamp_pb2.Timestamp(seconds=seconds, nanos=123456789)

        stamp = datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(timestamp)

        assert _to_seconds(when) == _to_seconds(stamp)
        assert stamp.microsecond == 123456
        assert stamp.nanosecond == 123456789
        assert stamp.tzinfo == datetime.timezone.utc


def _to_seconds(value):
    """Convert a datetime to seconds since the unix epoch.

    Args:
        value (datetime.datetime): The datetime to covert.

    Returns:
        int: Microseconds since the unix epoch.
    """
    assert value.tzinfo is datetime.timezone.utc
    return calendar.timegm(value.timetuple())