def rt(dts): """Do simple round-trip.""" return datetime_to_str(str_to_datetime(dts))
def test01_datetime_to_str(self): """Writing.""" self.assertEqual(datetime_to_str(0), "1970-01-01T00:00:00Z") self.assertEqual(datetime_to_str(0.000001), "1970-01-01T00:00:00.000001Z") self.assertEqual(datetime_to_str(0.1), "1970-01-01T00:00:00.100000Z") self.assertEqual(datetime_to_str(1), "1970-01-01T00:00:01Z") self.assertEqual(datetime_to_str(60), "1970-01-01T00:01:00Z") self.assertEqual(datetime_to_str(60 * 60), "1970-01-01T01:00:00Z") self.assertEqual(datetime_to_str(60 * 60 * 24), "1970-01-02T00:00:00Z") self.assertEqual(datetime_to_str(60 * 60 * 24 * 31), "1970-02-01T00:00:00Z") self.assertEqual(datetime_to_str(60 * 60 * 24 * 365), "1971-01-01T00:00:00Z") # Random other datetime self.assertEqual(datetime_to_str(1234567890), "2009-02-13T23:31:30Z") # Rounding issues self.assertEqual(datetime_to_str(0.199999), "1970-01-01T00:00:00.199999Z") self.assertEqual(datetime_to_str(0.1999991), "1970-01-01T00:00:00.199999Z") self.assertEqual(datetime_to_str(0.1999999), "1970-01-01T00:00:00.200000Z") self.assertEqual(datetime_to_str(0.200000), "1970-01-01T00:00:00.200000Z") self.assertEqual(datetime_to_str(0.2000001), "1970-01-01T00:00:00.200000Z") self.assertEqual(datetime_to_str(0.2000009), "1970-01-01T00:00:00.200001Z") self.assertEqual(datetime_to_str(0.200001), "1970-01-01T00:00:00.200001Z") # No fractions self.assertEqual(datetime_to_str(100, True), "1970-01-01T00:01:40Z") self.assertEqual(datetime_to_str(0.2000009, True), "1970-01-01T00:00:00Z") self.assertEqual(datetime_to_str(0.200001, no_fractions=True), "1970-01-01T00:00:00Z") # Special cases self.assertEqual(datetime_to_str(None), None) nt = datetime_to_str('now', no_fractions=True) self.assertTrue( re.match(r'''\d\d\d\d\-\d\d\-\d\dT\d\d:\d\d:\d\dZ''', nt))