def test_strftime_strptime(self): d1 = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567) self.assertEqual(d1.strftime('%Q'), 'Panjshanbeh 23 Esfand 1375 12:03:45 PM') self.assertEqual( d1.strptime('Panjshanbeh 23 Esfand 1375 12:03:45 PM', '%Q'), d1 - timedelta(microseconds=34567)) self.assertEqual(d1.isoformat(), '%s-12-23T12:03:45.034567' % self.leap_year) tz_datetime = d1.astimezone(teh_tz) self.assertEqual(tz_datetime.strftime('%Z'), 'Iran/Tehran')
def test_strftime_strptime(self): def check_format(jdate ,fmt): jdate_str = jdate.strftime(fmt) parsed_dt = JalaliDatetime.strptime(jdate_str, fmt) if jdate != parsed_dt: print(repr(jdate)) print(jdate_str) print(repr(parsed_dt)) self.assertEqual(jdate, parsed_dt) d1 = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567) self.assertEqual(d1.strftime('%Q'), 'Panjshanbeh 23 Esfand 1375 12:03:45 PM') # Test HOUR self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%H'), u'00') self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%H'), u'01') self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%H'), u'12') self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%H'), u'13') self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%H'), u'23') self.assertEqual( (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%H'), u'00') self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%I'), u'12') self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%I'), u'01') self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%I'), u'12') self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%I'), u'01') self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%I'), u'11') self.assertEqual( (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%I'), u'12') self.assertEqual(d1.strftime('%I'), u'12') self.assertEqual(JalaliDatetime.strptime('8', '%H'), JalaliDatetime(hour=8)) # Test Timezone tz_dt = JalaliDatetime(tzinfo=Timezone(timedelta(minutes=10))) self.assertEqual(JalaliDatetime.strptime('00:10', '%z'), tz_dt) self.assertEqual(tz_dt.strftime('%z'), '+00:10') tz_dt = JalaliDatetime(tzinfo=Timezone(timedelta(minutes=-30))) self.assertEqual(tz_dt.strftime('%z'), '-00:30') self.assertEqual(JalaliDatetime.strptime('', '%z'), JalaliDatetime()) self.assertEqual(JalaliDatetime.strptime('00:00', '%z'), JalaliDatetime()) self.assertEqual(JalaliDatetime.strptime('00:01', '%z'), JalaliDatetime(tzinfo=Timezone(timedelta(minutes=1)))) self.assertNotEqual(JalaliDatetime.strptime('04:30', '%z'), JalaliDatetime.strptime('04:31', '%z')) self.assertEqual(JalaliDatetime.strptime('04:30', '%z'), JalaliDatetime.strptime('04:30', '%z')) self.assertNotEqual(JalaliDatetime.strptime('04:30', '%z'), JalaliDatetime(tzinfo=teh_tz)) self.assertEqual(JalaliDatetime.strptime('+04:30', '%z').utcoffset(), timedelta(hours=4.50)) self.assertEqual(tz_dt.strftime('%z'), tz_dt.strftime('%Z')) self.assertEqual( JalaliDatetime(1394, 4, 28, 18, 14, 35, 962659, Timezone(timedelta(.3))).strftime('%Y-%m-%d %H:%M:%S.%f %z'), '1394-04-28 18:14:35.962659 +07:12') self.assertEqual( JalaliDatetime.strptime('1394-04-28 18:14:35.962659 +07:12', '%Y-%m-%d %H:%M:%S.%f %z'), JalaliDatetime(1394, 4, 28, 18, 14, 35, 962659, Timezone(timedelta(.3))) ) check_format(d1, '%Y-%m-%d %H:%M:%S.%f') check_format(JalaliDatetime(1375, 12, 23, 12, 0, 0, 0), '%Y-%m-%d %p %I:%M:%S.%f') self.assertEqual( JalaliDatetime.strptime(u'1394-تیر-29 دوشنبه 00:05:14.113389 +04:30', '%Y-%B-%d %A %H:%M:%S.%f %z'), JalaliDatetime(1394, 4, 29, 0, 5, 14, 113389, Timezone(timedelta(hours=4, minutes=30))) ) d2 = JalaliDatetime(self.leap_year, 12, 23) for i in xrange(100): d_test = d2 + timedelta(hours=i) check_format(d_test, '%Y%m%d%H%a%A%b%B%c%C%f%I%j%M%p%S%w%x%X%y%g%G%e%E%W%%') check_format(d_test, '%Y-%m-%d %p %I:%M:%S.%f') check_format(d_test, '%Y-%m-%d %X') check_format(d_test, '%x %H') check_format(d_test, '%c') check_format(d_test, '%C') check_format(d_test, '%q') check_format(d_test, '%Q') self.assertEqual(d1.isoformat(), '%s-12-23T12:03:45.034567' % self.leap_year) tz_datetime = d1.astimezone(teh_tz) self.assertEqual(tz_datetime.strftime('%Z'), 'Iran/Tehran')
def test_astimezone(self): d1 = JalaliDatetime(1395, 5, 29, 22, 18, 32, 749938, tzinfo=teh_tz) tz_fake = Timezone(timedelta(hours=2)) self.assertEqual(d1.astimezone(teh_tz), d1) self.assertEqual(d1.astimezone(tz_fake), JalaliDatetime(1395, 5, 29, 19, 48, 32, 749938, tzinfo=tz_fake))