2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2015-01-28 12:35:27 +00:00
|
|
|
from datetime import date, datetime, time, timedelta
|
2012-08-19 19:47:41 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.utils.dateparse import (
|
|
|
|
parse_date, parse_datetime, parse_duration, parse_time,
|
|
|
|
)
|
2013-09-08 08:43:33 +00:00
|
|
|
from django.utils.timezone import get_fixed_timezone
|
2012-08-19 19:47:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DateParseTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_parse_date(self):
|
|
|
|
# Valid inputs
|
|
|
|
self.assertEqual(parse_date('2012-04-23'), date(2012, 4, 23))
|
|
|
|
self.assertEqual(parse_date('2012-4-9'), date(2012, 4, 9))
|
|
|
|
# Invalid inputs
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(parse_date('20120423'))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
parse_date('2012-04-56')
|
2012-08-19 19:47:41 +00:00
|
|
|
|
|
|
|
def test_parse_time(self):
|
|
|
|
# Valid inputs
|
|
|
|
self.assertEqual(parse_time('09:15:00'), time(9, 15))
|
|
|
|
self.assertEqual(parse_time('10:10'), time(10, 10))
|
|
|
|
self.assertEqual(parse_time('10:20:30.400'), time(10, 20, 30, 400000))
|
2019-09-25 20:17:22 +00:00
|
|
|
self.assertEqual(parse_time('10:20:30,400'), time(10, 20, 30, 400000))
|
2012-08-19 19:47:41 +00:00
|
|
|
self.assertEqual(parse_time('4:8:16'), time(4, 8, 16))
|
2021-07-06 06:32:44 +00:00
|
|
|
# Time zone offset is ignored.
|
|
|
|
self.assertEqual(parse_time('00:05:23+04:00'), time(0, 5, 23))
|
|
|
|
# These should be invalid, see #32904.
|
|
|
|
self.assertEqual(parse_time('00:05:'), time(0, 5))
|
|
|
|
self.assertEqual(parse_time('4:18:101'), time(4, 18, 10))
|
2012-08-19 19:47:41 +00:00
|
|
|
# Invalid inputs
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(parse_time('091500'))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
parse_time('09:15:90')
|
2012-08-19 19:47:41 +00:00
|
|
|
|
|
|
|
def test_parse_datetime(self):
|
2017-07-03 21:08:58 +00:00
|
|
|
valid_inputs = (
|
|
|
|
('2012-04-23T09:15:00', datetime(2012, 4, 23, 9, 15)),
|
|
|
|
('2012-4-9 4:8:16', datetime(2012, 4, 9, 4, 8, 16)),
|
|
|
|
('2012-04-23T09:15:00Z', datetime(2012, 4, 23, 9, 15, 0, 0, get_fixed_timezone(0))),
|
|
|
|
('2012-4-9 4:8:16-0320', datetime(2012, 4, 9, 4, 8, 16, 0, get_fixed_timezone(-200))),
|
|
|
|
('2012-04-23T10:20:30.400+02:30', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(150))),
|
|
|
|
('2012-04-23T10:20:30.400+02', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(120))),
|
|
|
|
('2012-04-23T10:20:30.400-02', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(-120))),
|
2019-09-25 20:17:22 +00:00
|
|
|
('2012-04-23T10:20:30,400-02', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(-120))),
|
2021-05-08 00:59:17 +00:00
|
|
|
('2012-04-23T10:20:30.400 +0230', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(150))),
|
|
|
|
('2012-04-23T10:20:30,400 +00', datetime(2012, 4, 23, 10, 20, 30, 400000, get_fixed_timezone(0))),
|
|
|
|
('2012-04-23T10:20:30 -02', datetime(2012, 4, 23, 10, 20, 30, 0, get_fixed_timezone(-120))),
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2017-07-03 21:08:58 +00:00
|
|
|
for source, expected in valid_inputs:
|
|
|
|
with self.subTest(source=source):
|
|
|
|
self.assertEqual(parse_datetime(source), expected)
|
|
|
|
|
2012-08-19 19:47:41 +00:00
|
|
|
# Invalid inputs
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(parse_datetime('20120423091500'))
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
parse_datetime('2012-04-56T09:15:90')
|
2014-07-24 12:57:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DurationParseTests(unittest.TestCase):
|
2015-06-02 09:08:41 +00:00
|
|
|
|
|
|
|
def test_parse_python_format(self):
|
|
|
|
timedeltas = [
|
|
|
|
timedelta(days=4, minutes=15, seconds=30, milliseconds=100), # fractions of seconds
|
|
|
|
timedelta(hours=10, minutes=15, seconds=30), # hours, minutes, seconds
|
|
|
|
timedelta(days=4, minutes=15, seconds=30), # multiple days
|
|
|
|
timedelta(days=1, minutes=00, seconds=00), # single day
|
|
|
|
timedelta(days=-4, minutes=15, seconds=30), # negative durations
|
|
|
|
timedelta(minutes=15, seconds=30), # minute & seconds
|
|
|
|
timedelta(seconds=30), # seconds
|
|
|
|
]
|
|
|
|
for delta in timedeltas:
|
2017-07-03 21:08:58 +00:00
|
|
|
with self.subTest(delta=delta):
|
|
|
|
self.assertEqual(parse_duration(format(delta)), delta)
|
2015-06-02 09:08:41 +00:00
|
|
|
|
2017-04-13 02:03:35 +00:00
|
|
|
def test_parse_postgresql_format(self):
|
|
|
|
test_values = (
|
|
|
|
('1 day', timedelta(1)),
|
2020-12-21 09:11:55 +00:00
|
|
|
('-1 day', timedelta(-1)),
|
2017-04-13 02:03:35 +00:00
|
|
|
('1 day 0:00:01', timedelta(days=1, seconds=1)),
|
|
|
|
('1 day -0:00:01', timedelta(days=1, seconds=-1)),
|
|
|
|
('-1 day -0:00:01', timedelta(days=-1, seconds=-1)),
|
|
|
|
('-1 day +0:00:01', timedelta(days=-1, seconds=1)),
|
|
|
|
('4 days 0:15:30.1', timedelta(days=4, minutes=15, seconds=30, milliseconds=100)),
|
|
|
|
('4 days 0:15:30.0001', timedelta(days=4, minutes=15, seconds=30, microseconds=100)),
|
|
|
|
('-4 days -15:00:30', timedelta(days=-4, hours=-15, seconds=-30)),
|
|
|
|
)
|
|
|
|
for source, expected in test_values:
|
|
|
|
with self.subTest(source=source):
|
|
|
|
self.assertEqual(parse_duration(source), expected)
|
|
|
|
|
2014-07-24 12:57:24 +00:00
|
|
|
def test_seconds(self):
|
|
|
|
self.assertEqual(parse_duration('30'), timedelta(seconds=30))
|
|
|
|
|
|
|
|
def test_minutes_seconds(self):
|
|
|
|
self.assertEqual(parse_duration('15:30'), timedelta(minutes=15, seconds=30))
|
|
|
|
self.assertEqual(parse_duration('5:30'), timedelta(minutes=5, seconds=30))
|
|
|
|
|
|
|
|
def test_hours_minutes_seconds(self):
|
|
|
|
self.assertEqual(parse_duration('10:15:30'), timedelta(hours=10, minutes=15, seconds=30))
|
|
|
|
self.assertEqual(parse_duration('1:15:30'), timedelta(hours=1, minutes=15, seconds=30))
|
|
|
|
self.assertEqual(parse_duration('100:200:300'), timedelta(hours=100, minutes=200, seconds=300))
|
|
|
|
|
|
|
|
def test_days(self):
|
|
|
|
self.assertEqual(parse_duration('4 15:30'), timedelta(days=4, minutes=15, seconds=30))
|
|
|
|
self.assertEqual(parse_duration('4 10:15:30'), timedelta(days=4, hours=10, minutes=15, seconds=30))
|
|
|
|
|
|
|
|
def test_fractions_of_seconds(self):
|
2017-07-03 21:08:58 +00:00
|
|
|
test_values = (
|
|
|
|
('15:30.1', timedelta(minutes=15, seconds=30, milliseconds=100)),
|
|
|
|
('15:30.01', timedelta(minutes=15, seconds=30, milliseconds=10)),
|
|
|
|
('15:30.001', timedelta(minutes=15, seconds=30, milliseconds=1)),
|
|
|
|
('15:30.0001', timedelta(minutes=15, seconds=30, microseconds=100)),
|
|
|
|
('15:30.00001', timedelta(minutes=15, seconds=30, microseconds=10)),
|
|
|
|
('15:30.000001', timedelta(minutes=15, seconds=30, microseconds=1)),
|
2019-09-25 20:17:22 +00:00
|
|
|
('15:30,000001', timedelta(minutes=15, seconds=30, microseconds=1)),
|
2017-07-03 21:08:58 +00:00
|
|
|
)
|
|
|
|
for source, expected in test_values:
|
|
|
|
with self.subTest(source=source):
|
|
|
|
self.assertEqual(parse_duration(source), expected)
|
2014-07-24 12:57:24 +00:00
|
|
|
|
|
|
|
def test_negative(self):
|
2017-07-03 21:08:58 +00:00
|
|
|
test_values = (
|
|
|
|
('-4 15:30', timedelta(days=-4, minutes=15, seconds=30)),
|
|
|
|
('-172800', timedelta(days=-2)),
|
2019-02-16 07:43:42 +00:00
|
|
|
('-15:30', timedelta(minutes=-15, seconds=-30)),
|
|
|
|
('-1:15:30', timedelta(hours=-1, minutes=-15, seconds=-30)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('-30.1', timedelta(seconds=-30, milliseconds=-100)),
|
2019-09-25 20:17:22 +00:00
|
|
|
('-30,1', timedelta(seconds=-30, milliseconds=-100)),
|
2019-02-16 07:43:42 +00:00
|
|
|
('-00:01:01', timedelta(minutes=-1, seconds=-1)),
|
|
|
|
('-01:01', timedelta(seconds=-61)),
|
|
|
|
('-01:-01', None),
|
2017-07-03 21:08:58 +00:00
|
|
|
)
|
|
|
|
for source, expected in test_values:
|
|
|
|
with self.subTest(source=source):
|
|
|
|
self.assertEqual(parse_duration(source), expected)
|
2014-07-24 12:57:24 +00:00
|
|
|
|
|
|
|
def test_iso_8601(self):
|
2017-07-03 21:08:58 +00:00
|
|
|
test_values = (
|
|
|
|
('P4Y', None),
|
|
|
|
('P4M', None),
|
|
|
|
('P4W', None),
|
|
|
|
('P4D', timedelta(days=4)),
|
2020-12-19 09:53:35 +00:00
|
|
|
('-P1D', timedelta(days=-1)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('P0.5D', timedelta(hours=12)),
|
2019-11-27 07:14:00 +00:00
|
|
|
('P0,5D', timedelta(hours=12)),
|
2020-12-19 09:53:35 +00:00
|
|
|
('-P0.5D', timedelta(hours=-12)),
|
|
|
|
('-P0,5D', timedelta(hours=-12)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('PT5H', timedelta(hours=5)),
|
2020-12-21 09:11:55 +00:00
|
|
|
('-PT5H', timedelta(hours=-5)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('PT5M', timedelta(minutes=5)),
|
2020-12-21 09:11:55 +00:00
|
|
|
('-PT5M', timedelta(minutes=-5)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('PT5S', timedelta(seconds=5)),
|
2020-12-21 09:11:55 +00:00
|
|
|
('-PT5S', timedelta(seconds=-5)),
|
2017-07-03 21:08:58 +00:00
|
|
|
('PT0.000005S', timedelta(microseconds=5)),
|
2019-11-27 07:14:00 +00:00
|
|
|
('PT0,000005S', timedelta(microseconds=5)),
|
2020-12-21 09:11:55 +00:00
|
|
|
('-PT0.000005S', timedelta(microseconds=-5)),
|
|
|
|
('-PT0,000005S', timedelta(microseconds=-5)),
|
2020-12-19 09:53:35 +00:00
|
|
|
('-P4DT1H', timedelta(days=-4, hours=-1)),
|
2017-07-03 21:08:58 +00:00
|
|
|
)
|
|
|
|
for source, expected in test_values:
|
|
|
|
with self.subTest(source=source):
|
|
|
|
self.assertEqual(parse_duration(source), expected)
|