Repository navigation
Commit bd89085
authored
Replace deprecated utcnow() and utcfromtimestamp() with timezone-aware equivalents (#967)
* Replace deprecated utcnow() and utcfromtimestamp() with timezone-aware equivalents
Python 3.12 deprecated `datetime.utcnow()` and `datetime.utcfromtimestamp()`;
both are scheduled for removal in a future Python version. This change
replaces all internal uses with the non-deprecated, behavior-preserving
equivalents:
- `datetime.now(timezone.utc).replace(tzinfo=None)` for `datetime.utcnow()`
- `datetime.fromtimestamp(t, tz=timezone.utc).replace(tzinfo=None)` for
`datetime.utcfromtimestamp(t)`
This eliminates 4 DeprecationWarnings from the test suite without
changing any public behavior. The existing test suite continues to pass
(1830 passed) with `-W error::DeprecationWarning`.
* Add regression coverage for utcfromtimestamp behavior
The existing test_utcfromtimestamp only exercises t=0, which incidentally
gives back a value where every field happens to be zero. That makes it
weak against regressions in field handling, microsecond preservation,
and return-type semantics.
Add three focused tests that lock those down without changing or
removing any existing behavior:
- test_utcfromtimestamp_known_value: asserts that a specific non-zero
Unix timestamp (1577836800 = 2020-01-01T00:00:00 UTC) produces the
expected year/month/day/hour/minute/second/microsecond values.
- test_utcfromtimestamp_preserves_microseconds: asserts that a fractional
timestamp passes its microseconds through to the resulting DateTime,
protecting the precision contract of the method.
- test_utcfromtimestamp_returns_naive_pendulum_datetime: asserts that
the returned object is a pendulum.DateTime instance and is naive
(tzinfo is None), which is the documented behavior the method has had
before and after the deprecation cleanup in this PR.
All three tests are additions; no existing test is modified or removed.
Full suite: 1833 passed, 5 skipped (was 1830 passed) with
-W error::DeprecationWarning.1 parent 6247ff9 commit bd89085
3 files changed
Lines changed: 39 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1257 | 1257 | | |
1258 | 1258 | | |
1259 | 1259 | | |
1260 | | - | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
1261 | 1266 | | |
1262 | 1267 | | |
1263 | 1268 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
105 | | - | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
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 | + | |
110 | 139 | | |
111 | 140 | | |
112 | 141 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | | - | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
0 commit comments