Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
openpyxl
openpyxl
Commits
92f444de400a
Commit
1e0e6ddc
authored
Feb 09, 2021
by
nmartensen
Browse files
drop dependency on jdcal
--HG-- branch : 3.0
parent
b45f0332b09f
Changes
2
Hide whitespace changes
Inline
Side-by-side
openpyxl/utils/datetime.py
View file @
92f444de
...
@@ -9,18 +9,12 @@ from datetime import timedelta, tzinfo
...
@@ -9,18 +9,12 @@ from datetime import timedelta, tzinfo
from
math
import
isnan
from
math
import
isnan
import
re
import
re
from
jdcal
import
(
gcal2jd
,
jd2gcal
,
MJD_0
)
# constants
# constants
MAC_EPOCH
=
datetime
.
date
(
1904
,
1
,
1
)
MAC_EPOCH
=
datetime
.
date
time
(
1904
,
1
,
1
)
WINDOWS_EPOCH
=
datetime
.
date
(
1899
,
12
,
30
)
WINDOWS_EPOCH
=
datetime
.
date
time
(
1899
,
12
,
30
)
CALENDAR_WINDOWS_1900
=
sum
(
gcal2jd
(
WINDOWS_EPOCH
.
year
,
WINDOWS_EPOCH
.
month
,
WINDOWS_EPOCH
.
day
))
CALENDAR_WINDOWS_1900
=
2415018.5
# Julian date of
WINDOWS_EPOCH
CALENDAR_MAC_1904
=
sum
(
gcal2jd
(
MAC_EPOCH
.
year
,
MAC_EPOCH
.
month
,
MAC_EPOCH
.
day
))
CALENDAR_MAC_1904
=
2416480.5
# Julian date of
MAC_EPOCH
SECS_PER_DAY
=
86400
SECS_PER_DAY
=
86400
EPOCH
=
datetime
.
datetime
.
utcfromtimestamp
(
0
)
EPOCH
=
datetime
.
datetime
.
utcfromtimestamp
(
0
)
...
@@ -70,6 +64,12 @@ def from_ISO8601(formatted_string):
...
@@ -70,6 +64,12 @@ def from_ISO8601(formatted_string):
return
dt
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
):
def
to_excel
(
dt
,
offset
=
CALENDAR_WINDOWS_1900
):
if
isinstance
(
dt
,
datetime
.
time
):
if
isinstance
(
dt
,
datetime
.
time
):
return
time_to_days
(
dt
)
return
time_to_days
(
dt
)
...
@@ -77,7 +77,7 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
...
@@ -77,7 +77,7 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
return
timedelta_to_days
(
dt
)
return
timedelta_to_days
(
dt
)
if
isnan
(
dt
.
year
):
# Pandas supports Not a Date
if
isnan
(
dt
.
year
):
# Pandas supports Not a Date
return
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
:
if
jul
<=
60
and
offset
==
CALENDAR_WINDOWS_1900
:
jul
-=
1
jul
-=
1
if
hasattr
(
dt
,
'time'
):
if
hasattr
(
dt
,
'time'
):
...
@@ -88,19 +88,13 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
...
@@ -88,19 +88,13 @@ def to_excel(dt, offset=CALENDAR_WINDOWS_1900):
def
from_excel
(
value
,
offset
=
CALENDAR_WINDOWS_1900
):
def
from_excel
(
value
,
offset
=
CALENDAR_WINDOWS_1900
):
if
value
is
None
:
if
value
is
None
:
return
return
_
,
fraction
=
divmod
(
value
,
1
)
day
,
fraction
=
divmod
(
value
,
1
)
diff
=
datetime
.
timedelta
(
milliseconds
=
round
(
fraction
*
SECS_PER_DAY
*
1000.
))
diff
=
datetime
.
timedelta
(
milliseconds
=
round
(
fraction
*
SECS_PER_DAY
*
1000.
))
if
0
<=
value
<
1
and
diff
.
days
==
0
:
if
0
<=
value
<
1
and
diff
.
days
==
0
:
return
days_to_time
(
diff
)
return
days_to_time
(
diff
)
if
1
<
value
<
60
and
offset
==
CALENDAR_WINDOWS_1900
:
if
1
<
value
<
60
and
offset
==
CALENDAR_WINDOWS_1900
:
value
+=
1
day
+=
1
parts
=
list
(
jd2gcal
(
MJD_0
,
value
+
offset
-
MJD_0
))
return
_get_epoch
(
offset
)
+
datetime
.
timedelta
(
days
=
day
)
+
diff
jumped
=
(
parts
[
-
1
]
==
0
and
fraction
>
0
)
if
not
jumped
:
return
datetime
.
datetime
(
*
parts
[:
3
])
+
diff
else
:
return
datetime
.
datetime
(
*
parts
[:
3
]
+
[
0
])
class
GMT
(
tzinfo
):
class
GMT
(
tzinfo
):
...
...
setup.py
View file @
92f444de
...
@@ -76,7 +76,7 @@ setup(
...
@@ -76,7 +76,7 @@ setup(
license
=
__license__
,
license
=
__license__
,
python_requires
=
">=3.6, "
,
python_requires
=
">=3.6, "
,
install_requires
=
[
install_requires
=
[
'jdcal'
,
'et_xmlfile'
,
'et_xmlfile'
,
],
],
project_urls
=
{
project_urls
=
{
'Documentation'
:
'https://openpyxl.readthedocs.io/en/stable/'
,
'Documentation'
:
'https://openpyxl.readthedocs.io/en/stable/'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment