0

Is there any function to directly convert Unix Time Stamp (returned by time() [ctime library]) to a struct tm or something that holds the date and time in Jalali format?

And I'm not looking for a way to convert Gregorian to Jalali. I'm looking for an algorithm to convert what time(NULL) returns directly to the Jalali calendar.

2
  • Voted to reopen as the original question (which this was marked as a duplicate of) did not answer this question. Jun 24, 2017 at 15:04
  • I would love to see a non-iterative answer such as this one for Gregorian calendars (howardhinnant.github.io/date_algorithms.html#civil_from_days). This only converts from a count of days, but it is trivial to convert from count-of-seconds to count-of-days. With a good algorithm, such a calendar could be easily added to this date/time/timezone library which is designed to have many calendars inter-operate: github.com/HowardHinnant/date Jun 24, 2017 at 15:08

1 Answer 1

4

I've developed a similar algorithm to convert Julian Day Number to Solar Hijri (~= Jalali) calendar date. The principles are same: take a reference point in time (in your case 1970-01-01 which is equal to JDN #2440587) and calculate how many years, month and days are past since then. My algorithm is based on Akramis's mean-year calculation which has a good conformance to Official Iranian Calendar (same in near past).

The C implementation is available here:

https://github.com/soroush/libcalendars/blob/dev/lib/src/cl-solar-hijri.c

Note that you can convert unix time to JDN only by adding 2440587 offset. So you will need to:

int16_t year;
uint8_t month;
int16_t day;
jdn_to_sh(unix_time + 2440587, &year, &month, &date);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.