def test_affect(self): self.assertEquals( time(1, 2, 3, 0, SimpleTZInfo(0)).isoformat(), '01:02:03+00:00') self.assertEquals( time(1, 2, 3, 0, SimpleTZInfo(90)).isoformat(), '01:02:03+01:30') self.assertEquals( time(1, 2, 3, 0, SimpleTZInfo(-90)).isoformat(), '01:02:03-01:30')
def test_datetime_string_conversion(self): schema = schemaish.DateTime() converter = string_converter(schema) tz = SimpleTZInfo(90) tests = [ (datetime(2001, 2, 3, 4, 5, 6), '2001-02-03T04:05:06'), (datetime(2001, 2, 3), '2001-02-03T00:00:00'), (datetime(2001, 2, 3, 4, 5, 6, 7), '2001-02-03T04:05:06.000007'), (datetime(2001, 2, 3, 4, 5, 6, tzinfo=tz), '2001-02-03T04:05:06+01:30'), ] for t, s in tests: self.assertEquals(converter.from_type(t), s) self.assertEquals(converter.to_type(s), t)
def test_time_string_conversion(self): schema = schemaish.Time() converter = string_converter(schema) tz = SimpleTZInfo(90) tests = [(time(1), '01:00:00'), (time(1, 2), '01:02:00'), (time(1, 2, 3), '01:02:03'), (time(1, 2, 3, 4), '01:02:03.000004'), (time(1, 0, 0, 0, tz), '01:00:00+01:30'), (time(1, 2, 0, 0, tz), '01:02:00+01:30'), (time(1, 2, 3, 0, tz), '01:02:03+01:30'), (time(1, 2, 3, 4, tz), '01:02:03.000004+01:30')] for t, s in tests: self.assertEquals(converter.from_type(t), s) self.assertEquals(converter.to_type(s), t)
def _parse_time(value): # Parse the timezone offset if '+' in value: value, tz = value.split('+') tzdir = 1 elif '-' in value: value, tz = value.split('-') tzdir = -1 else: tz = None if tz: hours, minutes = tz.split(':') tz = SimpleTZInfo(tzdir * ((int(hours) * 60) + int(minutes))) # Parse milliseconds. if '.' in value: value, ms = value.split('.') else: ms = 0 # Parse hours, minutes and seconds. try: parts = value.split(':') if len(parts) < 2 or len(parts) > 3: raise ValueError() if len(parts) == 2: h, m = parts s = 0 else: h, m, s = parts h, m, s, ms = int(h), int(m), int(s), int(ms) except: raise ConvertError('Invalid time') try: value = time(h, m, s, ms, tz) except ValueError, e: raise ConvertError('Invalid time: ' + str(e))
def test_utcoffset(self): self.assertEquals( SimpleTZInfo(60).utcoffset(None), timedelta(minutes=60)) self.assertEquals( SimpleTZInfo(-60).utcoffset(None), timedelta(minutes=-60))
def test_tzname(self): self.assertEquals(SimpleTZInfo(0).tzname(None), "+00:00") self.assertEquals(SimpleTZInfo(60).tzname(None), "+01:00") self.assertEquals(SimpleTZInfo(90).tzname(None), "+01:30") self.assertEquals(SimpleTZInfo(-60).tzname(None), "-01:00") self.assertEquals(SimpleTZInfo(-90).tzname(None), "-01:30")
def test_dst(self): tz = SimpleTZInfo(60) self.assertEquals(tz.dst(None), timedelta())