Commit 1e0e6ddc authored by nmartensen's avatar nmartensen
Browse files

drop dependency on jdcal

--HG--
branch : 3.0
parent b45f0332b09f
......@@ -9,18 +9,12 @@ from datetime import timedelta, tzinfo
from math import isnan
import re
from jdcal import (
gcal2jd,
jd2gcal,
MJD_0
)
# constants
MAC_EPOCH = datetime.date(1904, 1, 1)
WINDOWS_EPOCH = datetime.date(1899, 12, 30)
CALENDAR_WINDOWS_1900 = sum(gcal2jd(WINDOWS_EPOCH.year, WINDOWS_EPOCH.month, WINDOWS_EPOCH.day))
CALENDAR_MAC_1904 = sum(gcal2jd(MAC_EPOCH.year, MAC_EPOCH.month, MAC_EPOCH.day))
MAC_EPOCH = datetime.datetime(1904, 1, 1)
WINDOWS_EPOCH = datetime.datetime(1899, 12, 30)
CALENDAR_WINDOWS_1900 = 2415018.5 # Julian date of WINDOWS_EPOCH
CALENDAR_MAC_1904 = 2416480.5 # Julian date of MAC_EPOCH
SECS_PER_DAY = 86400
EPOCH = datetime.datetime.utcfromtimestamp(0)
......@@ -70,6 +64,12 @@ def from_ISO8601(formatted_string):
return dt
def _get_epoch(offset):
if offset == CALENDAR_MAC_1904:
return MAC_EPOCH
return WINDOWS_EPOCH
def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
if isinstance(dt, datetime.time):
return time_to_days(dt)
......@@ -77,7 +77,7 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
return timedelta_to_days(dt)
if isnan(dt.year): # Pandas supports Not a Date
return
jul = sum(gcal2jd(dt.year, dt.month, dt.day)) - offset
jul = (datetime.datetime(dt.year, dt.month, dt.day) - _get_epoch(offset)).days
if jul <= 60 and offset == CALENDAR_WINDOWS_1900:
jul -= 1
if hasattr(dt, 'time'):
......@@ -88,19 +88,13 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
def from_excel(value, offset=CALENDAR_WINDOWS_1900):
if value is None:
return
_, fraction = divmod(value, 1)
day, fraction = divmod(value, 1)
diff = datetime.timedelta(milliseconds=round(fraction * SECS_PER_DAY * 1000.))
if 0 <= value < 1 and diff.days == 0:
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)
if not jumped:
return datetime.datetime(*parts[:3]) + diff
else:
return datetime.datetime(*parts[:3] + [0])
day += 1
return _get_epoch(offset) + datetime.timedelta(days=day) + diff
class GMT(tzinfo):
......
......@@ -76,7 +76,7 @@ setup(
license=__license__,
python_requires=">=3.6, ",
install_requires=[
'jdcal', 'et_xmlfile',
'et_xmlfile',
],
project_urls={
'Documentation': 'https://openpyxl.readthedocs.io/en/stable/',
......
Supports Markdown
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