LB Booster
Programming >> BASIC code examples >> Hijri calendar
http://lbb.conforums.com/index.cgi?board=code&action=display&num=1438650277

Hijri calendar
Post by Richard Russell on Aug 4th, 2015, 01:04am

I was asked to write a program to find the date according to the Hijri (Islamic) calendar. It took a lot of Googling to discover how to do it; here's the answer in case anybody else is interested. It needs Windows Vista or later:

Code:
    CAL.HIJRI = 6
    global CAL.HIJRI

    struct SYSTEMTIME, wYear as word, wMonth as word, _
      wDayOfWeek as word, wDay as word, wHour as word, _
      wMinute as word, wSecond as word, wMilliseconds as word

    struct CALDATETIME, CalId as long, Era as long, _
      Year as long, Month as long, Day as long, DayOfWeek as long, _
      Hour as long, Minute as long, Second as long, Tick as long

    calldll #kernel32, "GetSystemTime", SYSTEMTIME as struct, _
      result as long

    calldll #kernel32, "ConvertSystemTimeToCalDateTime", _
      SYSTEMTIME as struct, CAL.HIJRI as long, _
      CALDATETIME as struct, result as long

    print "Today's date in the Hijri calendar is:"
    print "Year ";  CALDATETIME.Year.struct;", ";
    print "Month "; CALDATETIME.Month.struct;", ";
    print "Day ";   CALDATETIME.Day.struct 

Richard.
Re: Hijri calendar
Post by Richard Russell on Aug 12th, 2015, 1:56pm

Gregorian-to-Hijri and Hijri-to-Gregorian conversion functions (the parameters are not checked for validity).

Edit: Corrected HijriToGregorian function.

Code:
    CAL.HIJRI = 6
    global CAL.HIJRI

    struct SYSTEMTIME, wYear as word, wMonth as word, _
      wDayOfWeek as word, wDay as word, wHour as word, _
      wMinute as word, wSecond as word, wMilliseconds as word

    struct CALDATETIME, CalId as long, Era as long, _
      Year as long, Month as long, Day as long, DayOfWeek as long, _
      Hour as long, Minute as long, Second as long, Tick as long

    year = 1952 : month = 5 : day = 3

    call GregorianToHijri year, month, day
    print year, month, day

    call HijriToGregorian year, month, day
    print year, month, day

    wait
    end

sub GregorianToHijri byref year, byref month, byref day
    SYSTEMTIME.wYear.struct = year
    SYSTEMTIME.wMonth.struct = month
    SYSTEMTIME.wDay.struct = day
    calldll #kernel32, "ConvertSystemTimeToCalDateTime", _
      SYSTEMTIME as struct, CAL.HIJRI as long, _
      CALDATETIME as struct, result as long
    year = CALDATETIME.Year.struct
    month = CALDATETIME.Month.struct
    day = CALDATETIME.Day.struct
end sub

sub HijriToGregorian byref year, byref month, byref day
    CALDATETIME.Year.struct = year
    CALDATETIME.Month.struct = month
    CALDATETIME.Day.struct = day
    CALDATETIME.CalId.struct = CAL.HIJRI
    CALDATETIME.Era.struct = 1
    for dow = 1 to 7
      CALDATETIME.DayOfWeek.struct = dow
      calldll #kernel32, "ConvertCalDateTimeToSystemTime", _
        CALDATETIME as struct, SYSTEMTIME as struct, _
        result as long
    next
    year = SYSTEMTIME.wYear.struct
    month = SYSTEMTIME.wMonth.struct
    day = SYSTEMTIME.wDay.struct
end sub 

Richard.
Re: Hijri calendar
Post by Richard Russell on Aug 13th, 2015, 9:24pm

I found this note about Gregorian-Hijri conversions generally:

"The same Gregorian date may actually have up to four Hijri equivalent dates according to the PLACE & TIME. The 1st of Ramadan in Jordan for example could be the 30th of Shaban in some other parts of the world according to the sighting of the moon, also 1st of Ramadan at an instance in San Francisco could be equivalent to the 2nd of Ramadan in China because of time zone difference. To add on top of that, the Islamic & Hebrew days begin at sunset, so the 1st of August before sunset & after sunset will correspond to two different Islamic & Hebrew days."

I don't know what assumptions the Windows API routines make, so don't be surprised if the conversion functions above give a different result from other converters you might find.

Richard.
Re: Hijri calendar
Post by Richard Russell on Aug 14th, 2015, 09:04am

Sorry, I discovered that the HijriToGregorian function didn't work. Now corrected in the post above.

Richard.