示例#1
0
 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)
示例#2
0
    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')
示例#3
0
    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 assert_parse_and_format(self, jdate, fmt, print_=False):
     jalali_date_str = jdate.strftime(fmt)
     parsed_dt = JalaliDatetime.strptime(jalali_date_str, fmt)
     if print_:
         print(jalali_date_str)
     if jdate != parsed_dt:
         print(repr(jdate))
         print(jalali_date_str)
         print(repr(parsed_dt))
     self.assertEqual(jdate, parsed_dt)
示例#5
0
    def test_am_pm(self):
        """
        Testing:
            %p            Locale’s equivalent of either AM or PM in persian format [ق.ظ, ب.ظ].
            %t            Locale’s equivalent of either AM or PM in ASCII format.
        """
        d1 = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567)
        self.assert_parse_and_format(d1, '%Y-%m-%d %p %I:%M:%S.%f')
        self.assert_parse_and_format(d1, '%Y-%m-%d %t %I:%M:%S.%F')
        self.assert_parse_and_format(JalaliDatetime(1375, 12, 23, 12, 0, 0, 0),
                                     '%Y-%m-%d %p %I:%M:%S.%F')
        self.assert_parse_and_format(JalaliDatetime(1375, 12, 23, 12, 0, 0, 0),
                                     '%Y-%m-%d %t %I:%M:%S.%F')

        d2 = JalaliDatetime(self.leap_year, 12, 23)
        for i in range(500):
            d_test = d2 + timedelta(hours=i)
            self.assert_parse_and_format(d_test, '%Y-%m-%d %p %I:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %p %i:%r:%s')
            self.assert_parse_and_format(d_test, '%Y-%m-%d %t %I:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %t %i:%r:%s')

        with self.assertRaises(ValueError):
            JalaliDatetime.strptime('۹:۲۴ ق.ظ', '%k:%r %p')
示例#6
0
    def read_single_post(self, post_url):

        post = {}

        post["id"] = post_url[post_url.rfind("/") + 1:post_url.rfind(".")]

        fp = urllib.request.urlopen(post_url)
        my_bytes = fp.read()

        page = my_bytes.decode("utf8")
        fp.close()
        soup = BeautifulSoup(page, 'html.parser')
        post["title"] = soup.select("#content h2 a")[0]
        abstract = soup.select("#content .box-note")[0]

        source_date = soup.select("#content .published")[0].getText().replace('تاریخ انتشار :  ', "") \
            .replace('\n', "") \
            .replace('\t', "")
        shamsi_date = JalaliDatetime.strptime(source_date,
                                              '%A, %d %B %Y %H:%M')
        shamsi_datexx = JalaliDatetime(shamsi_date.year, shamsi_date.month,
                                       shamsi_date.day, shamsi_date.hour,
                                       shamsi_date.minute, shamsi_date.second,
                                       0, TehranTimezone())

        source_date_utc = shamsi_datexx.todatetime().astimezone(
            pytz.timezone('UTC'))
        # localtime_shamsi_date = local.localize(source_date_local, is_dst=None)

        image_src = soup.select("#content p.imgarticle img")[0]["src"]
        post["source_media_url"] = "http://beytoote.com/" + image_src
        main_image_file_name = post["source_media_url"][
            post["source_media_url"].rfind("/") + 1:]
        post["media_type"] = 2

        all_paragraphs = soup.select("#content p")
        post["body"] = ""
        for p in all_paragraphs:
            paragraph_text = p.getText().strip()
            if len(paragraph_text) > 0:
                post["body"] = post["body"] + paragraph_text + "\n"
                # print(paragraph_text)

        post["date"] = source_date_utc
        return post
