[PYTHON] Correct Gregorian calendar date from Julian day

Hello. jdcal [^ 1](Conversion calculation to correct Gregorian calendar date (year, month, day) from Julian day (MJD)) And confirmed that it was correct.

Confirmation is based on the formula of "[Julian Day](https://ja.wikipedia.org/wiki/Julian Day)" (Wikipedia) (→ "[Conversion between Julian Day and Gregorian Calendar Date]( I found a match with the result of mjd2date ()) of "http://qiita.com/kkdd/items/2f045ee9aa3a76921cc7)".

[^ 1]: However, the formula used by this jdcal (jd2gcal) is not as good as the others. Not very good.

$ pip install jdcal
$ ./test_jdcal.py
 passed

test_jdcal.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
from jdcal import jd2gcal

def test():
    for mjd in range(146097):
        w = {jd2gcal: jd2gcal(2400000.5, mjd)[:3], mjd2date: mjd2date(mjd)}
        if w['jd2gcal']!=w['mjd2date']:
            print(w['jd2gcal'], "!=", w['mjd2date'])
            return
    print(' passed')

# conversion from modified Julian date to Gregorian calendar date 
def mjd2date(mjd):
    n = mjd + 678881  # Gregorian
    a = 4*n + 3
    a += 4*((3*((4*(n+1))//146097+1))//4)  # Gregorian
    b = 5*((a%1461)//4) + 2
    year, month, day = a//1461, b//153 + 3, (b%153)//5 + 1
    if month > 12:
        year, month = year+1, month%12
    return year, month, day

test()
exit(0)

Recommended Posts

Correct Gregorian calendar date from Julian day
Conversion between Julian day and Gregorian calendar date
Julian day calculation method