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))) )
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%')
def getJalaliDateFromISO(isoDate): dt = datetime.datetime.strptime(isoDate, '%Y-%m-%dT%H:%M:%S.%fZ') if translation.get_language() not in ['fa', 'ps']: return localtime(dt).strftime('%Y-%m-%d %H:%M:%S') dt = dt.replace(tzinfo=from_zone) dt = dt.astimezone(to_zone) jl = JalaliDatetime(dt) jlStr = jl.strftime('%N/%R/%D %k:%r:%s') return jlStr
def getJalaliDate(dt): if not dt: return '' if translation.get_language() not in ['fa', 'ps']: return localtime(dt).strftime('%Y-%m-%d %H:%M:%S') dt = dt.replace(tzinfo=from_zone) dt = dt.astimezone(to_zone) jl = JalaliDatetime(dt) jlStr = jl.strftime('%N/%R/%D %k:%r:%s') return jlStr
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 localize_datetime(dt, language_code=None): if language_code == 'fa': jdt = JalaliDatetime(dt) return jdt.strftime('%C') else: return dt
def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) if content_type == 'text': command = msg['text'] print('Got command: %s' % command) mss = command mss = mss.replace('/', ',') mss = mss.replace('.', ',') mss = mss.replace('-', ',') mss = mss.split(',') now = JalaliDatetime.now() zz = "" zzz = '' kk = 0 if len(mss) < 3: if command == 'Time': ss = str(datetime.datetime.now()) elif command == '/help': sss = 'سلام واسه استفاده بايد تاريخ رو با يکي از فرمت هاي زير وارد کني' + '\n' + '1364/3/27' + '\n' + '1382-3-27' + '\n' + '1373.1.15' + '\n' + '1373,4,5' ss = str(sss) elif command == 'راهنما': sss = 'سلام واسه استفاده بايد تاريخ رو با يکي از فرمت هاي زير وارد کني' + '\n' + '1364/3/27' + '\n' + '1382-3-27' + '\n' + '1373.1.15' + '\n' + '1373,4,5' ss = str(sss) elif command == '/start': markupp = ReplyKeyboardMarkup(keyboard=[['راهنما']], resize_keyboard=True) bot.sendMessage(chat_id, "خیلی خوش اومدی لطفا یه تاریخ وارد کن", reply_markup=markupp) elif command == 'ممنون': ss = str("خواهش میکنم مهربون .. قابلی نداشت ") elif command == 'احمق': ss = str("خودتی") else: ss = 'اگه میشه تاریخ رو اینجوری وارد کن تا من بتونم بخونمش' + '\t' + '1372.11.5' kk = 1 else: databa = User.query.filter_by(name=str(chat_id)) if databa.count() > 0: tedadestefade = databa.count() com = databa[tedadestefade - 1].comment sumofuse = db.session.query(User).count() markup = ReplyKeyboardMarkup(keyboard=[[command], ['راهنما']], resize_keyboard=True) bot.sendMessage(chat_id, command, reply_markup=markup) old = JalaliDatetime(int(mss[0]), int(mss[1]), int(mss[2])) dif = [ 100, 200, 222, 300, 333, 400, 444, 500, 555, 600, 666, 700, 777, 800, 888, 900, 999, 1000, 1111, 2000, 2222, 3000, 3333, 4000, 4444, 5000, 5555, 6000, 6006, 6116, 6226, 6336, 6446, 6556, 6666, 6776, 6886, 6996, 7000, 7007, 7100, 7117, 7200, 7227, 7300, 7337, 7400, 7447, 7500, 7557, 7600, 7667, 7700, 7777, 7800, 7887, 7900, 7997, 8000, 8100, 8200, 8228, 8300, 8338, 8400, 8448, 8500, 8558, 8668, 8778, 8888, 9000, 9009, 9999, 10000, 11111, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 21000, 22000, 22222, 23000, 24000, 25000, 26000, 27000 ] i = 0 d = (now - old).days if now.dayofyear() < old.dayofyear(): ruzt = old.dayofyear() - now.dayofyear() else: ruzt = old.dayofyear() + 365 - now.dayofyear() ss = 'سلام امروز ' + str(d) + ' روزه هستی ' ss = ss + "\n" + str(ruzt) + " روز مونده تا به دنیا بیای " zzz = " you are " + str(d) + " days old " cc = d for x in dif: dif1 = datetime.timedelta(x) date = old + dif1 jdate = JalaliDatetime(date) if (jdate > now and i < 10): ss = ss + '\n' + str( jdate.strftime('%A %D %B %N')) + ' میشی ' + str( dif1.days) + " روزه " if (i < 1): zz = zz + '\n' + 'You will be ' + str( dif1.days) + ' days old in the next ' + str( dif1.days - cc) + ' days' i = i + 1 ss = ss + '\n' + '\n' + str( tedadestefade ) + " تعداد استفاده شما " + '\n' + str( sumofuse ) + " تعداد استفاده کل " + '\n' + 'مواظب خوبیات باش' + " @strixdaybot " zz = zz + '\n' + '\n' + ' Take care of your good behaviors' + '\n' + ' and appreciate your days of life' + '\n' + '\n' + '\n' + '\n' + '\n' + ' telegram: @strixdaybot ' img = requests.get("http://behkaroma.ir/p.jpg") ff = requests.get("http://behkaroma.ir/p.ttf") font1 = ImageFont.truetype(StringIO(ff.content), 30) font2 = ImageFont.truetype(StringIO(ff.content), 40) font3 = ImageFont.truetype(StringIO(ff.content), 50) image = Image.open(StringIO(img.content)) draw = ImageDraw.Draw(image) draw.text((30, 150), zzz, (0, 0, 0), font=font3) draw.text((20, 400), zz, (255, 255, 255), font=font1) bio = BytesIO() bio.name = 'image.jpeg' image.save(bio, 'JPEG') bio.seek(0) if (kk == 0): if len(zz) > 10: bot.sendPhoto(chat_id, photo=bio) reg = User(str(chat_id), command) print(reg) db.session.add(reg) db.session.commit() bot.sendMessage(chat_id, text=ss)
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_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 convert_to_jalali_date(value, format="%x"): from khayyam import JalaliDatetime jd = JalaliDatetime(value) return jd.strftime(format)
def to_persian(input, lang): if lang == "fa": dt = JalaliDatetime(input) return dt.strftime('%C') else: return formats.date_format(input, "SHORT_DATETIME_FORMAT")
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')
def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) if content_type == 'text': command = msg['text'] print('Got command: %s' % command) mss = command mss = mss.replace('/', ',') mss = mss.replace('.', ',') mss = mss.replace('-', ',') mss = mss.split(',') now = JalaliDatetime.now() zz = "" zzz = '' kk = 0 if len(mss) < 3: if command == 'Time': ss = str(datetime.datetime.now()) elif command == '/help': sss = 'سلام واسه استفاده بايد تاريخ رو با يکي از فرمت هاي زير وارد کني' + '\n' + '1364/3/27' + '\n' + '1382-3-27' + '\n' + '1373.1.15' + '\n' + '1373,4,5' ss = str(sss) elif command == '/start': ss = str("خیلی خوش اومدی لطفا یه تاریخ وارد کن") elif command == 'ممنون': ss = str("خواهش میکنم مهربون .. قابلی نداشت ") elif command == 'احمق': ss = str("خودتی") else: ss = 'اگه میشه تاریخ رو اینجوری وارد کن تا من بتونم بخونمش' + '\t' + '1372.11.5' kk = 1 else: old = JalaliDatetime(int(mss[0]), int(mss[1]), int(mss[2])) dif = [ 100, 200, 222, 300, 333, 400, 444, 500, 555, 600, 666, 700, 777, 800, 888, 900, 999, 1000, 1111, 2000, 2222, 3000, 3333, 4000, 4444, 5000, 5555, 6000, 6006, 6116, 6226, 6336, 6446, 6556, 6666, 6776, 6886, 6996, 7000, 7007, 7100, 7117, 7200, 7227, 7300, 7337, 7400, 7447, 7500, 7557, 7600, 7667, 7700, 7777, 7800, 7887, 7900, 7997, 8000, 8100, 8200, 8228, 8300, 8338, 8400, 8448, 8500, 8558, 8668, 8778, 8888, 9000, 9009, 9999, 10000, 11111, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 21000, 22000, 22222, 23000, 24000, 25000, 26000, 27000 ] i = 0 d = (now - old).days ss = 'سلام امروز ' + str(d) + ' روزه هستی ' zzz = " you are " + str(d) + " days old " cc = d for x in dif: dif1 = datetime.timedelta(x) date = old + dif1 jdate = JalaliDatetime(date) if (jdate > now and i < 10): ss = ss + '\n' + str( jdate.strftime('%A %D %B %N')) + ' میشی ' + str( dif1.days) + " روزه " if (i < 1): zz = zz + '\n' + 'You will be ' + str( dif1.days) + ' days old in the next ' + str( dif1.days - cc) + ' days' i = i + 1 ss = ss + '\n' + '\n' + 'مواظب خوبیات باش' + " @strixdaybot " zz = zz + '\n' + '\n' + ' Take care of your good behaviors' + '\n' + ' and appreciate your days of life' + '\n' + '\n' + '\n' + '\n' + '\n' + ' telegram: @strixdaybot ' img = requests.get("http://behkaroma.ir/p.jpg") ff = requests.get("http://behkaroma.ir/p.ttf") font1 = ImageFont.truetype(StringIO(ff.content), 30) font2 = ImageFont.truetype(StringIO(ff.content), 40) font3 = ImageFont.truetype(StringIO(ff.content), 50) image = Image.open(StringIO(img.content)) draw = ImageDraw.Draw(image) draw.text((30, 150), zzz, (0, 0, 0), font=font3) draw.text((20, 400), zz, (255, 255, 255), font=font1) bio = BytesIO() bio.name = 'image.jpeg' image.save(bio, 'JPEG') bio.seek(0) if (kk == 0): bot.sendPhoto(chat_id, photo=bio) bot.sendMessage(chat_id, text=ss)