Skip to content

Commit 4f7f462

Browse files
committed
add an option to adjust hijri calendar by -/+ 1 day
1 parent af353f3 commit 4f7f462

File tree

5 files changed

+118
-22
lines changed

5 files changed

+118
-22
lines changed

Diff for: hijra.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@
5151
__p_const=191
5252
__q_const=360
5353
__a_const=48
54-
__hijri_epoch=227015; # = Julian 0622-7-16 = gregorian 0759-6-11 (I think it should be 622, 7, 19)
54+
__hijri_epoch=227015 # = Julian 0622-7-16 = gregorian 0759-6-11 (I think it should be 622, 7, 19)
5555
# TODO: Why does the the hijri_epoch 227015 does not give the expected value when converted to gregorian
5656

5757
def get_consts():
5858
"""Return a tuple of the 3 constants (p,q,a) used by this algothim, for debuging"""
5959
return (__p_const, __q_const, __a_const)
60+
6061
def get_epoch():
6162
"""Return Hijri epoch, number of days since gregorian epoch, (should be Julian 0622-7-16 (ie. 227015 days)"""
6263
return __hijri_epoch

Diff for: hijrical.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929

3030
class HijriCal:
3131
"""a Class that provide a high level Islamic Hijri calendar API"""
32-
def __init__(self):
32+
def __init__(self, adjustment=0):
33+
self.adjustment = adjustment
3334
"""Create HijriCal Object"""
3435
self.__md = [[""]*7,[""]*7,[""]*7,[""]*7,[""]*7,[""]*7] # 7 days on at most 6 weeks
3536
self.__g_md = [[""]*7,[""]*7,[""]*7,[""]*7,[""]*7,[""]*7] # 7 days on at most 6 weeks
@@ -52,6 +53,7 @@ def refresh_today(self):
5253
else:
5354
yy, mm, dd = localtime()[:3]
5455
self.g_today= (yy, mm, dd)
56+
dd += int(self.adjustment) # adjust hijri calendar
5557
Y, M, D = gregorian_to_hijri(yy, mm, dd)
5658
wd = hijri_day_of_week (Y, M, D)
5759
self.today= (Y, M, D, wd)
@@ -60,6 +62,7 @@ def refresh_today(self):
6062
def goto_gregorian_day(self,yy, mm, dd):
6163
"""Jump to some Hijri day"""
6264
try:
65+
dd += int(self.adjustment) # adjust hijri calendar
6366
Y, M, D = gregorian_to_hijri(yy, mm, dd)
6467
self.goto_hijri_day(Y, M, D)
6568
except:

Diff for: options.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(self):
3939
self._normaladhan = cparse.get('DEFAULT', 'normal-adhan')
4040
self._audionotifications = cparse.get('DEFAULT', 'audio-notifications')
4141
self._daylightsavingtime = cparse.get('DEFAULT', 'daylight-saving-time')
42+
self._hijricaladjustment = cparse.get('DEFAULT', 'hijrical-adjustment')
4243

4344
except configparser.NoOptionError:
4445
print ("DEBUG: No configration file using default settings")
@@ -57,6 +58,7 @@ def __init__(self):
5758
self._normaladhan = (self.get_normal_adhans())[0]
5859
self._audionotifications = '1'
5960
self._daylightsavingtime = '1'
61+
self._hijricaladjustment = '0'
6062
self.save_options()
6163

6264
except ValueError:
@@ -77,6 +79,7 @@ def __init__(self):
7779
self._normaladhan = (self.get_normal_adhans())[0]
7880
self._audionotifications = '1'
7981
self._daylightsavingtime = '1'
82+
self._hijricaladjustment = '0'
8083
self.save_options()
8184

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