示例#7
0
    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_hour(self):
        """
        Testing:
            %H            Hour (24-hour clock) as a zero padded decimal number [00, 23].
            %k            Hour (24-hour clock) as a decimal number in persian form [۱, ۲۳].
            %h            Hour (24-hour clock) as a zero padded decimal number in persian form [۰۰, ۲۳].
            %I            Hour (12-hour clock) as a zero padded decimal number [01, 12].
            %i            Hour (12-hour clock) as a zero padded decimal number in persian form [۰۱, ۱۲].
            %l            Hour (12-hour clock) as a decimal number in persian form [۱, ۱۲].
        """
        d1 = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567)

        # Test HOUR 24
        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.strptime('08', '%H'),
                         JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'2', '%H')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%k'), u'۰')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%k'), u'۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 12).strftime('%k'), u'۱۲')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 13).strftime('%k'), u'۱۳')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 23).strftime('%k'), u'۲۳')
        self.assertEqual((JalaliDatetime(1386, 12, 23, 23, 59, 59) +
                          timedelta(seconds=1)).strftime('%k'), u'۰')
        self.assertEqual(JalaliDatetime.strptime(u'۸', '%k'),
                         JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۰۲', '%k')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%h'), u'۰۰')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%h'), u'۰۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 12).strftime('%h'), u'۱۲')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 13).strftime('%h'), u'۱۳')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 23).strftime('%h'), u'۲۳')
        self.assertEqual((JalaliDatetime(1386, 12, 23, 23, 59, 59) +
                          timedelta(seconds=1)).strftime('%h'), u'۰۰')
        self.assertEqual(JalaliDatetime.strptime(u'۰۸', '%h'),
                         JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۲', '%h')

        # Test HOUR 12
        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.assertRaises(ValueError, JalaliDatetime.strptime, u'2', '%I')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%l'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%l'), u'۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 12).strftime('%l'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%l'), u'۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 23).strftime('%l'), u'۱۱')
        self.assertEqual((JalaliDatetime(1386, 12, 23, 23, 59, 59) +
                          timedelta(seconds=1)).strftime('%l'), u'۱۲')
        self.assertEqual(d1.strftime('%l'), u'۱۲')
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۰۲', '%l')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%i'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%i'), u'۰۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 12).strftime('%i'), u'۱۲')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 13).strftime('%i'), u'۰۱')
        self.assertEqual(
            JalaliDatetime(1386, 12, 23, 23).strftime('%i'), u'۱۱')
        self.assertEqual((JalaliDatetime(1386, 12, 23, 23, 59, 59) +
                          timedelta(seconds=1)).strftime('%i'), u'۱۲')
        self.assertEqual(d1.strftime('%i'), u'۱۲')
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۲', '%i')

        d2 = JalaliDatetime(self.leap_year, 12, 23)
        for i in range(1000):
            d_test = d2 + timedelta(hours=i)
            self.assert_parse_and_format(d_test, '%Y-%m-%d %H:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %k:%r:%s')
            self.assert_parse_and_format(d_test, '%N-%R-%D %h:%v:%L')
            self.assert_parse_and_format(d_test, '%Y-%m-%d %p %I:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %t %i:%r:%s')
            self.assert_parse_and_format(d_test, '%N-%R-%D %p %l:%v:%L')
    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)))
        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(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(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(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))))

        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))))
    def test_hour(self):
        """
        Testing:
            %H            Hour (24-hour clock) as a zero padded decimal number [00, 23].
            %k            Hour (24-hour clock) as a decimal number in persian form [۱, ۲۳].
            %h            Hour (24-hour clock) as a zero padded decimal number in persian form [۰۰, ۲۳].
            %I            Hour (12-hour clock) as a zero padded decimal number [01, 12].
            %i            Hour (12-hour clock) as a zero padded decimal number in persian form [۰۱, ۱۲].
            %l            Hour (12-hour clock) as a decimal number in persian form [۱, ۱۲].
        """
        d1 = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567)

        # Test HOUR 24
        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.strptime('08', '%H'), JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'2', '%H')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%k'), u'۰')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%k'), u'۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%k'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%k'), u'۱۳')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%k'), u'۲۳')
        self.assertEqual(
            (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%k'), u'۰')
        self.assertEqual(JalaliDatetime.strptime(u'۸', '%k'), JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۰۲', '%k')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%h'), u'۰۰')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%h'), u'۰۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%h'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%h'), u'۱۳')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%h'), u'۲۳')
        self.assertEqual(
            (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%h'), u'۰۰')
        self.assertEqual(JalaliDatetime.strptime(u'۰۸', '%h'), JalaliDatetime(hour=8))
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۲', '%h')

        # Test HOUR 12
        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.assertRaises(ValueError, JalaliDatetime.strptime, u'2', '%I')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%l'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%l'), u'۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%l'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%l'), u'۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%l'), u'۱۱')
        self.assertEqual(
            (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%l'), u'۱۲')
        self.assertEqual(d1.strftime('%l'), u'۱۲')
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۰۲', '%l')

        self.assertEqual(JalaliDatetime(1386, 12, 23).strftime('%i'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 1).strftime('%i'), u'۰۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 12).strftime('%i'), u'۱۲')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 13).strftime('%i'), u'۰۱')
        self.assertEqual(JalaliDatetime(1386, 12, 23, 23).strftime('%i'), u'۱۱')
        self.assertEqual(
            (JalaliDatetime(1386, 12, 23, 23, 59, 59) + timedelta(seconds=1)).strftime('%i'), u'۱۲')
        self.assertEqual(d1.strftime('%i'), u'۱۲')
        self.assertRaises(ValueError, JalaliDatetime.strptime, u'۲', '%i')

        d2 = JalaliDatetime(self.leap_year, 12, 23)
        for i in range(1000):
            d_test = d2 + timedelta(hours=i)
            self.assert_parse_and_format(d_test, '%Y-%m-%d %H:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %k:%r:%s')
            self.assert_parse_and_format(d_test, '%N-%R-%D %h:%v:%L')
            self.assert_parse_and_format(d_test, '%Y-%m-%d %p %I:%M:%S')
            self.assert_parse_and_format(d_test, '%N-%R-%D %t %i:%r:%s')
            self.assert_parse_and_format(d_test, '%N-%R-%D %p %l:%v:%L')
    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)))
        )
示例#12
0
 def assert_parse_and_format(self, jdate, fmt):
     jalali_date_str = jdate.strftime(fmt)
     parsed_dt = JalaliDatetime.strptime(jalali_date_str, fmt)
     self.assertEqual(jdate, parsed_dt)