def to_excel(dt, offset=CALENDAR_WINDOWS_1900): 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
# Python stdlib imports import datetime import re from openpyxl.jdcal import ( gcal2jd, jd2gcal, MJD_0 ) from openpyxl.compat import lru_cache # 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)) SECS_PER_DAY = 86400 EPOCH = datetime.datetime.utcfromtimestamp(0) W3CDTF_FORMAT = '%Y-%m-%dT%H:%M:%SZ' W3CDTF_REGEX = re.compile('(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(.(\d{2}))?Z?') def datetime_to_W3CDTF(dt): """Convert from a datetime to a timestamp string.""" return datetime.datetime.strftime(dt, W3CDTF_FORMAT) def W3CDTF_to_datetime(formatted_string): """Convert from a timestamp string to a datetime object."""