-
-
Notifications
You must be signed in to change notification settings - Fork 681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creation from Excel Serial Dates #979
Open
anishnya
wants to merge
1
commit into
arrow-py:master
Choose a base branch
from
anishnya:excel_date
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
from dateutil import tz as dateutil_tz | ||
from dateutil.relativedelta import relativedelta | ||
|
||
import arrow | ||
from arrow import formatter, locales, parser, util | ||
from arrow.constants import DEFAULT_LOCALE, DEHUMANIZE_LOCALES | ||
from arrow.locales import TimeFrameLiteral | ||
|
@@ -1787,6 +1788,42 @@ def _get_tzinfo(tz_expr: Optional[TZ_EXPR]) -> dt_tzinfo: | |
except parser.ParserError: | ||
raise ValueError(f"{tz_expr!r} not recognized as a timezone.") | ||
|
||
@classmethod | ||
def excel_date( | ||
cls, delta: Union[int, float], default_windows_date: bool = True | ||
) -> "Arrow": | ||
"""Returns a new :class:`Arrow <arrow.arrow.Arrow>` object, that represents | ||
the date of an Excel Serial formatted date. | ||
|
||
:param delta: a ``int`` or ``float`` representing an Excel Serial Date. | ||
:param default_windows_date: (optional) a ``bool`` specifying whether a user | ||
wants to use the Windows or macOS date system. Defaults to 'True' for Windows | ||
date system. | ||
|
||
Usage:: | ||
|
||
>>> arw = Arrow.excel_date(34519) | ||
>>> arw | ||
<Arrow [1994-07-04T00:00:00+00:00]> | ||
>>> arw = Arrow.excel_date(34519, default_windows_date = False) | ||
>>> arw | ||
<Arrow [1998-07-05T00:00:00+00:00]> | ||
|
||
""" | ||
|
||
if default_windows_date: | ||
# Need to have this clause as Excel incorrectly considers 1900 a leap year | ||
if delta < 60: | ||
start_date = arrow.get("1899-12-31") | ||
else: | ||
start_date = arrow.get("1899-12-30") | ||
else: | ||
start_date = arrow.get("1904-01-01") | ||
Comment on lines
+1816
to
+1821
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using |
||
|
||
shifted_time = start_date.shift(days=delta) | ||
|
||
return shifted_time | ||
|
||
@classmethod | ||
def _get_datetime( | ||
cls, expr: Union["Arrow", dt_datetime, int, float, str] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change the kwarg to instead use the Microsoft terminology of 1900 and 1904 datesystem: https://docs.microsoft.com/en-us/office/troubleshoot/excel/1900-and-1904-date-system? Like make the kwarg
1904_date_system=True
? Or do you think using the OS names is clearer?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense to use the 1900/1904 convention. I looked in Excel and that's what they end up of using.