Skip to content

Commit 0638773

Browse files
Add tests for datetime_to_djd and djd_to_datetime
1 parent 9347f12 commit 0638773

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

pvlib/tests/test_tools.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,65 @@ def test_normalize_max2one(data_in, expected):
272272
],
273273
)
274274
def test_localize_to_utc(input, expected):
275-
"""Test localization of naive time to UTC using the specified location."""
276275
got = tools.localize_to_utc(**input)
277276
if isinstance(got, (pd.Series, pd.DataFrame)):
278277
assert got.equals(expected)
279278
else:
280279
assert got == expected
280+
281+
282+
@pytest.mark.parametrize(
283+
'input,expected',
284+
[
285+
(
286+
{
287+
"time": datetime(1974, 6, 22, 18, 30, 15, tzinfo=ZoneInfo("Etc/GMT+5"))
288+
},
289+
27201.47934027778,
290+
),
291+
(
292+
{
293+
"time": datetime(1974, 6, 22, 23, 30, 15, tzinfo=ZoneInfo("UTC"))
294+
},
295+
27201.47934027778,
296+
),
297+
],
298+
ids=[
299+
"datetime.datetime with tzinfo",
300+
"datetime.datetime",
301+
],
302+
)
303+
def test_datetime_to_djd(input, expected):
304+
assert tools.datetime_to_djd(input["time"]) == expected
305+
306+
307+
@pytest.mark.parametrize(
308+
'input,expected',
309+
[
310+
(
311+
{
312+
"djd": 27201.47934027778,
313+
"tz": "Etc/GMT+5",
314+
},
315+
datetime(1974, 6, 22, 18, 30, 15, tzinfo=ZoneInfo("Etc/GMT+5")),
316+
),
317+
(
318+
{
319+
"djd": 27201.47934027778,
320+
"tz": None,
321+
},
322+
datetime(1974, 6, 22, 23, 30, 15, tzinfo=ZoneInfo("UTC")),
323+
),
324+
],
325+
ids=[
326+
"djd with tzinfo",
327+
"djd",
328+
],
329+
)
330+
def test_djd_to_datetime(input, expected):
331+
if input["tz"] is not None:
332+
got = tools.djd_to_datetime(input["djd"])
333+
else:
334+
got = tools.djd_to_datetime(input["djd"], tz="Etc/GMT+5")
335+
336+
assert got == expected

pvlib/tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def atand(number):
119119

120120
def localize_to_utc(time, location):
121121
"""
122-
Converts or localizes a time series to UTC.
122+
Converts time to UTC, localizing if necessary using location.
123123
124124
Parameters
125125
----------
@@ -129,7 +129,7 @@ def localize_to_utc(time, location):
129129
130130
Returns
131131
-------
132-
pandas object localized to UTC.
132+
datetime.datetime or pandas object localized to UTC.
133133
"""
134134
if isinstance(time, dt.datetime):
135135
if time.tzinfo is None:

0 commit comments

Comments
 (0)