class CalverterMixin: conversion_method = None ISLAMIC_HOLIDAYS = () def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.calverter = Calverter() if self.conversion_method is None: raise NotImplementedError def converted(self, year): conversion_method = getattr(self.calverter, 'jd_to_%s' % self.conversion_method) current = date(year, 1, 1) days = [] while current.year == year: julian_day = self.calverter.gregorian_to_jd( current.year, current.month, current.day) days.append(conversion_method(julian_day)) current = current + timedelta(days=1) return days def calverted_years(self, year): converted = self.converted(year) generator = (y for y, m, d in converted) return sorted(list(set(generator))) def get_islamic_holidays(self): return self.ISLAMIC_HOLIDAYS def get_delta_islamic_holidays(self, year): """ Return the delta to add/substract according to the year or customs. By default, to return None or timedelta(days=0) """ return None def get_variable_days(self, year): warnings.warn('Please take note that, due to arbitrary decisions, ' 'this Islamic calendar computation may be wrong.') days = super().get_variable_days(year) years = self.calverted_years(year) conversion_method = getattr(self.calverter, '%s_to_jd' % self.conversion_method) for month, day, label in self.get_islamic_holidays(): for y in years: jd = conversion_method(y, month, day) g_year, g_month, g_day = self.calverter.jd_to_gregorian(jd) holiday = date(g_year, g_month, g_day) # Only add a delta if necessary delta = self.get_delta_islamic_holidays(year) if delta: holiday += delta if holiday.year == year: days.append((holiday, label)) return days
def jalali_to_gregorian(date_str): if date_str == "": return "" cal = Calverter() year, month, day = date_str.split("/") jd = cal.jalali_to_jd(int(year), int(month), int(day)) gre = cal.jd_to_gregorian(jd) return date(gre[0], gre[1], gre[2])
def jalali_mysql_date(value): #gd = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S') gd = value cal = Calverter() jd = cal.gregorian_to_jd(gd.year, gd.month, gd.day) d = cal.jd_to_jalali(jd) d = "%s/%s/%s" % (d[0], d[1], d[2]) return d
def gregorian_to_jalali(date): if isinstance(date, str): date = parser(date) if isinstance(date, unicode): date = parser(date) cal = Calverter() jd = cal.gregorian_to_jd(date.year, date.month, date.day) jalali = cal.jd_to_jalali(jd) return "%s/%s/%s" % (jalali[0], jalali[1], jalali[2])
def hijri_date(): gr_date = time.strptime(time.ctime()) cal = Calverter() jd_today = cal.gregorian_to_jd(gr_date.tm_year, gr_date.tm_mon, gr_date.tm_mday) jal_today = list(cal.jd_to_jalali(jd_today)) list.append(jal_today, wday_to_hday(gr_date.tm_wday)) print( str(gr_date.tm_hour) + ":" + str(gr_date.tm_min) + " " + str(jal_today[3]) + " " + str(jal_today[1]) + "/" + str(jal_today[2]))
def format_date(date, lang): if lang == "fa": cal = Calverter() jd = cal.gregorian_to_jd(date.year, date.month, date.day) wday = cal.jwday(jd) jalali = cal.jd_to_jalali(jd) result = "%s، %d %s %d" % (DAYS_NAMES[wday], jalali[2], MONTHS_NAMES[jalali[1] - 1], jalali[0]) return to_persian_digits(result) return date
def gregorian_date(zero_date): cal = Calverter() iso_day = cal.gregorian_to_jd( int(zero_date[0]), int(zero_date[1]), int(zero_date[2]) ) end_date = cal.jd_to_jalali(iso_day) end_date = list(end_date) week_day_num = cal.jwday(iso_day) end_date.append(week_day_num) print(end_date[0], end_date[1], end_date[2], end_date[3])
class CalverterMixin(Calendar): conversion_method = None ISLAMIC_HOLIDAYS = () def __init__(self, *args, **kwargs): super(CalverterMixin, self).__init__(*args, **kwargs) self.calverter = Calverter() if self.conversion_method is None: raise NotImplementedError def converted(self, year): conversion_method = getattr( self.calverter, 'jd_to_%s' % self.conversion_method) current = date(year, 1, 1) days = [] while current.year == year: julian_day = self.calverter.gregorian_to_jd( current.year, current.month, current.day) days.append(conversion_method(julian_day)) current = current + timedelta(days=1) return days def calverted_years(self, year): converted = self.converted(year) generator = (y for y, m, d in converted) return sorted(list(set(generator))) def get_islamic_holidays(self): return self.ISLAMIC_HOLIDAYS def get_variable_days(self, year): warnings.warn('Please take note that, due to arbitrary decisions, ' 'this Islamic calendar computation may be wrong.') days = super(CalverterMixin, self).get_variable_days(year) years = self.calverted_years(year) conversion_method = getattr( self.calverter, '%s_to_jd' % self.conversion_method) for month, day, label in self.get_islamic_holidays(): for y in years: jd = conversion_method(y, month, day) g_year, g_month, g_day = self.calverter.jd_to_gregorian(jd) if g_year == year: holiday = date(g_year, g_month, g_day) days.append((holiday, label)) return days
class CalverterMixin(Calendar): conversion_method = None ISLAMIC_HOLIDAYS = () def __init__(self, *args, **kwargs): super(CalverterMixin, self).__init__(*args, **kwargs) self.calverter = Calverter() if self.conversion_method is None: raise NotImplementedError def converted(self, year): conversion_method = getattr( self.calverter, 'jd_to_%s' % self.conversion_method) current = date(year, 1, 1) days = [] while current.year == year: julian_day = self.calverter.gregorian_to_jd( current.year, current.month, current.day) days.append(conversion_method(julian_day)) current = current + timedelta(days=1) return days def calverted_years(self, year): converted = self.converted(year) generator = (y for y, m, d in converted) return sorted(list(set(generator))) def get_variable_days(self, year): days = super(CalverterMixin, self).get_variable_days(year) years = self.calverted_years(year) conversion_method = getattr( self.calverter, '%s_to_jd' % self.conversion_method) for month, day, label in self.ISLAMIC_HOLIDAYS: for y in years: jd = conversion_method(y, month, day) g_year, g_month, g_day = self.calverter.jd_to_gregorian(jd) if g_year == year: days.append((date(g_year, g_month, g_day), label)) return days
def hijri_date(): gr_date = time.strptime(time.ctime()) cal = Calverter() jd_today = cal.gregorian_to_jd( gr_date.tm_year, gr_date.tm_mon, gr_date.tm_mday ) jal_today = list(cal.jd_to_jalali(jd_today)) list.append(jal_today, wday_to_hday(gr_date.tm_wday)) print( str(gr_date.tm_hour) + ":" + str(gr_date.tm_min) + " " + str(jal_today[3]) + " " + str(jal_today[1]) + "/" + str(jal_today[2]) )
def today_event(jalali_date): if jalali_date == "": return "" cal = Calverter() year, month, day = jalali_date.split("/") jd = cal.jalali_to_jd(int(year), int(month), int(day)) is_holiday = False events = [] gr = cal.jd_to_gregorian(jd) if (gr[1], gr[2]) in gregorian_event: event = gregorian_event[(gr[1], gr[2])] events.append(event[0]) if event[1]: is_holiday = True hj = cal.jd_to_islamic(jd) if (hj[1], hj[2]) in hijri_event: event = hijri_event[(hj[1], hj[2])] events.append(event[0]) if event[1]: is_holiday = True pr = cal.jd_to_jalali(jd) if (pr[1], pr[2]) in persians_famous: event = persians_famous[(pr[1], pr[2])] events.append(event[0]) if event[1]: is_holiday = True if (pr[1], pr[2]) in persian_event: event = persian_event[(pr[1], pr[2])] events.append(event[0]) if event[1]: is_holiday = True return events, is_holiday
def __init__(self, *args, **kwargs): super(CalverterMixin, self).__init__(*args, **kwargs) self.calverter = Calverter() if self.conversion_method is None: raise NotImplementedError
#File Name: date.py (Digitas date calculator engine file) import datetime from calverter import Calverter #Get current Gregorian date. now = datetime.datetime.now() greg_year = now.year greg_month = now.month greg_day = now.day greg_weekday_number = now.weekday() #Convert Gregorian date to Julian date. cal = Calverter() julian = cal.gregorian_to_jd(greg_year,greg_month,greg_day) #Convert Julian date to Hijri date. hijri = cal.jd_to_islamic(julian) #Organize Hijri date to [greg_weekday_number, month_name, day_number, year] format. hijri_year = hijri[0] hijri_month = hijri[1] hijri_day_number = hijri[2] hijri_date = [greg_weekday_number, hijri_month, hijri_day_number, hijri_year] #Convert greg_weekday_number (which are in number forms) into weekday name.
#!/usr/bin/python # -*- coding: utf-8 -*- import datetime now = datetime.datetime.now() year = now.year month = now.month day = now.day weekday_number = now.weekday() #Convert current date to Julian date. from calverter import Calverter cal = Calverter() julian = cal.gregorian_to_jd(year, month, day) #Convert Julian date to Islamic date. hijri = cal.jd_to_islamic(julian) #Convert Islamic date to weekday_name, day_number, month_name, year format. hijri_year = hijri[0] hijri_month = hijri[1] hijri_day_number = hijri[2] hijri_date = [weekday_number, hijri_day_number, hijri_month, hijri_year] #Convert hijri_month (which are in number forms) into month names. # Just define 12 months names: islamic_months = [ "محرّم", "صفر", "ربيع_اﻷوّل", "ربيع_الثّاني", "جمادى_اﻷولى", "جمادى_الثّانية", "رجب", "شعبان", "رمضان", "شوّال", "ذو_القعدة", "ذوالحجّة" ]
# print pta sys.path.append(pta) from guess_language import guessLanguage as lg from flib.models import Project, Review, Task, User, Report, r, fdb from flib.models.db import session_factory from sqlalchemy import or_, and_ import arrow from datetime import date, timedelta from flib.tasks import send_envelope # send emails from mako.template import Template import os from collections import defaultdict from sqlalchemy import desc, asc from calverter import Calverter cal = Calverter() import misaka import ujson templates_folder = os.path.join(os.path.dirname(__file__), '../templates') quotes_file = open(os.path.join(templates_folder, 'quotes.json')) quotes = ujson.load(quotes_file) from random import choice cc = [] bcc = [] '''Dates''' now = arrow.utcnow()