Skip to content

Commit

Permalink
More robust parsing and support for ordinal dates and 8601 basic format
Browse files Browse the repository at this point in the history
See this issue thread for more details: #612
  • Loading branch information
jadchaar committed Sep 8, 2019
2 parents 877debf + 3160c4c commit 2344c11
Show file tree
Hide file tree
Showing 10 changed files with 862 additions and 339 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -24,7 +24,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v1.22.1
rev: v1.23.0
hooks:
- id: pyupgrade
- repo: https://github.com/pre-commit/pygrep-hooks
Expand All @@ -33,7 +33,7 @@ repos:
- id: python-no-eval
- id: python-check-blanket-noqa
- id: rst-backticks
- repo: https://github.com/python/black
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
Expand Down
2 changes: 1 addition & 1 deletion arrow/arrow.py
Expand Up @@ -1372,7 +1372,7 @@ def _get_iteration_params(cls, end, limit):
if end is None:

if limit is None:
raise Exception("one of 'end' or 'limit' is required")
raise ValueError("one of 'end' or 'limit' is required")

return cls.max, limit

Expand Down
38 changes: 7 additions & 31 deletions arrow/factory.py
Expand Up @@ -9,7 +9,6 @@
from __future__ import absolute_import

import calendar
import warnings
from datetime import date, datetime
from datetime import tzinfo as dt_tzinfo
from time import struct_time
Expand All @@ -21,18 +20,6 @@
from arrow.util import is_timestamp, isstr


class ArrowParseWarning(DeprecationWarning):
"""Raised when arrow.get() is passed a string with no formats and matches incorrectly
on one of the default formats.
e.g.
arrow.get('blabla2016') -> <Arrow [2016-01-01T00:00:00+00:00]>
arrow.get('13/4/2045') -> <Arrow [2045-01-01T00:00:00+00:00]>
In version 0.15.0 this warning will become a ParserError.
"""


class ArrowFactory(object):
""" A factory for generating :class:`Arrow <arrow.arrow.Arrow>` objects.
Expand Down Expand Up @@ -73,7 +60,7 @@ def get(self, *args, **kwargs):
>>> arrow.get(arw)
<Arrow [2013-10-23T15:21:54.354846+00:00]>
**One** ``str``, ``float``, or ``int``, convertible to a floating-point timestamp, to get
**One** ``float`` or ``int``, convertible to a floating-point timestamp, to get
that timestamp in UTC::
>>> arrow.get(1367992474.293378)
Expand All @@ -82,17 +69,16 @@ def get(self, *args, **kwargs):
>>> arrow.get(1367992474)
<Arrow [2013-05-08T05:54:34+00:00]>
>>> arrow.get('1367992474.293378')
<Arrow [2013-05-08T05:54:34.293378+00:00]>
>>> arrow.get('1367992474')
<Arrow [2013-05-08T05:54:34+00:00]>
**One** ISO-8601-formatted ``str``, to parse it::
>>> arrow.get('2013-09-29T01:26:43.830580')
<Arrow [2013-09-29T01:26:43.830580+00:00]>
**One** ISO-8601-formatted ``str``, in basic format, to parse it::
>>> arrow.get('20160413T133656.456289')
<Arrow [2016-04-13T13:36:56.456289+00:00]>
**One** ``tzinfo``, to get the current time **converted** to that timezone::
>>> arrow.get(tz.tzlocal())
Expand Down Expand Up @@ -177,7 +163,7 @@ def get(self, *args, **kwargs):
if arg is None:
return self.type.utcnow()

# try (int, float, str(int), str(float)) -> utc, from timestamp.
# try (int, float) -> utc, from timestamp.
if is_timestamp(arg):
return self.type.utcfromtimestamp(arg)

Expand All @@ -199,11 +185,6 @@ def get(self, *args, **kwargs):

# (str) -> parse.
elif isstr(arg):
warnings.warn(
"The .get() parsing method without a format string will parse more strictly in version 0.15.0."
"See https://github.com/crsmithdev/arrow/issues/612 for more details.",
ArrowParseWarning,
)
dt = parser.DateTimeParser(locale).parse_iso(arg)
return self.type.fromdatetime(dt, tz)

Expand Down Expand Up @@ -246,11 +227,6 @@ def get(self, *args, **kwargs):

# (str, format) -> parse.
elif isstr(arg_1) and (isstr(arg_2) or isinstance(arg_2, list)):
warnings.warn(
"The .get() parsing method with a format string will parse more strictly in version 0.15.0."
"See https://github.com/crsmithdev/arrow/issues/612 for more details.",
ArrowParseWarning,
)
dt = parser.DateTimeParser(locale).parse(args[0], args[1])
return self.type.fromdatetime(dt, tzinfo=tz)

Expand Down

0 comments on commit 2344c11

Please sign in to comment.