123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- # -*- coding: utf-8 -*-
- # Copyright (c) 2021, Brandon Nielsen
- # All rights reserved.
- #
- # This software may be modified and distributed under the terms
- # of the BSD license. See the LICENSE file for details.
- import unittest
- import aniso8601
- from aniso8601.builders import DatetimeTuple, DateTuple, TimeTuple, TimezoneTuple
- from aniso8601.exceptions import ISOFormatError
- from aniso8601.resolution import TimeResolution
- from aniso8601.tests.compat import mock
- from aniso8601.time import (
- _get_time_resolution,
- get_datetime_resolution,
- get_time_resolution,
- parse_datetime,
- parse_time,
- )
- class TestTimeResolutionFunctions(unittest.TestCase):
- def test_get_time_resolution(self):
- self.assertEqual(get_time_resolution("01:23:45"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("24:00:00"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("23:21:28,512400"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("23:21:28.512400"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("01:23"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("24:00"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("01:23,4567"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("01:23.4567"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("012345"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("240000"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("0123"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("2400"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("01"), TimeResolution.Hours)
- self.assertEqual(get_time_resolution("24"), TimeResolution.Hours)
- self.assertEqual(get_time_resolution("12,5"), TimeResolution.Hours)
- self.assertEqual(get_time_resolution("12.5"), TimeResolution.Hours)
- self.assertEqual(
- get_time_resolution("232128.512400+00:00"), TimeResolution.Seconds
- )
- self.assertEqual(get_time_resolution("0123.4567+00:00"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("01.4567+00:00"), TimeResolution.Hours)
- self.assertEqual(get_time_resolution("01:23:45+00:00"), TimeResolution.Seconds)
- self.assertEqual(get_time_resolution("24:00:00+00:00"), TimeResolution.Seconds)
- self.assertEqual(
- get_time_resolution("23:21:28.512400+00:00"), TimeResolution.Seconds
- )
- self.assertEqual(get_time_resolution("01:23+00:00"), TimeResolution.Minutes)
- self.assertEqual(get_time_resolution("24:00+00:00"), TimeResolution.Minutes)
- self.assertEqual(
- get_time_resolution("01:23.4567+00:00"), TimeResolution.Minutes
- )
- self.assertEqual(
- get_time_resolution("23:21:28.512400+11:15"), TimeResolution.Seconds
- )
- self.assertEqual(
- get_time_resolution("23:21:28.512400-12:34"), TimeResolution.Seconds
- )
- self.assertEqual(
- get_time_resolution("23:21:28.512400Z"), TimeResolution.Seconds
- )
- self.assertEqual(
- get_time_resolution("06:14:00.000123Z"), TimeResolution.Seconds
- )
- def test_get_datetime_resolution(self):
- self.assertEqual(
- get_datetime_resolution("2019-06-05T01:03:11.858714"),
- TimeResolution.Seconds,
- )
- self.assertEqual(
- get_datetime_resolution("2019-06-05T01:03:11"), TimeResolution.Seconds
- )
- self.assertEqual(
- get_datetime_resolution("2019-06-05T01:03"), TimeResolution.Minutes
- )
- self.assertEqual(get_datetime_resolution("2019-06-05T01"), TimeResolution.Hours)
- def test_get_time_resolution_badtype(self):
- testtuples = (None, 1, False, 1.234)
- for testtuple in testtuples:
- with self.assertRaises(ValueError):
- get_time_resolution(testtuple)
- def test_get_time_resolution_badstr(self):
- testtuples = ("A6:14:00.000123Z", "06:14:0B", "bad", "")
- for testtuple in testtuples:
- with self.assertRaises(ISOFormatError):
- get_time_resolution(testtuple)
- def test_get_time_resolution_internal(self):
- self.assertEqual(
- _get_time_resolution(TimeTuple(hh="01", mm="02", ss="03", tz=None)),
- TimeResolution.Seconds,
- )
- self.assertEqual(
- _get_time_resolution(TimeTuple(hh="01", mm="02", ss=None, tz=None)),
- TimeResolution.Minutes,
- )
- self.assertEqual(
- _get_time_resolution(TimeTuple(hh="01", mm=None, ss=None, tz=None)),
- TimeResolution.Hours,
- )
- class TestTimeParserFunctions(unittest.TestCase):
- def test_parse_time(self):
- testtuples = (
- ("01:23:45", {"hh": "01", "mm": "23", "ss": "45", "tz": None}),
- ("24:00:00", {"hh": "24", "mm": "00", "ss": "00", "tz": None}),
- (
- "23:21:28,512400",
- {"hh": "23", "mm": "21", "ss": "28.512400", "tz": None},
- ),
- (
- "23:21:28.512400",
- {"hh": "23", "mm": "21", "ss": "28.512400", "tz": None},
- ),
- (
- "01:03:11.858714",
- {"hh": "01", "mm": "03", "ss": "11.858714", "tz": None},
- ),
- (
- "14:43:59.9999997",
- {"hh": "14", "mm": "43", "ss": "59.9999997", "tz": None},
- ),
- ("01:23", {"hh": "01", "mm": "23", "ss": None, "tz": None}),
- ("24:00", {"hh": "24", "mm": "00", "ss": None, "tz": None}),
- ("01:23,4567", {"hh": "01", "mm": "23.4567", "ss": None, "tz": None}),
- ("01:23.4567", {"hh": "01", "mm": "23.4567", "ss": None, "tz": None}),
- ("012345", {"hh": "01", "mm": "23", "ss": "45", "tz": None}),
- ("240000", {"hh": "24", "mm": "00", "ss": "00", "tz": None}),
- ("232128,512400", {"hh": "23", "mm": "21", "ss": "28.512400", "tz": None}),
- ("232128.512400", {"hh": "23", "mm": "21", "ss": "28.512400", "tz": None}),
- ("010311.858714", {"hh": "01", "mm": "03", "ss": "11.858714", "tz": None}),
- (
- "144359.9999997",
- {"hh": "14", "mm": "43", "ss": "59.9999997", "tz": None},
- ),
- ("0123", {"hh": "01", "mm": "23", "ss": None, "tz": None}),
- ("2400", {"hh": "24", "mm": "00", "ss": None, "tz": None}),
- ("01", {"hh": "01", "mm": None, "ss": None, "tz": None}),
- ("24", {"hh": "24", "mm": None, "ss": None, "tz": None}),
- ("12,5", {"hh": "12.5", "mm": None, "ss": None, "tz": None}),
- ("12.5", {"hh": "12.5", "mm": None, "ss": None, "tz": None}),
- (
- "232128,512400+00:00",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "232128.512400+00:00",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "0123,4567+00:00",
- {
- "hh": "01",
- "mm": "23.4567",
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "0123.4567+00:00",
- {
- "hh": "01",
- "mm": "23.4567",
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "01,4567+00:00",
- {
- "hh": "01.4567",
- "mm": None,
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "01.4567+00:00",
- {
- "hh": "01.4567",
- "mm": None,
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "01:23:45+00:00",
- {
- "hh": "01",
- "mm": "23",
- "ss": "45",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "24:00:00+00:00",
- {
- "hh": "24",
- "mm": "00",
- "ss": "00",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "23:21:28.512400+00:00",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "01:23+00:00",
- {
- "hh": "01",
- "mm": "23",
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "24:00+00:00",
- {
- "hh": "24",
- "mm": "00",
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "01:23.4567+00:00",
- {
- "hh": "01",
- "mm": "23.4567",
- "ss": None,
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- },
- ),
- (
- "23:21:28.512400+11:15",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "11", "15", "+11:15"),
- },
- ),
- (
- "23:21:28.512400-12:34",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(True, None, "12", "34", "-12:34"),
- },
- ),
- (
- "23:21:28.512400Z",
- {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, True, None, None, "Z"),
- },
- ),
- (
- "06:14:00.000123Z",
- {
- "hh": "06",
- "mm": "14",
- "ss": "00.000123",
- "tz": TimezoneTuple(False, True, None, None, "Z"),
- },
- ),
- )
- for testtuple in testtuples:
- with mock.patch.object(
- aniso8601.time.PythonTimeBuilder, "build_time"
- ) as mockBuildTime:
- mockBuildTime.return_value = testtuple[1]
- result = parse_time(testtuple[0])
- self.assertEqual(result, testtuple[1])
- mockBuildTime.assert_called_once_with(**testtuple[1])
- def test_parse_time_badtype(self):
- testtuples = (None, 1, False, 1.234)
- for testtuple in testtuples:
- with self.assertRaises(ValueError):
- parse_time(testtuple, builder=None)
- def test_parse_time_badstr(self):
- testtuples = (
- "A6:14:00.000123Z",
- "06:14:0B",
- "06:1 :02",
- "0000,70:24,9",
- "00.27:5332",
- "bad",
- "",
- )
- for testtuple in testtuples:
- with self.assertRaises(ISOFormatError):
- parse_time(testtuple, builder=None)
- def test_parse_time_mockbuilder(self):
- mockBuilder = mock.Mock()
- expectedargs = {"hh": "01", "mm": "23", "ss": "45", "tz": None}
- mockBuilder.build_time.return_value = expectedargs
- result = parse_time("01:23:45", builder=mockBuilder)
- self.assertEqual(result, expectedargs)
- mockBuilder.build_time.assert_called_once_with(**expectedargs)
- mockBuilder = mock.Mock()
- expectedargs = {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "00", "00", "+00:00"),
- }
- mockBuilder.build_time.return_value = expectedargs
- result = parse_time("232128.512400+00:00", builder=mockBuilder)
- self.assertEqual(result, expectedargs)
- mockBuilder.build_time.assert_called_once_with(**expectedargs)
- mockBuilder = mock.Mock()
- expectedargs = {
- "hh": "23",
- "mm": "21",
- "ss": "28.512400",
- "tz": TimezoneTuple(False, None, "11", "15", "+11:15"),
- }
- mockBuilder.build_time.return_value = expectedargs
- result = parse_time("23:21:28.512400+11:15", builder=mockBuilder)
- self.assertEqual(result, expectedargs)
- mockBuilder.build_time.assert_called_once_with(**expectedargs)
- def test_parse_datetime(self):
- testtuples = (
- (
- "2019-06-05T01:03:11,858714",
- (
- DateTuple("2019", "06", "05", None, None, None),
- TimeTuple("01", "03", "11.858714", None),
- ),
- ),
- (
- "2019-06-05T01:03:11.858714",
- (
- DateTuple("2019", "06", "05", None, None, None),
- TimeTuple("01", "03", "11.858714", None),
- ),
- ),
- (
- "1981-04-05T23:21:28.512400Z",
- (
- DateTuple("1981", "04", "05", None, None, None),
- TimeTuple(
- "23",
- "21",
- "28.512400",
- TimezoneTuple(False, True, None, None, "Z"),
- ),
- ),
- ),
- (
- "1981095T23:21:28.512400-12:34",
- (
- DateTuple("1981", None, None, None, None, "095"),
- TimeTuple(
- "23",
- "21",
- "28.512400",
- TimezoneTuple(True, None, "12", "34", "-12:34"),
- ),
- ),
- ),
- (
- "19810405T23:21:28+00",
- (
- DateTuple("1981", "04", "05", None, None, None),
- TimeTuple(
- "23", "21", "28", TimezoneTuple(False, None, "00", None, "+00")
- ),
- ),
- ),
- (
- "19810405T23:21:28+00:00",
- (
- DateTuple("1981", "04", "05", None, None, None),
- TimeTuple(
- "23",
- "21",
- "28",
- TimezoneTuple(False, None, "00", "00", "+00:00"),
- ),
- ),
- ),
- )
- for testtuple in testtuples:
- with mock.patch.object(
- aniso8601.time.PythonTimeBuilder, "build_datetime"
- ) as mockBuildDateTime:
- mockBuildDateTime.return_value = testtuple[1]
- result = parse_datetime(testtuple[0])
- self.assertEqual(result, testtuple[1])
- mockBuildDateTime.assert_called_once_with(*testtuple[1])
- def test_parse_datetime_spacedelimited(self):
- expectedargs = (
- DateTuple("2004", None, None, "53", "6", None),
- TimeTuple(
- "23", "21", "28.512400", TimezoneTuple(True, None, "12", "34", "-12:34")
- ),
- )
- with mock.patch.object(
- aniso8601.time.PythonTimeBuilder, "build_datetime"
- ) as mockBuildDateTime:
- mockBuildDateTime.return_value = expectedargs
- result = parse_datetime("2004-W53-6 23:21:28.512400-12:34", delimiter=" ")
- self.assertEqual(result, expectedargs)
- mockBuildDateTime.assert_called_once_with(*expectedargs)
- def test_parse_datetime_commadelimited(self):
- expectedargs = (
- DateTuple("1981", "04", "05", None, None, None),
- TimeTuple(
- "23", "21", "28.512400", TimezoneTuple(False, True, None, None, "Z")
- ),
- )
- with mock.patch.object(
- aniso8601.time.PythonTimeBuilder, "build_datetime"
- ) as mockBuildDateTime:
- mockBuildDateTime.return_value = expectedargs
- result = parse_datetime("1981-04-05,23:21:28,512400Z", delimiter=",")
- self.assertEqual(result, expectedargs)
- mockBuildDateTime.assert_called_once_with(*expectedargs)
- def test_parse_datetime_baddelimiter(self):
- testtuples = (
- "1981-04-05,23:21:28,512400Z",
- "2004-W53-6 23:21:28.512400-12:3",
- "1981040523:21:28",
- )
- for testtuple in testtuples:
- with self.assertRaises(ISOFormatError):
- parse_datetime(testtuple, builder=None)
- def test_parse_datetime_badtype(self):
- testtuples = (None, 1, False, 1.234)
- for testtuple in testtuples:
- with self.assertRaises(ValueError):
- parse_datetime(testtuple, builder=None)
- def test_parse_datetime_badstr(self):
- testtuples = (
- "1981-04-05TA6:14:00.000123Z",
- "2004-W53-6T06:14:0B",
- "2014-01-230T23:21:28+00",
- "201401230T01:03:11.858714",
- "9999 W53T49",
- "9T0000,70:24,9",
- "bad",
- "",
- )
- for testtuple in testtuples:
- with self.assertRaises(ISOFormatError):
- parse_datetime(testtuple, builder=None)
- def test_parse_datetime_mockbuilder(self):
- mockBuilder = mock.Mock()
- expectedargs = (
- DateTuple("1981", None, None, None, None, "095"),
- TimeTuple(
- "23", "21", "28.512400", TimezoneTuple(True, None, "12", "34", "-12:34")
- ),
- )
- mockBuilder.build_datetime.return_value = expectedargs
- result = parse_datetime("1981095T23:21:28.512400-12:34", builder=mockBuilder)
- self.assertEqual(result, expectedargs)
- mockBuilder.build_datetime.assert_called_once_with(*expectedargs)
|