Skip to content

Commit

Permalink
add an option to adjust hijri calendar by -/+ 1 day
Browse files Browse the repository at this point in the history
  • Loading branch information
AXeL-dev committed Dec 5, 2018
1 parent af353f3 commit 4f7f462
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 22 deletions.
3 changes: 2 additions & 1 deletion hijra.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@
__p_const=191
__q_const=360
__a_const=48
__hijri_epoch=227015; # = Julian 0622-7-16 = gregorian 0759-6-11 (I think it should be 622, 7, 19)
__hijri_epoch=227015 # = Julian 0622-7-16 = gregorian 0759-6-11 (I think it should be 622, 7, 19)
# TODO: Why does the the hijri_epoch 227015 does not give the expected value when converted to gregorian

def get_consts():
"""Return a tuple of the 3 constants (p,q,a) used by this algothim, for debuging"""
return (__p_const, __q_const, __a_const)

def get_epoch():
"""Return Hijri epoch, number of days since gregorian epoch, (should be Julian 0622-7-16 (ie. 227015 days)"""
return __hijri_epoch
Expand Down
5 changes: 4 additions & 1 deletion hijrical.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

class HijriCal:
"""a Class that provide a high level Islamic Hijri calendar API"""
def __init__(self):
def __init__(self, adjustment=0):
self.adjustment = adjustment
"""Create HijriCal Object"""
self.__md = [[""]*7,[""]*7,[""]*7,[""]*7,[""]*7,[""]*7] # 7 days on at most 6 weeks
self.__g_md = [[""]*7,[""]*7,[""]*7,[""]*7,[""]*7,[""]*7] # 7 days on at most 6 weeks
Expand All @@ -52,6 +53,7 @@ def refresh_today(self):
else:
yy, mm, dd = localtime()[:3]
self.g_today= (yy, mm, dd)
dd += int(self.adjustment) # adjust hijri calendar
Y, M, D = gregorian_to_hijri(yy, mm, dd)
wd = hijri_day_of_week (Y, M, D)
self.today= (Y, M, D, wd)
Expand All @@ -60,6 +62,7 @@ def refresh_today(self):
def goto_gregorian_day(self,yy, mm, dd):
"""Jump to some Hijri day"""
try:
dd += int(self.adjustment) # adjust hijri calendar
Y, M, D = gregorian_to_hijri(yy, mm, dd)
self.goto_hijri_day(Y, M, D)
except:
Expand Down
18 changes: 17 additions & 1 deletion options.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self):
self._normaladhan = cparse.get('DEFAULT', 'normal-adhan')
self._audionotifications = cparse.get('DEFAULT', 'audio-notifications')
self._daylightsavingtime = cparse.get('DEFAULT', 'daylight-saving-time')
self._hijricaladjustment = cparse.get('DEFAULT', 'hijrical-adjustment')

except configparser.NoOptionError:
print ("DEBUG: No configration file using default settings")
Expand All @@ -57,6 +58,7 @@ def __init__(self):
self._normaladhan = (self.get_normal_adhans())[0]
self._audionotifications = '1'
self._daylightsavingtime = '1'
self._hijricaladjustment = '0'
self.save_options()

except ValueError:
Expand All @@ -77,6 +79,7 @@ def __init__(self):
self._normaladhan = (self.get_normal_adhans())[0]
self._audionotifications = '1'
self._daylightsavingtime = '1'
self._hijricaladjustment = '0'
self.save_options()

## Functions with lists for the Buttons
Expand Down Expand Up @@ -249,6 +252,16 @@ def notification_time(self, data):
#print ("DEBUG: setting notification time settings @", (str(datetime.datetime.now())))
self._notif = str(data)

@property
def hijrical_adjustment(self):
#print ("DEBUG: getting hijri cal adjustment settings @", (str(datetime.datetime.now())))
return float(self._hijricaladjustment)

@hijrical_adjustment.setter
def hijrical_adjustment(self, data):
#print ("DEBUG: setting hijri cal adjustment settings @", (str(datetime.datetime.now())))
self._hijricaladjustment = str(data)

