本文整理匯總了Python中khayyam.JalaliDatetime.to_datetime方法的典型用法代碼示例。如果您正苦於以下問題:Python JalaliDatetime.to_datetime方法的具體用法?Python JalaliDatetime.to_datetime怎麽用?Python JalaliDatetime.to_datetime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類khayyam.JalaliDatetime
的用法示例。
在下文中一共展示了JalaliDatetime.to_datetime方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestJalaliDateTime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import to_datetime [as 別名]
class TestJalaliDateTime(unittest.TestCase):
def setUp(self):
self.leap_year = 1375
self.naive_jdt = JalaliDatetime(self.leap_year, 12, 30, 10, 2, 1, 3)
self.aware_jdt = JalaliDatetime(self.leap_year, 12, 30, 10, 2, 1, 3, TehTz())
def test_to_from_datetime(self):
# Naive
jdate1 = JalaliDatetime.from_datetime(self.naive_jdt.to_datetime())
self.assertEqual(self.naive_jdt, jdate1)
# Aware
jdate2 = JalaliDatetime.from_datetime(self.aware_jdt.to_datetime())
self.assertEqual(self.aware_jdt, jdate2)
def test_today(self):
dt = datetime.now().date()
jdt = JalaliDatetime.today().date()
self.assertEqual(jdt, JalaliDate.from_date(dt))
def test_now(self):
self.assertTrue(JalaliDatetime.now() != None)
def test_utcnow(self):
jutcnow = JalaliDatetime.utcnow()
utcnow = jutcnow.to_datetime()
self.assertEqual(jutcnow.time(), utcnow.time())
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)
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'), u'پپنجشنبهاساسفندپ 23 اس 75 12:3پنجشنبه 23 اسفند 1375 12:3:45 ب.ظ23345671212359123ب.ظ455پنجشنبه 23 اسفند 1375 12:3:45 ب.ظ12:3:45 ب.ظ751375%51')
def test_algorithm(self):
min = datetime(1900, 1, 1, 1, 1, 1)
max_days = 5000 # 3000 years !
days = 0
while True:
dt = min + timedelta(days=days)
jd = JalaliDatetime.from_datetime(dt)
print('Processing day: %s' % jd.year)
dt2 = jd.to_datetime()
self.assertEqual(dt, dt2)
days += 1
if days > max_days:
break;
# def test_timetuple(self):
# jdate = JalaliDatetime(self.leap_year, 12, 23, 12, 3, 45, 34567, teh_tz)
# self.assertEqual(jdate.timetuple().__repr__(), 'time.struct_time(tm_year=1375, tm_mon=12, tm_mday=23, tm_hour=12, tm_min=3, tm_sec=45, tm_wday=3, tm_yday=359, tm_isdst=2)')
# self.assertEqual(jdate.utctimetuple().__repr__(), 'time.struct_time(tm_year=1375, tm_mon=12, tm_mday=23, tm_hour=8, tm_min=33, tm_sec=45, tm_wday=3, tm_yday=359, tm_isdst=0)')
def test_iso_format(self):
jdate = JalaliDate(self.leap_year, 12, 23)
self.assertEqual(jdate.isoformat(), '%s-12-23' % self.leap_year)
def test_add(self):
jdate = JalaliDate(self.leap_year, 12, 23)
jdate2 = jdate + timedelta(10)
self.assertEqual(jdate2, JalaliDate(self.leap_year + 1, 1, 3))
def test_sub(self):
jdate = JalaliDate(self.leap_year, 12, 23)
jdate2 = jdate - timedelta(10)
self.assertEqual(jdate2, JalaliDate(self.leap_year, 12, 13))
jtimedelta = jdate - JalaliDate(self.leap_year - 1, 12, 1)
self.assertEqual(jtimedelta, timedelta(387))
def test_lt_gt_le_ge_ne_eg(self):
jdate = JalaliDate(self.leap_year, 12, 23)
jdate2 = JalaliDate(self.leap_year, 12, 24)
jdate3 = JalaliDate(self.leap_year, 12, 24)
self.assertTrue(jdate <= jdate2)
self.assertTrue(jdate != jdate2)
self.assertFalse(jdate > jdate2)
self.assertTrue(jdate2 == jdate3)
def test_replace(self):
self.assertRaises(ValueError, self.naive_jdt.replace,
self.leap_year + 1)
示例2: TestJalaliDateTime
# 需要導入模塊: from khayyam import JalaliDatetime [as 別名]
# 或者: from khayyam.JalaliDatetime import to_datetime [as 別名]
class TestJalaliDateTime(unittest.TestCase):
def setUp(self):
self.leap_year = 1375
self.naive_jdt = JalaliDatetime(self.leap_year, 12, 30, 10, 2, 1, 3)
self.aware_jdt = JalaliDatetime(self.leap_year, 12, 30, 10, 2, 1, 3, TehranTimezone())
def test_to_from_datetime(self):
# Naive
jdate1 = JalaliDatetime.from_datetime(self.naive_jdt.to_datetime())
self.assertEqual(self.naive_jdt, jdate1)
# Aware
jdate2 = JalaliDatetime.from_datetime(self.aware_jdt.to_datetime())
self.assertEqual(self.aware_jdt, jdate2)
def test_today(self):
dt = datetime.now().date()
jdt = JalaliDatetime.today().date()
self.assertEqual(jdt, JalaliDate.from_date(dt))
def test_now(self):
self.assertIsNotNone(JalaliDatetime.now())
self.assertIsNone(JalaliDatetime.now().tzinfo)
self.assertIsNotNone(JalaliDatetime.now(TehranTimezone()).tzinfo)
def test_utcnow(self):
jutcnow = JalaliDatetime.utcnow()
utcnow = jutcnow.to_datetime()
self.assertEqual(jutcnow.time(), utcnow.time())
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)))
)
#.........這裏部分代碼省略.........