首页 > 解决方案 > 回历日期公式

问题描述

将“自纪元以来的时间”戳记转换为1525249213回历日期(年-月-日)的公式是什么?

我知道有一天会有误差,这对我来说是可以的。

标签: datehijri

解决方案


我发现的算法是将日期转换为儒略日期,然后将儒略日期转换为回历。

PS我不记得我在哪里找到的。我不对使用此代码的任何错误或错误准确性或后果 bla bla bla 负责。

unsigned long gregorian_to_julian(const int gyear, const int gmonth, const int gday) {
    if (gmonth < 3) {
        gyear -= 1;
        gmonth += 12;
    }
    auto a = int(gyear / 100.0f);
    auto b = (gyear == 1582 && (gmonth > 10 || (gmonth == 10 && gday > 4)) ? -10 :
              (gyear == 1582 && gmonth == 10 ? 0 :
               (gyear < 1583 ? 0 : 2 - a + int(a / 4.0f))));
    return int(365.25f * (gyear + 4716)) + int(30.6001f * (gmonth + 1)) + gday + b - 1524;
}

std::array<int,3> julian_to_hijri(const unsigned long julian_datestamp) {
    std::array<int,3> result;
    auto y = 10631.0f / 30.0f;
    auto epoch_astro = 1948084;
    auto shift1 = 8.01f / 60.0f;
    auto z = julian_day - epoch_astro;
    auto cyc = int(z / 10631.0f);
    z = z - 10631 * cyc;
    auto j = int((z - shift1) / y);
    z = z - int(j * y + shift1);
    result[0] = 30 * cyc + j; //Hijri Year
    result[1]= int((z + 28.5001f) / 29.5f); //Hijri Month
    if (result[1] == 13) {
        result[1]= 12;
    }
    result[2] = z - int(29.5001f * result[1]- 29);// Hijri day
    return result;
}

PS 结果回历日期会有 +-1 一日误差。


推荐阅读