Пример #1
0
    def test_timezone(self):
        """
        Testing:
            %z            UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).
            %o            UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive) in persian format i.e +۰۳:۳۰.
            %Z            Time zone name (empty string if the object is naive).
        """
        # Test Timezone
        tz_dt = JalaliDatetime(tzinfo=Timezone(timedelta(minutes=10)))
        naive_dt = tz_dt.replace(tzinfo=None)
        self.assertEqual(JalaliDatetime.strptime('00:10', '%z'), tz_dt)
        self.assertEqual(JalaliDatetime.strptime('', '%z'), naive_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(tz_dt.strftime('%o'), u'-۰۰:۳۰')
        self.assertEqual(JalaliDatetime.strptime('', '%o'), 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(naive_dt.strftime('%z'), '')
        self.assertEqual(naive_dt.strftime('%Z'), '')

        self.assertEqual(JalaliDatetime.strptime(u'۰۰:۰۰', '%o'), JalaliDatetime())
        self.assertEqual(JalaliDatetime.strptime(u'۰۰:۰۱', '%o'), JalaliDatetime(tzinfo=Timezone(timedelta(minutes=1))))
        self.assertNotEqual(JalaliDatetime.strptime(u'۰۴:۳۰', '%o'), JalaliDatetime.strptime('04:31', '%z'))
        self.assertEqual(JalaliDatetime.strptime(u'۰۴:۳۰', '%o'), JalaliDatetime.strptime('04:30', '%z'))
        self.assertNotEqual(JalaliDatetime.strptime(u'۰۴:۳۰', '%o'), JalaliDatetime(tzinfo=teh_tz))
        self.assertEqual(JalaliDatetime.strptime(u'+۰۴:۳۰', '%o').utcoffset(), timedelta(hours=4.50))
        self.assertEqual(naive_dt.strftime('%o'), '')

        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)))
            )

        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)))
        )
Пример #2
0
 def test_replace(self):
     d1 = JalaliDatetime(1391, 12, 30)
     self.assertEqual(d1.replace(year=1395), JalaliDatetime(1395, 12, 30))
     self.assertEqual(d1.replace(month=1),   JalaliDatetime(1391, 1, 30))
     self.assertEqual(d1.replace(day=1),     JalaliDatetime(1391, 12, 1))
     self.assertRaises(ValueError, d1.replace, year=1392)
Пример #3
0
 def test_replace(self):
     d1 = JalaliDatetime(1391, 12, 30)
     self.assertEqual(d1.replace(year=1395), JalaliDatetime(1395, 12, 30))
     self.assertEqual(d1.replace(month=1),   JalaliDatetime(1391, 1, 30))
     self.assertEqual(d1.replace(day=1),     JalaliDatetime(1391, 12, 1))
     self.assertRaises(ValueError, d1.replace, year=1392)