How to use the arrow.parser.ParserError function in arrow

To help you get started, we’ve selected a few arrow examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crsmithdev / arrow / tests / parser_tests.py View on Github external
self.assertEqual(
            self.parser.parse_iso("20180517T105513-0700"),
            datetime(2018, 5, 17, 10, 55, 13, tzinfo=tz.tzoffset(None, -25200)),
        )

        self.assertEqual(
            self.parser.parse_iso("20180517T105513-07"),
            datetime(2018, 5, 17, 10, 55, 13, tzinfo=tz.tzoffset(None, -25200)),
        )

        # ordinal in basic format: YYYYDDDD
        self.assertEqual(self.parser.parse_iso("1998136"), datetime(1998, 5, 16))

        # timezone requires +- seperator
        with self.assertRaises(ParserError):
            self.parser.parse_iso("20180517T1055130700")

        with self.assertRaises(ParserError):
            self.parser.parse_iso("20180517T10551307")

        # too many digits in date
        with self.assertRaises(ParserError):
            self.parser.parse_iso("201860517T105513Z")

        # too many digits in time
        with self.assertRaises(ParserError):
            self.parser.parse_iso("20180517T1055213Z")
github crsmithdev / arrow / tests / factory_tests.py View on Github external
def test_one_arg_timestamp(self):

        int_timestamp = int(time.time())
        timestamp_dt = datetime.utcfromtimestamp(int_timestamp).replace(
            tzinfo=tz.tzutc()
        )

        self.assertEqual(self.factory.get(int_timestamp), timestamp_dt)

        with self.assertRaises(ParserError):
            self.factory.get(str(int_timestamp))

        float_timestamp = time.time()
        timestamp_dt = datetime.utcfromtimestamp(float_timestamp).replace(
            tzinfo=tz.tzutc()
        )

        self.assertEqual(self.factory.get(float_timestamp), timestamp_dt)

        with self.assertRaises(ParserError):
            self.factory.get(str(float_timestamp))

        # Regression test for issue #216
        # Python 3 raises OverflowError, Python 2 raises ValueError
        timestamp = 99999999999999999999999999.99999999999999999999999999
        with self.assertRaises((OverflowError, ValueError)):
github broadinstitute / cms / illumina.py View on Github external
# possible formats found in RunInfo.xml:
        #   "170712" (YYMMDD)
        #   "20170712" (YYYYMMDD)
        #   "6/27/2018 4:59:20 PM" (M/D/YYYY h:mm:ss A)
        datestring_formats = [
            "YYMMDD",
            "YYYYMMDD",
            "M/D/YYYY h:mm:ss A"
        ]
        for datestring_format in datestring_formats:
            try:
                date_parsed = arrow.get(rundate, datestring_format)
                return date_parsed
            except arrow.parser.ParserError:
                pass
        raise arrow.parser.ParserError("The date string seen in RunInfo.xml ('%s') did not match known Illumina formats: %s" % (rundate,datestring_formats) )
github crsmithdev / arrow / arrow / parser.py View on Github external
month = parts.get("month")
            if year is None:
                raise ParserError(
                    "Year component is required with the DDD and DDDD tokens."
                )

            if month is not None:
                raise ParserError(
                    "Month component is not allowed with the DDD and DDDD tokens."
                )

            date_string = "{}-{}".format(year, day_of_year)
            try:
                dt = datetime.strptime(date_string, "%Y-%j")
            except ValueError:
                raise ParserError(
                    "The provided day of year '{}' is invalid.".format(day_of_year)
                )

            parts["year"] = dt.year
            parts["month"] = dt.month
            parts["day"] = dt.day

        am_pm = parts.get("am_pm")
        hour = parts.get("hour", 0)

        if am_pm == "pm" and hour < 12:
            hour += 12
        elif am_pm == "am" and hour == 12:
            hour = 0

        # Support for midnight at the end of day
github csirtgadgets / csirtg-indicator-py-v1 / csirtg_indicator / utils / ztime.py View on Github external
raise RuntimeError('Invalid Timestamp: %s' % ts)

        try:
            t = arrow.get(ts, ['YYYY-MM-DD HH:mm:ss ZZZ', 'ddd, DD MMM YYYY HH:mm:ss Z', 'x'])
            if t.year < 1980:
                if type(ts) == datetime:
                    ts = str(ts)
                if ts_len == 8:
                    ts = '{}T00:00:00Z'.format(ts)
                    t = arrow.get(ts, 'YYYYMMDDTHH:mm:ssZ')

                if t.year < 1970:
                    raise RuntimeError('invalid timestamp: %s' % ts)
            return t

        except arrow.parser.ParserError as e:
            t = pendulum.parse(ts, strict=False)
            t = arrow.get(t)
            return t

    else:
        raise RuntimeError('Invalid Timestamp: %s' % ts)
github theotherp / nzbhydra / libs / arrow / arrow.py View on Github external
def _get_tzinfo(tz_expr):

        if tz_expr is None:
            return dateutil_tz.tzutc()
        if isinstance(tz_expr, tzinfo):
            return tz_expr
        else:
            try:
                return parser.TzinfoParser.parse(tz_expr)
            except parser.ParserError:
                raise ValueError('\'{0}\' not recognized as a timezone'.format(
                    tz_expr))
github Tautulli / Tautulli / lib / arrow / arrow.py View on Github external
def _get_tzinfo(tz_expr):

        if tz_expr is None:
            return dateutil_tz.tzutc()
        if isinstance(tz_expr, tzinfo):
            return tz_expr
        else:
            try:
                return parser.TzinfoParser.parse(tz_expr)
            except parser.ParserError:
                raise ValueError('\'{0}\' not recognized as a timezone'.format(
                    tz_expr))