예제 #1
0
 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))
예제 #2
0
 def test_negative(self):
     test_values = (
         ('-4 15:30', timedelta(days=-4, minutes=15, seconds=30)),
         ('-172800', timedelta(days=-2)),
         ('-15:30', timedelta(minutes=-15, seconds=30)),
         ('-1:15:30', timedelta(hours=-1, minutes=15, seconds=30)),
         ('-30.1', timedelta(seconds=-30, milliseconds=-100)),
     )
     for source, expected in test_values:
         with self.subTest(source=source):
             self.assertEqual(parse_duration(source), expected)
예제 #3
0
 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:
         with self.subTest(delta=delta):
             self.assertEqual(parse_duration(format(delta)), delta)
예제 #4
0
 def test_iso_8601(self):
     test_values = (
         ('P4Y', None),
         ('P4M', None),
         ('P4W', None),
         ('P4D', timedelta(days=4)),
         ('P0.5D', timedelta(hours=12)),
         ('PT5H', timedelta(hours=5)),
         ('PT5M', timedelta(minutes=5)),
         ('PT5S', timedelta(seconds=5)),
         ('PT0.000005S', timedelta(microseconds=5)),
     )
     for source, expected in test_values:
         with self.subTest(source=source):
             self.assertEqual(parse_duration(source), expected)
예제 #5
0
 def test_fractions_of_seconds(self):
     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)),
     )
     for source, expected in test_values:
         with self.subTest(source=source):
             self.assertEqual(parse_duration(source), expected)
예제 #6
0
 def test_parse_postgresql_format(self):
     test_values = (
         ('1 day', timedelta(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)),
         ('-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)
예제 #7
0
 def test_seconds(self):
     self.assertEqual(parse_duration('30'), timedelta(seconds=30))
예제 #8
0
 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))