Skip to content

Commit efc4905

Browse files
committed
Fix handling of ambiguous times during DST shift
1 parent 6354443 commit efc4905

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pvlib/solarposition.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,14 @@ def hour_angle(times, longitude, equation_of_time):
13921392
times = times.tz_localize('utc')
13931393
tzs = np.array([ts.utcoffset().total_seconds() for ts in times]) / 3600
13941394

1395+
is_dst = []
1396+
for time in times:
1397+
dst = time.tzinfo.dst(time)
1398+
if dst is None:
1399+
is_dst.append(False)
1400+
else:
1401+
is_dst.append(dst > dt.timedelta(seconds=0))
1402+
13951403
# Some timezones have a DST shift at midnight:
13961404
# 11:59pm -> 1:00am - results in a nonexistent midnight
13971405
# 12:59am -> 12:00am - results in an ambiguous midnight
@@ -1401,7 +1409,7 @@ def hour_angle(times, longitude, equation_of_time):
14011409
# Use Pandas functionality for shifting nonexistent times forward
14021410
# or infering ambiguous times (which arose from normalizing)
14031411
normalized_times = naive_normalized_times.tz_localize(
1404-
times.tz, nonexistent='shift_forward', ambiguous='infer')
1412+
times.tz, nonexistent='shift_forward', ambiguous=is_dst)
14051413

14061414
hrs_minus_tzs = (times - normalized_times) / pd.Timedelta('1h') - tzs
14071415

pvlib/tests/test_solarposition.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,12 +677,13 @@ def test_hour_angle():
677677
def test_hour_angle_with_tricky_timezones():
678678
# tests timezones that have a DST shift at midnight
679679

680-
eot = np.array([-3.935172, -4.117227])
680+
eot = np.array([-3.935172, -4.117227, -4.026295])
681681

682682
longitude = 70.6693
683683
times = pd.DatetimeIndex([
684-
'2014-09-07 10:00:00',
685-
'2014-09-07 11:00:00',
684+
'2014-11-02 00:00:00',
685+
'2014-09-07 01:00:00',
686+
'2014-09-07 02:00:00',
686687
]).tz_localize('America/Santiago')
687688

688689
with pytest.raises(pytz.exceptions.NonExistentTimeError):
@@ -693,9 +694,10 @@ def test_hour_angle_with_tricky_timezones():
693694

694695
longitude = 82.3666
695696
times = pd.DatetimeIndex([
696-
'2014-11-02 10:00:00',
697-
'2014-11-02 11:00:00',
698-
]).tz_localize('America/Havana')
697+
'2014-11-02 00:00:00',
698+
'2014-11-02 01:00:00',
699+
'2014-11-02 02:00:00',
700+
]).tz_localize('America/Havana', ambiguous=[True, False, False])
699701

700702
with pytest.raises(pytz.exceptions.AmbiguousTimeError):
701703
times.normalize()

0 commit comments

Comments
 (0)