Commit 2704cbb6 authored by nmartensen's avatar nmartensen
Browse files

datetime.(from_excel, to_excel): drop excel bug workaround

The specification is clear on how to (de)serialize dates in the 1900
date system. Instead of attempting to follow what Excel does (which we
can't do fully anyway), let's follow the specification. This increases
interoperability and allows correct timedelta calculations for time
differences above 60 days.
parent 25286d0f35b3
Pipeline #16943 passed with stage
in 5 minutes and 38 seconds
......@@ -77,8 +77,6 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
if isnan(dt.year): # Pandas supports Not a Date
return
jul = sum(gcal2jd(dt.year, dt.month, dt.day)) - offset
if jul <= 60 and offset == CALENDAR_WINDOWS_1900:
jul -= 1
if hasattr(dt, 'time'):
jul += time_to_days(dt)
return jul
......@@ -91,8 +89,6 @@ def from_excel(value, offset=CALENDAR_WINDOWS_1900):
diff = datetime.timedelta(days=fraction)
if 0 <= value < 1:
return days_to_time(diff)
if 1 < value < 60 and offset == CALENDAR_WINDOWS_1900:
value += 1
parts = list(jd2gcal(MJD_0, value + offset - MJD_0))
jumped = (parts[-1] == 0 and fraction > 0)
......
......@@ -48,14 +48,14 @@ def test_from_iso(value, expected):
@pytest.mark.parametrize("value, expected",
[
(date(1899, 12, 31), 0),
(date(1900, 1, 15), 15),
(date(1900, 2, 28), 59),
(datetime(1900, 2, 28, 21, 0, 0), 59.875),
(date(1899, 12, 30), 0),
(date(1900, 1, 15), 16),
(date(1900, 2, 28), 60),
(datetime(1900, 2, 28, 21, 0, 0), 60.875),
(date(1900, 3, 1), 61),
(datetime(2010, 1, 18, 14, 15, 20, 1600), 40196.5939815),
(date(2009, 12, 20), 40167),
(datetime(1506, 10, 15), -143618.0),
(datetime(1506, 10, 15), -143617.0),
(time(0), 0),
(time(6, 0), 0.25),
(timedelta(hours=6), 0.25),
......@@ -89,9 +89,9 @@ def test_to_excel_mac(value, expected):
[
(40167, datetime(2009, 12, 20)),
(21980, datetime(1960, 3, 5)),
(59, datetime(1900, 2, 28)),
(60, datetime(1900, 2, 28)),
(-25063, datetime(1831, 5, 18, 0, 0)),
(59.875, datetime(1900, 2, 28, 21, 0, 0)),
(59.875, datetime(1900, 2, 27, 21, 0, 0)),
(60, datetime(1900, 2, 28, 0, 0)),
(60.5, datetime(1900, 2, 28, 12, 0)),
(61, datetime(1900, 3, 1, 0, 0)),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment