本文整理匯總了Python中khayyam.JalaliDatetime.strftime方法的典型用法代碼示例。如果您正苦於以下問題:Python JalaliDatetime.strftime方法的具體用法?Python JalaliDatetime.strftime怎麽用?Python JalaliDatetime.strftime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類khayyam.JalaliDatetime
的用法示例。
在下文中一共展示了JalaliDatetime.strftime方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_timezone
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
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: test_strftime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
def test_strftime(self):
jdate = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567)
self.assertEqual(jdate.isoformat(), '%s-12-23T12:3:45.34567' % self.leap_year)
## "%U%W" not implemented
self.assertEqual(jdate.strftime(u'%a%A%b%B%c%C%d%f%H%I%j%m%M%p%S%w%x%X%y%Y%z%Z%%'), 'پپنجشنبهاساسفندپ 23 اس 75 12:3پنجشنبه 23 اسفند 1375 12:3:45 ب.ظ23345671212359123ب.ظ453پنجشنبه 23 اسفند 1375 12:3:45 ب.ظ12:3:45 ب.ظ751375%')
示例3: test_strftime_strptime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
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')
示例4: localize_datetime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
def localize_datetime(dt, language_code=None):
if language_code == 'fa':
jdt = JalaliDatetime(dt)
return jdt.strftime('%C')
else:
return dt
示例5: test_strftime_strptime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
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')
示例6: test_hour
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
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')
示例7: test_strftime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import strftime [as 別名]
def test_strftime(self):
jdate = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567)
self.assertEqual(jdate.isoformat(), '%s-12-23T12:03:45.034567' % self.leap_year)
self.assertEqual(jdate.strftime(u'%a%A%b%B%c%C%d%f%H%I%j%m%M%p%S%w%x%X%y%Y%z%Z%%%W%e%E%g%G'), u'پپنجشنبهاساسفندپ 23 اس 75 12:03پنجشنبه 23 اسفند 1375 12:03:45 ب.ظ2303456712123591203ب.ظ455پنجشنبه 23 اسفند 1375 12:03:45 ب.ظ12:03:45 ب.ظ751375%51PPanjshanbehEEsfand')