GNSS时间转化(GPS时-儒略日-年月日时分秒)

使用python进行时间转换代码如下:

代码引用自:https://blog.csdn.net/qq_40185784/article/details/104956594

import math
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]


def cal2gps(cal):
    # cal2gps 将公历GPS时间转换到GPS周和周内的秒
    # 返回列表,周和周内秒
    mjd = cal2mjd(cal)
    # GPS从MJD44244开始
    e = mjd - 44244
    week = math.floor(e / 7)
    e = e - week * 7
    return [week, round(e * 86400)]


def gps2cal(gpst):
    # gps2cal  将GPS周和周内的秒转换到公历GPS时间
    # 返回列表 年月日时分秒
    # GPS从MJD44244开始
    mjd = 44244 + (gpst[0] * 86400 * 7 + gpst[1]) / 86400
    cal = mjd2cal(mjd)
    return cal

测试代码如下: 

import timesystem
line=input("请输入cal:")#line为字符串
cal=[]
for n in line.split(' '):#使用空格将字符串分离
    cal.append(int(n)) #将字符串转换为整数并存入列表
GPStime=timesystem.cal2gps(cal)
print(GPStime)

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值