儒略日和常规日期的转换(Python版)

天文地理学科经常会使用儒略日(Julian Day)
这里使用Python的datetime模块实现其和常规日期的转换

from datetime import datetime


def d_to_jd(time):
    fmt = '%Y.%m.%d'
    dt = datetime.strptime(time, fmt)
    tt = dt.timetuple()
    return tt.tm_year * 1000 + tt.tm_yday


def jd_to_time(time):
    dt = datetime.strptime(time, '%Y%j').date()
    fmt = '%Y.%m.%d'
    return dt.strftime(fmt)


if '__main__' == __name__:
    flag = input('Please input the date transform type: 1 for Julian day to date; 2 for date to Julian day:')
    if 1 == int(flag):
        time = input('Please input the date (YY.MM.DD):')
        print(d_to_jd(time))
    elif 2 == int(flag):
        time = input('Please input the Julian day:')
        print(jd_to_time(time))

运行结果:

Please input the date transform type: 1 for Julian day to date; 2 for date to Julian day:1
Please input the date (YY.MM.DD):2016.08.21
2016234
Please input the date transform type: 1 for Julian day to date; 2 for date to Julian day:2
Please input the Julian day:2016234
2016.08.21

你可能感兴趣的:(GIS,数学(概念与方法),其它)