def test_base(self): self.assertEqual(JalaliDateTime(1367, 2, 14, 14, 0, 0, 0), JalaliDateTime.to_jalali(datetime(1988, 5, 4, 14, 0, 0, 0))) self.assertEqual(JalaliDateTime(1369, 7, 1, 14, 14, 1, 1111), JalaliDateTime(datetime(1990, 9, 23, 14, 14, 1, 1111))) self.assertEqual(JalaliDateTime(1369, 7, 1, 14, 14, 1, 9111), JalaliDateTime(JalaliDateTime(1369, 7, 1, 14, 14, 1, 9111))) g = JalaliDateTime.now() self.assertEqual(g.time(), _time(g.hour, g.minute, g.second, g.microsecond)) g = g.replace(tzinfo=pytz.timezone("America/Los_Angeles")) self.assertEqual(g.timetz(), _time(g.hour, g.minute, g.second, g.microsecond, pytz.timezone("America/Los_Angeles"))) self.assertEqual(JalaliDateTime.fromtimestamp(578723400, pytz.utc), JalaliDateTime(1367, 2, 14, 4, 30, 0, 0, pytz.utc)) self.assertEqual(JalaliDateTime.utcfromtimestamp(578723400), JalaliDateTime(1367, 2, 14, 4, 30, 0, 0)) try: JalaliDateTime._check_time_fields(20, 1, 61, 1000) except ValueError: assert True else: assert False try: JalaliDateTime._check_time_fields("20", 1, 61, 1000) except TypeError: assert True else: assert False
def from_gregorian(self,greg_date_time,add_time_zone=True): if not add_time_zone: return JalaliDateTime.to_jalali(greg_date_time).strftime("%Y/%m/%d %H:%M:%S") if SERVER_ON_PARS or SERVER_ON_HEROKU: if self.is_in_first_half_shamsi_year(): hours=4 else: hours=3 minutes=30 else: if self.is_in_first_half_shamsi_year(): hours=4 else: hours=3 minutes=30 return JalaliDateTime.to_jalali(greg_date_time+datetime.timedelta(hours=hours,minutes=minutes)).strftime("%Y/%m/%d %H:%M:%S")
def test_gregorian_to_shamsi(self): self.assertEqual( JalaliDateTime(datetime(1990, 9, 23, 14, 14, 1, 1111)), JalaliDateTime(1369, 7, 1, 14, 14, 1, 1111), ) self.assertEqual( JalaliDateTime.to_jalali(datetime(1988, 5, 4, 14, 0, 0, 0)), JalaliDateTime(1367, 2, 14, 14, 0, 0, 0), )
def startups_api(request): """an api to show startups that have credit in 100startups main page Arguments: request {REQUEST} -- for HTTP Response Returns: JSONRESPONSE -- returns serialized informaion about startup """ protocol = urlsplit(request.build_absolute_uri(None)).scheme current_site = get_current_site(request) result = [] startup = StartUp.objects.filter(credit__isnull=False) site = ''.join([str(protocol), '://', str(current_site)]) for db in startup: date = JalaliDateTime.to_jalali( datetime.datetime(db.created_date.year, db.created_date.month, db.created_date.day, db.created_date.hour, db.created_date.minute, db.created_date.second, 0)) result.append({ 'id': db.id, 'logo': site + str(db.image.url), 'name': db.title, 'site': db.site, 'credit': db.credit, 'explaine': db.explain_startup, 'created_date': str(date), 'leader': { 'first_name': db.rahbar_asli.first_name, 'last_name': db.rahbar_asli.last_name, 'avatar': site + str(db.rahbar_asli.avatar.url) }, 'investor': list( db.investor.annotate(avatar=F('user__avatar'), first_name=F('user__first_name'), last_name=F('user__last_name')).values( 'avatar', 'first_name', 'last_name')), 'team': list( db.teammember.annotate(first_name=F('t_first_name'), last_name=F('t_last_name'), avatar=F('t_avatar'), role=F('t_role_in_startup')).values( 'first_name', 'last_name', 'avatar', 'role')) }) return JsonResponse(result, safe=False)
def jdatetime(value, arg=""): try: if str(value) == "now": if arg == "": arg = "%Y-%m-%d %H:%M:%S" resultdate = JalaliDateTime.now() elif str(value) == "time": if arg == "": arg = "%H:%M:%S" resultdate = JalaliDateTime.now() elif str(value) == "date": if arg == "": arg = "%Y-%m-%d" resultdate = JalaliDateTime.now() else: if isinstance(value, datetime.date): resultdate = JalaliDateTime.to_jalali(value) else: year, month, day = map(int, value[:10].split('-')) hour = minute = second = millisecond = 0 if value.find(' ') == 10: date, time = value.split(' ') time = time.split(':') if len(time) == 1: hour = int(time[0]) elif len(time) == 2: hour = int(time[0]) minute = int(time[1]) elif len(time) == 3: hour = int(time[0]) minute = int(time[1]) time = time[2].split('.') second = int(time[0]) if len(time) > 1: millisecond = int(time[1]) resultdate = JalaliDateTime.to_jalali( datetime.datetime(year, month, day, hour, minute, second, millisecond)) if arg: return resultdate.strftime(arg) else: return resultdate except Exception as err: return "jdatetime error:%s" % err
def order_list(): database = current_app.config['DATABASE_NAME'] client = MongoClient('localhost', 27017) db = client[database] res = list(db.basket.find()) for i in res: i["_id"] = str(i["_id"]) i["time_record"] = digits.en_to_fa( JalaliDate((JalaliDateTime.to_jalali(i["time_record"]))).strftime("%Y/%m/%d")) # i["total_costs"] = digits.en_to_fa(str(i["total_costs"])) i["total_costs"] = f"{i['total_costs']:,}" res = list(db.basket.find()) for i in res: i["_id"] = str(i["_id"]) i["time_record"] = digits.en_to_fa( JalaliDate((JalaliDateTime.to_jalali(i["time_record"]))).strftime("%Y/%m/%d")) # i["total_costs"] = digits.en_to_fa(str(i["total_costs"])) i["total_costs"] = f"{i['total_costs']:,}" i["time_give"] = digits.en_to_fa( JalaliDate((JalaliDateTime.to_jalali(i["time_give"]))).strftime("%Y/%m/%d")) return jsonify(list(res))
def jsort (self): worldtime = self.ti w = str(worldtime).split() #date date = w[0].split('-') #time time = w[1].split(':') jsoort = JalaliDateTime.to_jalali(datetime.datetime( int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2])) ).strftime("%Y%m%d%H%M%S") return jsoort
def jconvert (self): worldtime = self.ti w = str(worldtime).split() #date date = w[0].split('-') #time time = w[1].split(':') convert = JalaliDateTime.to_jalali(datetime.datetime( int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2])) ).strftime(self.jformat) return convert