Jalali Calendar Date to Julian Day Number Conversion Algorithm

405 views
Skip to first unread message

Soroush Rabiei

unread,
Jul 18, 2017, 7:23:33 PM7/18/17
to Persian Computing
Hi everyone

I'm working on a new feature for Qt 5.10 that will hopefully bring support for various calendar systems, first of all Jalali (Persian) Calendar. The work is almost done, anf if everything goes well till 2017.08.08, we will have calendars registered as a new feature for Qt (: 

The calendaring backend, QDate-integration and widgets are almost done. The only remaining piece of requirements that my code can't fulfil, is the conversion algorithm to Julian Day Number and vice versa. I have tried median year length algorithm of Akram Mousavi, and several other implementations from KDE, but none is accurate enough for all days between JDN 0 to possibly some day around 3000/01/01. 

Do you know about, (or have implemented) an accurate algorithm that can convert Persian calendar date (y, m, d) to JDN and vice versa?

Note: I'm not looking for Persian Date <=> Gregorian Date conversion (aka 33=year algorithm, nor the farsiweb implementation). Please refer to JDN Wikipedia page for more information.

Cheers,
Soroush

Behnam Esfahbod

unread,
Jul 18, 2017, 7:26:21 PM7/18/17
to Soroush Rabiei, Persian Computing
Great news, Soroush! Looking forward to see this implemented.

Have you checked out the Mono implementation? It's here:

This and some other info is available on the (old) Sharif FarsiWeb page on Iranian Calendar: <http://persian-computing.org/references/Sharif-FarsiWeb-Inc/Iranian_calendar.html>


Hope this helps,
-Behnam




--
--
http://persian-computing.org/
http://groups.google.com/group/persian-computing/

---
You received this message because you are subscribed to the Google Groups "Persian Computing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to persian-computing+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Behnam Esfahbod | بهنام اسفهبد
http://behnam.es/
GPG-FP: 3E7F.B4B6.6F4C.A8AB.9BB9.7520.5701.CA40.259E.0F8B

Behdad Esfahbod

unread,
Jul 18, 2017, 7:27:41 PM7/18/17
to Soroush Rabiei, Persian Computing
If you check the FarsiWeb jalali.c, it's trivial to extract the part that converts Persian <=> Julian day number.

You can also check my calendar widget:

  http://taghvim.info/
  http://taghvim.info/behdadcalendar.phps

 
Please refer to JDN Wikipedia page for more information.

Cheers,
Soroush

--
--
http://persian-computing.org/
http://groups.google.com/group/persian-computing/

---
You received this message because you are subscribed to the Google Groups "Persian Computing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to persian-computing+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Soroush Rabiei

unread,
Jul 19, 2017, 3:32:20 AM7/19/17
to Persian Computing, soroush...@gmail.com
If you check the FarsiWeb jalali.c, it's trivial to extract the part that converts Persian <=> Julian day number.

I have gone through that code many times... There is no explicit conversion between JDN and calendar date. There is a variable named `j_day_no' which I take it to be Jalali day number (number of days since Jalali epoch) just like `g_day_no'. Neither can lead us to a JDN<=>Jalali conversion. The `j_day_no' points to some time near 1593-03-21 B.C. (979/01-01 AP) which is 1139660 days after JDN = 0. (JDN epoch is -4713-01-01). Simply applying the offset, the algorithm fails to maintain sanity on day numbers (minus days). Also applying 3120 year offset to final calculations of year number didn't help.

Behdad Esfahbod

unread,
Jul 19, 2017, 11:30:15 AM7/19/17
to Soroush Rabiei, Persian Computing
On Tue, Jul 18, 2017 at 11:16 PM, Soroush Rabiei <soroush...@gmail.com> wrote:
If you check the FarsiWeb jalali.c, it's trivial to extract the part that converts Persian <=> Julian day number.

I have gone through that code many times... There is no explicit conversion between JDN and calendar date. There is a variable named `j_day_no' which I take it to be Jalali day number (number of days since Jalali epoch) just like `g_day_no'. Neither can lead us to a JDN<=>Jalali conversion. The `j_day_no' points to some time near 1593-03-21 B.C. (979/01-01 AP) which is 1139660 days after JDN = 0. (JDN epoch is -4713-01-01). Simply applying the offset, the algorithm fails to maintain sanity on day numbers (minus days). Also applying 3120 year offset to final calculations of year number didn't help.

By definition the j_day_no is just an offset away from JDN.  I'm guessing that you are hitting C division issues.  The C version looks wrong to me.  Check the JS version for example:

  http://www.farsiweb.info/jalali/jalali.js

function div(a,b) {
  return Math.floor(a/b);
}

function remainder(a,b) {
  return a - div(a,b)*b;
}
Hope that helps,
b
 

--
--
http://persian-computing.org/
http://groups.google.com/group/persian-computing/

---
You received this message because you are subscribed to the Google Groups "Persian Computing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to persian-computing+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Behdad Esfahbod

unread,
Jul 19, 2017, 11:30:59 AM7/19/17
to Soroush Rabiei, Persian Computing
Reminds me that someone should put all the different jalali versions somewhere on github...

Behnam Esfahbod

unread,
Jul 19, 2017, 3:31:14 PM7/19/17
to Behdad Esfahbod, Soroush Rabiei, Persian Computing
On Wed, Jul 19, 2017 at 9:30 AM, Behdad Esfahbod <beh...@behdad.org> wrote:
Reminds me that someone should put all the different jalali versions somewhere on github...


Vahid N.

unread,
Jul 19, 2017, 3:31:38 PM7/19/17
to Persian Computing

Ebrahim Byagowi

unread,
Jul 19, 2017, 4:17:57 PM7/19/17
to Vahid N., Persian Computing
These would be useful also I guess, 1 2 3

On Wed, Jul 19, 2017 at 2:26 PM, Vahid N. <vahid....@gmail.com> wrote:

--

Soroush Rabiei

unread,
Jul 26, 2017, 11:05:40 AM7/26/17
to Persian Computing
Thank you all for your kind replies, I will try to catch up... By the way I managed to hack JDN to Persian Date conversion to handle a wider (maybe unlimited) range of years from JDN 0 (4753 B,C. to \infinity for Qt). The implementation is under review for Qt 5.10.0. There is a good chance we have it merged till Aug 8th before FF (In case there is no resistance over API change). 

I have implemented an algorithm based on 2048 cycle / mean year length studies of Mousa Akrami. Akrami's version is different compared to Birashk's cycles and have a different distribution of leap years. This algorithm seems to be the most accurate (compared to equinox observations). The official calendar of Iran relies on observation (not 33-year cycles for example) So this implementation sounds better.

Meanwhile, please check the implementation for possible bugs before merge. Its available here [https://codereview.qt-project.org/#/c/182341/]. I have checked against several dates, and it seems to work fine. There is also a hacked version of Islamic Civil calendar. Please have a look at it too. (Attached a big gif of calendar widgets in use)

For future reference: .NET implementation of Persian calendar falls behind date for days before epoch and it's buggy in leap year math. Farsiweb implementation uses an offset of jdn that makes it unable to convert negative years and buggy for years before 475. 

Cheers,
Soroush

 


Reply all
Reply to author
Forward
0 new messages