Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tz(self):
tz_z_re = parser.DateTimeParser._TZ_Z_RE
self.assertEqual(tz_z_re.findall("-0700"), [("-", "07", "00")])
self.assertEqual(tz_z_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120") is None)
tz_zz_re = parser.DateTimeParser._TZ_ZZ_RE
self.assertEqual(tz_zz_re.findall("-07:00"), [("-", "07", "00")])
self.assertEqual(tz_zz_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120") is None)
tz_name_re = parser.DateTimeParser._TZ_NAME_RE
self.assertEqual(tz_name_re.findall("Europe/Warsaw"), ["Europe/Warsaw"])
self.assertEqual(tz_name_re.findall("GMT"), ["GMT"])
def test_timestamp(self):
timestamp_re = parser.DateTimeParser._TIMESTAMP_RE
self.assertEqual(
timestamp_re.findall("1565707550.452729"), ["1565707550.452729"]
)
self.assertEqual(
timestamp_re.findall("-1565707550.452729"), ["-1565707550.452729"]
)
self.assertEqual(timestamp_re.findall("-1565707550"), ["-1565707550"])
self.assertEqual(timestamp_re.findall("1565707550"), ["1565707550"])
self.assertEqual(timestamp_re.findall("1565707550."), [])
self.assertEqual(timestamp_re.findall(".1565707550"), [])
def test_tz(self):
tz_z_re = parser.DateTimeParser._TZ_Z_RE
self.assertEqual(tz_z_re.findall("-0700"), [("-", "07", "00")])
self.assertEqual(tz_z_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120") is None)
tz_zz_re = parser.DateTimeParser._TZ_ZZ_RE
self.assertEqual(tz_zz_re.findall("-07:00"), [("-", "07", "00")])
self.assertEqual(tz_zz_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120") is None)
tz_name_re = parser.DateTimeParser._TZ_NAME_RE
self.assertEqual(tz_name_re.findall("Europe/Warsaw"), ["Europe/Warsaw"])
self.assertEqual(tz_name_re.findall("GMT"), ["GMT"])
def test_spanish(self):
parser_ = parser.DateTimeParser("es_es")
self.assertEqual(
parser_.parse("Enero 1º, 2013", "MMMM Do, YYYY"), datetime(2013, 1, 1)
)
def test_month_abbreviations(self):
p = parser.DateTimeParser("en_us")
text = "_".join(calendar.month_abbr[1:])
result = p._input_re_map["MMM"].findall(text)
self.assertEqual(result, calendar.month_abbr[1:])
def test_tz(self):
tz_z_re = parser.DateTimeParser._TZ_Z_RE
self.assertEqual(tz_z_re.findall("-0700"), [("-", "07", "00")])
self.assertEqual(tz_z_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_z_re.search("15/01/2019T04:05:06.789120") is None)
tz_zz_re = parser.DateTimeParser._TZ_ZZ_RE
self.assertEqual(tz_zz_re.findall("-07:00"), [("-", "07", "00")])
self.assertEqual(tz_zz_re.findall("+07"), [("+", "07", "")])
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120Z") is not None)
self.assertTrue(tz_zz_re.search("15/01/2019T04:05:06.789120") is None)
tz_name_re = parser.DateTimeParser._TZ_NAME_RE
self.assertEqual(tz_name_re.findall("Europe/Warsaw"), ["Europe/Warsaw"])
self.assertEqual(tz_name_re.findall("GMT"), ["GMT"])
def test_escape(self):
escape_regex = parser.DateTimeParser._ESCAPE_RE
self.assertEqual(
escape_regex.findall("2018-03-09 8 [h] 40 [hello]"), ["[h]", "[hello]"]
)
def test_time(self):
time_re = parser.DateTimeParser._TIME_RE
time_seperators = [":", ""]
for sep in time_seperators:
self.assertEqual(time_re.findall("12"), [("12", "", "", "", "")])
self.assertEqual(
time_re.findall("12{sep}35".format(sep=sep)), [("12", "35", "", "", "")]
)
self.assertEqual(
time_re.findall("12{sep}35{sep}46".format(sep=sep)),
[("12", "35", "46", "", "")],
)
self.assertEqual(
time_re.findall("12{sep}35{sep}46.952313".format(sep=sep)),
[("12", "35", "46", ".", "952313")],
)
self.assertEqual(
# (datetime) -> from datetime.
if isinstance(arg, datetime):
return self.type.fromdatetime(arg)
# (date) -> from date.
if isinstance(arg, date):
return self.type.fromdate(arg)
# (tzinfo) -> now, @ tzinfo.
elif isinstance(arg, tzinfo):
return self.type.now(arg)
# (str) -> parse.
elif isstr(arg):
dt = parser.DateTimeParser(locale).parse_iso(arg)
return self.type.fromdatetime(dt)
# (struct_time) -> from struct_time
elif isinstance(arg, struct_time):
return self.type.utcfromtimestamp(calendar.timegm(arg))
else:
raise TypeError('Can\'t parse single argument type of \'{0}\''.format(type(arg)))
elif arg_count == 2:
arg_1, arg_2 = args[0], args[1]
if isinstance(arg_1, datetime):
# (datetime, tzinfo/str) -> fromdatetime replace tzinfo.
def iso8601_to_dt(iso8601):
"""Given an ISO8601 string as returned by Device Cloud, convert to a datetime object"""
# We could just use arrow.get() but that is more permissive than we actually want.
# Internal (but still public) to arrow is the actual parser where we can be
# a bit more specific
parser = DateTimeParser()
try:
arrow_dt = arrow.Arrow.fromdatetime(parser.parse_iso(iso8601))
return arrow_dt.to('utc').datetime
except ParserError as pe:
raise ValueError("Provided was not a valid ISO8601 string: %r" % pe)