255+
@property
256+
def hijrical_adjustment(self):
257+
#print ("DEBUG: getting hijri cal adjustment settings @", (str(datetime.datetime.now())))
258+
return float(self._hijricaladjustment)
259+
260+
@hijrical_adjustment.setter
261+
def hijrical_adjustment(self, data):
262+
#print ("DEBUG: setting hijri cal adjustment settings @", (str(datetime.datetime.now())))
263+
self._hijricaladjustment = str(data)
264+
252265
@property
253266
def iconlabel_num(self):
254267
return self._iconlabel
@@ -378,13 +391,16 @@ def save_options(self):
378391
# Should the application use daylight saving time
379392
daylight-saving-time = %s
380393
394+
# Adjust Hijri Calendar
395+
hijrical-adjustment = %s
396+
381397
# Paths to the audio files
382398
fajr-adhan = %s
383399
normal-adhan = %s
384400
385401
''' % (self.city, self.country, self.latitude, self.longitude, self.timezone, \
386402
self.calculation_method_name, self.madhab_name, self.clock_format, \
387403
self.notification_time, self.iconlabel_num, self.audio_notifications_num, self.start_minimized_num, \
388-
self.daylight_saving_time_num, self.fajr_adhan, self.normal_adhan)
404+
self.daylight_saving_time_num, self.hijrical_adjustment, self.fajr_adhan, self.normal_adhan)
389405
config.write(Text)
390406
config.close()

Diff for: silaty.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def set_layout(self):
8484
self.sidebar.add_to_stack(self.qiblacompass, 'qibla')
8585

8686
# Set up calendar panel
87-
cal = SilatyCal()
88-
self.sidebar.add_to_stack(cal, "calendar")
87+
self.calendar = SilatyCal()
88+
self.sidebar.add_to_stack(self.calendar, "calendar")
8989

9090
# Set up the options pane
9191
self.set_options()
@@ -173,6 +173,15 @@ def set_options(self):
173173
self.cfmenu.connect("changed", self.on_entered_clock_format)
174174
settings.add_setting(self.cfmenu, cflabel)
175175

176+
# Adjust Hijri Calendar
177+
defaultvalue = self.prayertimes.options.hijrical_adjustment
178+
hijrilabel = Gtk.Label('Adjust Hijri Calendar:', halign=Gtk.Align.START)
179+
hijriadj = Gtk.Adjustment(value=0, lower=-1, upper=1, step_incr=1, page_incr=1, page_size=0)
180+
self.hijrivalue = Gtk.SpinButton(adjustment=hijriadj, halign=Gtk.Align.FILL)
181+
self.hijrivalue.set_value(float(defaultvalue))
182+
self.hijrivalue.connect("value-changed",self.on_entered_hijrical_adjustment)
183+
settings.add_setting(self.hijrivalue, hijrilabel)
184+
176185
settings.add_category("Notifications")
177186

178187
# Show Icon with label
@@ -243,7 +252,7 @@ def set_options(self):
243252

244253
settings.add_category("Jurisprudence")
245254

246-
# Cal Method
255+
# Calculation Method
247256
defaultmethod = self.prayertimes.options.calculation_method_name
248257
methods = self.prayertimes.options.get_cal_methods()
249258
calmethodlabel = Gtk.Label('Calculation Method:', halign=Gtk.Align.START)
@@ -454,6 +463,11 @@ def on_entered_timezone(self, widget):
454463
def on_entered_notification_time(self, widget):
455464
self.prayertimes.options.notification_time = widget.get_value()
456465

466+
def on_entered_hijrical_adjustment(self, widget):
467+
self.prayertimes.options.hijrical_adjustment = widget.get_value()
468+
self.calendar.cal.hijrical.adjustment = widget.get_value()
469+
self.calendar.cal.refresh()
470+
457471
def on_entered_clock_format(self, widget):
458472
self.prayertimes.options.clock_format = widget.get_active_text()
459473
self.update_prayers(False)

Diff for: silatycal.py

+78-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from gi.repository import GObject, GLib, Gtk, Gdk, GdkPixbuf
88
from datetime import date, timedelta
99
from hijrical import *
10+
from options import *
1011
import datetime
1112
import os
1213

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

30-
calc = HijriCal()
31+
self.options = Options()
32+
33+
calc = HijriCal(self.options.hijrical_adjustment)
3134
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']
3235
h_year, h_month, h_day, h_week_day = calc.today
3336
h_date = '%i %s %i' % ( h_day, h_months[int(h_month-1)], h_year)
@@ -52,7 +55,7 @@ def __init__(self):
5255
topbox.pack_end(box, False, False, 0)
5356

5457
# Set up the date switcher
55-
self.cal = Cal()
58+
self.cal = Cal(self)
5659

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

8285
class Cal(Gtk.Box):
83-
def __init__(self):
86+
def __init__(self, parent):
8487
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL, spacing=12)
8588
self.dateswitcher = Gtk.Grid(row_homogeneous=True)
8689
self.dateswitcher.set_halign(Gtk.Align.CENTER)
8790

91+
self.parent = parent
92+
8893
self._refdate = datetime.datetime.today()
89-
self.hijcal = HijriCal()
94+
self.hijrical = HijriCal(self.parent.options.hijrical_adjustment)
9095
self.weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
9196
self.gmonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
9297
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']
@@ -195,7 +200,7 @@ def __init__(self):
195200
gday = (self.refdate).strftime("%d")
196201
gmonth = (self.refdate).strftime("%B")
197202
gyear = (self.refdate).strftime("%Y")
198-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
203+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
199204

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

261-
hijrical = HijriCal()
262-
263-
self.hytoday, self.hmtoday, self.hdtoday = hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)
266+
self.hytoday, self.hmtoday, self.hdtoday = self.hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)
264267

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

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

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

282285
if (cgmday != self.gmtoday):
283286
gcolor = CalendarColor.DGrey
@@ -327,20 +330,20 @@ def left_arrow_pressed(self, widget, event):
327330
gday = self.refdate.strftime("%d")
328331
gmonth = self.refdate.strftime("%B")
329332
gyear = self.refdate.strftime("%Y")
330-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
333+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
331334
refwd = self.weekdays.index(self.refdate.strftime("%A"))
332335
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
333336
else:
334337
gday = self.refdate.strftime("%d")
335338
gmonth = self.refdate.strftime("%B")
336339
gyear = self.refdate.strftime("%Y")
337-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
340+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
338341
ndays = hijri_month_days(hyear, hmonth-2)
339342
self.refdate = self.refdate-timedelta(days=ndays)
340343
gday = self.refdate.strftime("%d")
341344
gmonth = self.refdate.strftime("%B")
342345
gyear = self.refdate.strftime("%Y")
343-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
346+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
344347
refwd = self.weekdays.index(self.refdate.strftime("%A"))
345348
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
346349
self.shift_days_of_table(gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, CalendarDirection.RIGHT)
@@ -356,20 +359,20 @@ def right_arrow_pressed(self, widget, event):
356359
gday = (self.refdate).strftime("%d")
357360
gmonth = (self.refdate).strftime("%B")
358361
gyear = (self.refdate).strftime("%Y")
359-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
362+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
360363
refwd = self.weekdays.index(self.refdate.strftime("%A"))
361364
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
362365
else:
363366
gday = (self.refdate).strftime("%d")
364367
gmonth = (self.refdate).strftime("%B")
365368
gyear = (self.refdate).strftime("%Y")
366-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
369+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
367370
ndays = timedelta(days=hijri_month_days(hyear, hmonth))
368371
self.refdate = self.refdate+ndays
369372
gday = self.refdate.strftime("%d")
370373
gmonth = self.refdate.strftime("%B")
371374
gyear = self.refdate.strftime("%Y")
372-
hyear, hmonth, hday = self.hijcal.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
375+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
373376
refwd = self.weekdays.index(self.refdate.strftime("%A"))
374377
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
375378
self.shift_days_of_table(gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, CalendarDirection.LEFT)
@@ -384,13 +387,72 @@ def set_image_from_file(self, iconpath):
384387
icon = Gtk.Image.new_from_stock(Gtk.STOCK_MISSING_IMAGE, 22)
385388
return icon
386389

390+
def refresh(self):
391+
# update hijri date
392+
self.hytoday, self.hmtoday, self.hdtoday = self.hijrical.goto_gregorian_day(self.gytoday, (self.gmtoday+1), self.gdtoday)
393+
# update hijri title label
394+
now_wd = datetime.datetime.now().strftime("%A")
395+
h_date = '%i %s %i' % ( self.hdtoday, self.hmonths[int(self.hmtoday-1)], self.hytoday)
396+
self.parent.titlestack.get_child_by_name("Hijri").set_label(now_wd+", "+h_date)
397+
# update calendar
398+
gday = (self.refdate).strftime("%d")
399+
gmonth = (self.refdate).strftime("%B")
400+
gyear = (self.refdate).strftime("%Y")
401+
if self.state == CalendarState.Gregorian:
402+
# Get the size of this month
403+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
404+
refwd = self.weekdays.index(self.refdate.strftime("%A"))
405+
calendarindex = -(refwd+(((6-refwd)+int(self.refdate.strftime("%d")))//7)*7)
406+
else:
407+
hyear, hmonth, hday = self.hijrical.goto_gregorian_day(int(gyear), (self.gmonths.index(gmonth)+1), int(gday))
408+
refwd = self.weekdays.index(self.refdate.strftime("%A"))
409+
calendarindex = -(refwd+(((6-refwd)+hday)//7)*7)
410+
for row in range(0, 6):
411+
for column in range(0, 7):
412+
newgday = int((self.refdate + timedelta(days=(calendarindex))).strftime("%d"))
413+
newgmonth = self.gmonths.index((self.refdate + timedelta(days=(calendarindex))).strftime("%B"))
414+
newgyear = int((self.refdate + timedelta(days=(calendarindex))).strftime("%Y"))
415+
newhyear, newhmonth, newhday = self.hijrical.goto_gregorian_day(newgyear, newgmonth+1, newgday)
416+
417+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next = newgday
418+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next = newhday
419+
420+
if (column == 0) or (column == 6):
421+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.LGrey
422+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.LGrey
423+
else:
424+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.White
425+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.White
426+
427+
if newgmonth != self.gmonths.index(self.refdate.strftime("%B")):
428+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.DGrey
429+
430+
if newhmonth != hmonth:
431+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.DGrey
432+
433+
if (newgday == self.gdtoday and newgmonth == self.gmtoday and newgyear == self.gytoday):
434+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background = CalendarColor.Blue
435+
436+
if (newhday == self.hdtoday and newhmonth == self.hmtoday and newhyear == self.hytoday):
437+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background = CalendarColor.Blue
438+
439+
newgbackground = self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_next_background
440+
newhbackground = self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_next_background
441+
442+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day = newgday
443+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Gregorian').day_background = newgbackground
444+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day = newhday
445+
self.caltable.get_child_at(column,row).labelstack.get_child_by_name('Hijri').day_background = newhbackground
446+
447+
calendarindex += 1
448+
387449
def shift_days_of_table(self, gday, gmonth, gyear, hday, hmonth, hyear, calendarindex, direction):
388450
for row in range(0, 6):
389451
for column in range(0, 7):
390452
newgday = int((self.refdate + timedelta(days=(calendarindex))).strftime("%d"))
391453
newgmonth = self.gmonths.index((self.refdate + timedelta(days=(calendarindex))).strftime("%B"))
392454
newgyear = int((self.refdate + timedelta(days=(calendarindex))).strftime("%Y"))
393-
newhyear, newhmonth, newhday = self.hijcal.goto_gregorian_day(newgyear, newgmonth+1, newgday)
455+
newhyear, newhmonth, newhday = self.hijrical.goto_gregorian_day(newgyear, newgmonth+1, newgday)
394456

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

0 commit comments

Comments
 (0)