儒略日转公历 以及 公历转儒略日(python版)

作为一个测绘专业的学生,经常需要儒略日,年积日,GPS周秒等的时间转换。

写了个小函数方便每次的转换时间,利用的是列表形式

mjd2cal() 函数是简化儒略日计算公历年月日时分秒,输入简化儒略日(含小数),返回年月日时分秒的一维列表。

cal2mjd() 函数将公历年月日时分秒转换到简化儒略日,输入年月日时分秒的一维列表返回简化儒略日值。

def cal2mjd(cal):
# cal2jd 将公历年月日时分秒转换到简化儒略日。
# 输入公历时间列表,返回儒略日
    if(len(cal)<6):
        for i in range(len(cal),6):
            cal.append(0)
    year  = cal[0]
    month = cal[1]
    day   = cal[2]+(cal[3]*3600+cal[4]*60+cal[5])/86400;
    y     = year+4800  
    m     = month
    if( year < 0 ):
        print('Year is wrong')
        return False
    
    if( m <= 2):
        # 1,2月视为前一年13,14月
        m = m + 12
        y = y - 1
    e = math.floor(30.6 * (m+1))
    a = math.floor(y/100)
    # 教皇格雷戈里十三世于1582年2月24日以教皇训令颁布,将1582年10月5日至14抹掉。1582年10月4日过完后第二天是10月15日
    if( year <1582 )or(year==1582 and month<10)or(year==1582 and month==10 and day<15):
        b = -38
    else:
        b = math.floor((a/4) - a)
    c = math.floor(365.25 * y)
    jd = b + c + e + day - 32167.5
    mjd = jd - 2400000.5
    return mjd

def mjd2cal(mjd):
# 从简化儒略日计算公历年月日时分秒
# 返回的cal是年月日时分秒 列表
# 公元1582年10月4日24:00点之前使用儒略历,公元1582年10月15日00:00点之后使用公历
    J = mjd + 2400000.5
    if (J < 1721423.5):
        #公元1月1日0时
        BC = 1;
    else:
        BC = 0;

    if( J < 2299160.5 ): 
        # 1582.10.4. 24:00 前
        j0=math.floor(J+0.5)
        dd=J+0.5-j0
    else: 
        #不是闰年的年数
        n1=math.floor((J-2342031.5)/36524.25/4)+1   #1700.3.1.0
        n2=math.floor((J-2378555.5)/36524.25/4)+1   #1800.3.1.0
        n3=math.floor((J-2415079.5)/36524.25/4)+1   #1900.3.1.0
        j0=n1+n2+n3+J+10
        dd=j0+0.5-math.floor(j0+0.5)
        j0=math.floor(j0+0.5)
    
    j0=j0+32083
    year0=math.ceil(j0/365.25)-1
    year=year0-4800
    day=j0-math.floor(year0*365.25)
    month=math.floor((day-0.6)/30.6)+3
    day=day-round((month-3)*30.6)
    
    if (month>12):
        month=month-12
        year=year+1    
    year=year-BC
    sec=round(dd*86400)    
    hour=math.floor(sec/3600)
    sec=sec-hour*3600
    minute=math.floor(sec/60)
    sec=sec-minute*60  
    return [year, month, day, hour, minute, sec]

这里转化的是简化儒略日,需要儒略日不需要减去2400000.5即可

代码里注释写的也比较清楚,计算公式网上也可以查到。这里就不重复了

  • 10
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值