@property
def iconlabel_num(self):
return self._iconlabel
Expand Down Expand Up @@ -378,13 +391,16 @@ def save_options(self):
# Should the application use daylight saving time
daylight-saving-time = %s
# Adjust Hijri Calendar
hijrical-adjustment = %s
# Paths to the audio files
fajr-adhan = %s
normal-adhan = %s
''' % (self.city, self.country, self.latitude, self.longitude, self.timezone, \
self.calculation_method_name, self.madhab_name, self.clock_format, \
self.notification_time, self.iconlabel_num, self.audio_notifications_num, self.start_minimized_num, \
self.daylight_saving_time_num, self.fajr_adhan, self.normal_adhan)
self.daylight_saving_time_num, self.hijrical_adjustment, self.fajr_adhan, self.normal_adhan)
config.write(Text)
config.close()
20 changes: 17 additions & 3 deletions silaty.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def set_layout(self):
self.sidebar.add_to_stack(self.qiblacompass, 'qibla')

# Set up calendar panel
cal = SilatyCal()
self.sidebar.add_to_stack(cal, "calendar")
self.calendar = SilatyCal()
self.sidebar.add_to_stack(self.calendar, "calendar")

# Set up the options pane
self.set_options()
Expand Down Expand Up @@ -173,6 +173,15 @@ def set_options(self):
self.cfmenu.connect("changed", self.on_entered_clock_format)
settings.add_setting(self.cfmenu, cflabel)

# Adjust Hijri Calendar
defaultvalue = self.prayertimes.options.hijrical_adjustment
hijrilabel = Gtk.Label('Adjust Hijri Calendar:', halign=Gtk.Align.START)
hijriadj = Gtk.Adjustment(value=0, lower=-1, upper=1, step_incr=1, page_incr=1, page_size=0)
self.hijrivalue = Gtk.SpinButton(adjustment=hijriadj, halign=Gtk.Align.FILL)
self.hijrivalue.set_value(float(defaultvalue))
self.hijrivalue.connect("value-changed",self.on_entered_hijrical_adjustment)
settings.add_setting(self.hijrivalue, hijrilabel)

settings.add_category("Notifications")

# Show Icon with label
Expand Down Expand Up @@ -243,7 +252,7 @@ def set_options(self):

settings.add_category("Jurisprudence")

# Cal Method
# Calculation Method
defaultmethod = self.prayertimes.options.calculation_method_name
methods = self.prayertimes.options.get_cal_methods()
calmethodlabel = Gtk.Label('Calculation Method:', halign=Gtk.Align.START)
Expand Down Expand Up @@ -454,6 +463,11 @@ def on_entered_timezone(self, widget):
def on_entered_notification_time(self, widget):
self.prayertimes.options.notification_time = widget.get_value()

def on_entered_hijrical_adjustment(self, widget):
self.prayertimes.options.hijrical_adjustment = widget.get_value()
self.calendar.cal.hijrical.adjustment = widget.get_value()
self.calendar.cal.refresh()

def on_entered_clock_format(self, widget):
self.prayertimes.options.clock_format = widget.get_active_text()
self.update_prayers(False)
Expand Down
94 changes: 78 additions & 16 deletions silatycal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from gi.repository import GObject, GLib, Gtk, Gdk, GdkPixbuf
from datetime import date, timedelta
from hijrical import *
from options import *
import datetime
import os

Expand All @@ -27,7 +28,9 @@ def __init__(self):
gtitlelabel = Gtk.Label(label=(now_wd+", "+g_date))
gtitlelabel.props.halign = Gtk.Align.START

calc = HijriCal()
self.options = Options()

calc = HijriCal(self.options.hijrical_adjustment)
h_months = ['Muharram', 'Safar', 'Rabi al Awwal', 'Rabi al Akhira', 'Jumada al Ula', 'Jumada al Akhira', 'Rajab', "Sha'ban", 'Ramadan', 'Shawwal', "Dhu al Qa'da", 'Dhu al Hijja']
h_year, h_month, h_day, h_week_day = calc.today
h_date = '%i %s %i' % ( h_day, h_months[int(h_month-1)], h_year)
Expand All @@ -52,7 +55,7 @@ def __init__(self):
topbox.pack_end(box, False, False, 0)

# Set up the date switcher
self.cal = Cal()
self.cal = Cal(self)

#bottombox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, halign=Gtk.Align.FILL, margin=24)
#opmaya = Gtk.Button(label="Open Maya")
Expand Down Expand Up @@ -80,13 +83,15 @@ def on_entered_hijri(self, widget, event):
self.cal.caltable.get_child_at(column,row).state = CalendarState.Gregorian

class Cal(Gtk.Box):
def __init__(self):
def __init__(self, parent):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL, spacing=12)
self.dateswitcher = Gtk.Grid(row_homogeneous=True)
self.dateswitcher.set_halign(Gtk.Align.CENTER)

self.parent = parent

self._refdate = datetime.datetime.today()
self.hijcal = HijriCal()
self.hijrical = HijriCal(self.parent.options.hijrical_adjustment)
self.weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
self.gmonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
self.hmonths = ['Muharram', 'Safar', 'Rabi al Awwal', 'Rabi al Akhira', 'Jumada al Ula', 'Jumada al Akhira', 'Rajab', "Sha'ban", 'Ramadhan', 'Shawwal', "Dhu al Qa'da", 'Dhu al Hijja']
Expand Down Expand Up @@ -195,7 +200,7 @@ def __init__(self):
gday = (self.refdate).strftime("%d")
gmonth = (self.refdate).strftime("%B")
gyear = (self.refdate).strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))

self.gmonthstack.set_visible_child_name(self.refdate.strftime("%B"))
self.hmonthstack.set_visible_child_name(self.hmonths[int(hmonth-1)])
Expand Down Expand Up @@ -258,9 +263,7 @@ def __init__(self):
self.gmtoday = self.gmonths.index(_gmonth)
self.gytoday = int(_gyear)

hijrical = HijriCal()

self.hytoday, self.hmtoday, self.hdtoday = hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)
self.hytoday, self.hmtoday, self.hdtoday = self.hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)

calendarindex = -(wtoday+(((6-wtoday)+self.gdtoday)//7)*7)

Expand All @@ -277,7 +280,7 @@ def __init__(self):
cgmday = self.gmonths.index((datetime.datetime.now() + timedelta(days=calendarindex)).strftime("%B"))
cgyday = int((datetime.datetime.now() + timedelta(days=calendarindex)).strftime("%Y"))

chyday, chmday, chdday = hijrical.goto_gregorian_day(cgyday, (cgmday+1), cgdday)
chyday, chmday, chdday = self.hijrical.goto_gregorian_day(cgyday, (cgmday+1), cgdday)

if (cgmday != self.gmtoday):
gcolor = CalendarColor.DGrey
Expand Down Expand Up @@ -327,20 +330,20 @@ def left_arrow_pressed(self, widget, event):
gday = self.refdate.strftime("%d")
gmonth = self.refdate.strftime("%B")
gyear = self.refdate.strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
else:
gday = self.refdate.strftime("%d")
gmonth = self.refdate.strftime("%B")
gyear = self.refdate.strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
ndays = hijri_month_days(hyear, hmonth-2)
self.refdate = self.refdate-timedelta(days=ndays)
gday = self.refdate.strftime("%d")
gmonth = self.refdate.strftime("%B")
gyear = self.refdate.strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
self.shift_days_of_table(gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, CalendarDirection.RIGHT)
Expand All @@ -356,20 +359,20 @@ def right_arrow_pressed(self, widget, event):
gday = (self.refdate).strftime("%d")
gmonth = (self.refdate).strftime("%B")
gyear = (self.refdate).strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
else:
gday = (self.refdate).strftime("%d")
gmonth = (self.refdate).strftime("%B")
gyear = (self.refdate).strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
ndays = timedelta(days=hijri_month_days(hyear, hmonth))
self.refdate = self.refdate+ndays
gday = self.refdate.strftime("%d")
gmonth = self.refdate.strftime("%B")
gyear = self.refdate.strftime("%Y")
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
self.shift_days_of_table(gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, CalendarDirection.LEFT)
Expand All @@ -384,13 +387,72 @@ def set_image_from_file(self, iconpath):
icon = Gtk.Image.new_from_stock(Gtk.STOCK_MISSING_IMAGE, 22)
return icon

def refresh(self):
# update hijri date
self.hytoday, self.hmtoday, self.hdtoday = self.hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)
# update hijri title label
now_wd = datetime.datetime.now().strftime("%A")
h_date = '%i %s %i' % ( self.hdtoday, self.hmonths[int(self.hmtoday-1)], self.hytoday)
self.parent.titlestack.get_child_by_name("Hijri").set_label(now_wd+", "+h_date)
# update calendar
gday = (self.refdate).strftime("%d")
gmonth = (self.refdate).strftime("%B")
gyear = (self.refdate).strftime("%Y")
if self.state == CalendarState.Gregorian:
# Get the size of this month
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
else:
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
refwd = self.weekdays.index(self.refdate.strftime("%A"))
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
for row in range(0, 6):
for column in range(0, 7):
newgday = int((self.refdate + timedelta(days=(calendarindex))).strftime("%d"))
newgmonth = self.gmonths.index((self.refdate + timedelta(days=(calendarindex))).strftime("%B"))
newgyear = int((self.refdate + timedelta(days=(calendarindex))).strftime("%Y"))
newhyear, newhmonth, newhday = self.hijrical.goto_gregorian_day(newgyear, newgmonth+1, newgday)

self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next = newgday
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next = newhday

if (column == 0) or (column == 6):
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.LGrey
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.LGrey
else:
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.White
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.White

if newgmonth != self.gmonths.index(self.refdate.strftime("%B")):
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.DGrey

if newhmonth != hmonth:
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.DGrey

if (newgday == self.gdtoday and newgmonth == self.gmtoday and newgyear == self.gytoday):
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.Blue

if (newhday == self.hdtoday and newhmonth == self.hmtoday and newhyear == self.hytoday):
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.Blue

newgbackground = self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background
newhbackground = self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background

self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day = newgday
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_background = newgbackground
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day = newhday
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_background = newhbackground

calendarindex += 1

def shift_days_of_table(self, gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, direction):
for row in range(0, 6):
for column in range(0, 7):
newgday = int((self.refdate + timedelta(days=(calendarindex))).strftime("%d"))
newgmonth = self.gmonths.index((self.refdate + timedelta(days=(calendarindex))).strftime("%B"))
newgyear = int((self.refdate + timedelta(days=(calendarindex))).strftime("%Y"))
newhyear, newhmonth, newhday = self.hijcal.goto_gregorian_day(newgyear, newgmonth+1, newgday)
newhyear, newhmonth, newhday = self.hijrical.goto_gregorian_day(newgyear, newgmonth+1, newgday)

self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next = newgday
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next = newhday
Expand Down

0 comments on commit 4f7f462

Please sign in to comment.