예제 #1
0
 def test_microseconds(self):
     duration = datetime.timedelta(hours=1,
                                   minutes=3,
                                   seconds=5,
                                   microseconds=12345)
     self.assertEqual(parse_duration(duration_iso_string(duration)),
                      duration)
예제 #2
0
파일: json.py 프로젝트: iMerica/dj-models
 def default(self, o):
     # See "Date Time String Format" in the ECMA-262 specification.
     if isinstance(o, datetime.datetime):
         r = o.isoformat()
         if o.microsecond:
             r = r[:23] + r[26:]
         if r.endswith('+00:00'):
             r = r[:-6] + 'Z'
         return r
     elif isinstance(o, datetime.date):
         return o.isoformat()
     elif isinstance(o, datetime.time):
         if is_aware(o):
             raise ValueError("JSON can't represent timezone-aware times.")
         r = o.isoformat()
         if o.microsecond:
             r = r[:12]
         return r
     elif isinstance(o, datetime.timedelta):
         return duration_iso_string(o)
     elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
         return str(o)
     else:
         return super().default(o)
예제 #3
0
 def test_negative(self):
     duration = datetime.timedelta(days=-1, hours=1, minutes=3, seconds=5)
     self.assertEqual(
         parse_duration(duration_iso_string(duration)).total_seconds(),
         duration.total_seconds())
예제 #4
0
 def test_days(self):
     duration = datetime.timedelta(days=1, hours=1, minutes=3, seconds=5)
     self.assertEqual(parse_duration(duration_iso_string(duration)),
                      duration)
예제 #5
0
 def test_negative(self):
     duration = -1 * datetime.timedelta(
         days=1, hours=1, minutes=3, seconds=5)
     self.assertEqual(duration_iso_string(duration), '-P1DT01H03M05S')
예제 #6
0
 def test_microseconds(self):
     duration = datetime.timedelta(hours=1,
                                   minutes=3,
                                   seconds=5,
                                   microseconds=12345)
     self.assertEqual(duration_iso_string(duration), 'P0DT01H03M05.012345S')
예제 #7
0
 def test_simple(self):
     duration = datetime.timedelta(hours=1, minutes=3, seconds=5)
     self.assertEqual(duration_iso_string(duration), 'P0DT01H03M05S')