MJD时间的计算与UTC的转换

  • UTC(Universal Time, Co-ordinated) :世界协调时间
  • MJD(Modified Julian Date): 简化的儒略日
  • JD(Julian Day):儒略日

连续纪日的儒略日(JD):以儒略历公元前4713年1月1日的GMT正午为第0日的开始。

简化儒略日(MJD): MJD=JD-2400000.5 MJD的第0日是从公历1858年11月17日的GMT零时开始的。

在线计算MJD(十进制):http://www.csgnetwork.com/julianmodifdateconv.html
十进制转十六进制:https://www.rapidtables.com/convert/number/decimal-to-hex.html

计算MJD时间的C++代码:

#include <bits/stdc++.h>
using namespace std;

//年月日 转 mjd
long cal_mjd(int y, int m, int d) {
    //计算出儒略日
    int jd = (d - 32075 + 1461 * (y + 4800 + (m - 14) / 12) / 4 + 367 * (m - 2 - (m - 14) / 12 * 12) / 12 -
              3 * ((y + 4900 + (m - 14) / 12) / 100) / 4);
    //简化儒略日,其定义为:
    //MJD=JD-2400000.5
    int mjd = jd - 2400000.5;
    return mjd;
}

//mjd 转 年月日
void MJD2YMD(long MJD) {
    int year, month, day;
    short K;
    short dayInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    year = ((MJD) * 100 - 1507820) / 36525;
    month = ((MJD) * 10000 - 149561000 - year * 36525 / 100 * 10000) / 306001;
    day = (MJD) - 14956 - (year * 36525) / 100 - (month * 306001) / 10000;

    if (month == 14 || month == 15) {
        K = 1;
    } else {
        K = 0;
    }
    year = year + K;
    month = month - 1 - K * 12;
    //瑞年判断
    if ((year % 4 == 0) && (year % 400 != 0)) {
        dayInMonth[2] = 29;
    }
    if (day > dayInMonth[month]) {
        day -= dayInMonth[month];
        month += 1;
        if (month > 12) {
            month -= 12;
            year += 1;
        }
    }
    int Y = year + 1900;
    int M = month;
    int D = day;
    cout << "year = " << Y << " Month = " << M << " Day = " << D << endl;
    return;
}

string DecIntToHexStr(long num) {
    string str;
    long tmp = num / 16;
    int left = num % 16;
    if (tmp > 0)
        str += DecIntToHexStr(tmp);
    if (left < 10)
        str += (left + '0');
    else
        str += ('A' + left - 10);
    return str;
}

int main() {
    long mjd = cal_mjd(2019, 6, 13);
    cout << "get MJD:" << mjd << endl;
    string hexstr=+"0x"+DecIntToHexStr(mjd);
    cout << "MJD = "<< mjd << ",MJD to hex is :" << hexstr << endl;
    cout << "MJD to Date:";
    MJD2YMD(mjd);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值