soroush / libcalendars

Collection of calendar arithmetic algorithms
GNU General Public License v3.0
46 stars 5 forks source link

Add support for unix timestamp #2

Open soroush opened 6 years ago

soroush commented 6 years ago

Support for converting unix timestamp to the Solar Hijri calendar is added. This is an early implementation that needs to be carefully tested and if everything goes well, I will release a version containing the same arithmetic for all calendar/timezone pairs. The code is in currently in add_timestamp branch.

Please test this feature as far as you can. Following is a simple test program to validate any input data.

#include <stdio.h>
#include <stdint.h>
#include <libcalendars/cl-solar-hijri.h>
#include <libcalendars/cl-gregorian.h>

int main() {
    time_t ts[] = {1421107199, 1521318959, 1508592260,
                   1521307799, 1521307800
                  };
    for(size_t i = 0; i < (sizeof(ts) / sizeof(ts[0])); ++i) {
        int16_t jy;
        uint8_t jm;
        uint16_t jd;
        uint8_t h;
        uint8_t m;
        uint8_t s;
        ts_to_sh(ts[i], 12600, &jy, &jm, &jd, &h, &m, &s);
        struct tm* gm = gmtime(&ts[i]);
        printf("%ld = %02d/%02d/%02d %02d:%02d:%02d %hu/%02d/%02d %02d:%02d:%02d\n", ts[i],
               gm->tm_year+1900,
               gm->tm_mon+1,
               gm->tm_mday+1,
               gm->tm_hour,
               gm->tm_min,
               gm->tm_sec,
               jy, jm, jd, h, m, s);
    }
    return 0;
}

which results in:

1421107199 = 2015/01/13 23:59:59 1393/10/23 03:29:59
1521318959 = 2018/03/18 20:35:59 1396/12/27 00:05:59
1508592260 = 2017/10/22 13:24:20 1396/07/29 16:54:20
1521307799 = 2018/03/18 17:29:59 1396/12/26 20:59:59
1521307800 = 2018/03/18 17:30:00 1396/12/26 21:00:00
soroush commented 6 years ago

@IMAN4K You might be interested in this.

IMAN4K commented 6 years ago

Well done @soroush Sounds working fine. I'll do more test ASAP.