Python 儒略日和公历互转代码

Python 儒略日和公历互转代码

# python3

#-------------------------------------------------------------------------------
# Purpose:     conversion between Julian date and the Gregorian calendar.
# Author:      plovess
# Created:     2020-03-01
# update:      
#-------------------------------------------------------------------------------


# define the number of days for every month.
day_number = {'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30,\
        'Jul':31, 'Aug':31, 'Sep':30, 'Oct':31, 'Nov':30, 'Dec':31}
month_mapping = {1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31}


# determine whether the year is the leap year or not
def is_leap_year(year):

    if (year % 400 == 0):
        day_number['Feb'] = 29
        month_mapping[2] = 29
    elif (year % 4 == 0 and year % 100 != 0):
        day_number['Feb'] = 29
        month_mapping[2] = 29
    else:
        day_number['Feb'] = 28
        month_mapping[2] = 28


# obtain corresponding Julian day
def obtain_julian(time):
    year, month, day = time.split('-')
    year = int(year); month = int(month); day = int(day)
    is_leap_year(year)

    if month == 2 and day > month_mapping.get(2):
        print("Your input of the Gregorian calendar is error!")
        exit()

    julian_day = 0
    for count in range(1, month):
        julian_day += month_mapping.get(count)
    julian_day += day

    return julian_day


def obtain_date(time):

    year, julian = time.split('-')
    year = int(year)
    julian = int(julian)
    is_leap_year(year) 
    
    if (month_mapping.get(2) == 29 and julian > 366) or (month_mapping.get(2) == 28 and julian > 365):
        print("Your input of julian day is error!")
        exit()
    
    for index in range(1, 13):
        julian -= month_mapping.get(index)
        if (julian <= 0):
            month = index
            day = julian + month_mapping.get(index)
            break

    # modify the format
    if month < 10:
        month = '0' + str(month)
    if day < 10:
        day = '0' + str(day)

    return month, day


if (__name__ == '__main__'):

    a = input("1.convert xxxx-xx-xx to xxxx-julian; 2.convert xxxx-julian to xxxx-xx-xx. Please input the choose (1 or 2)\n")

    if not a.isalnum():
        print("There is no option that you input!")
        exit()

    if (int(a) == 1):
        time = input("Please input the time like xxxx-xx-xx!\n")
        julian_day = obtain_julian(time)
        print("The julian day of %s is %d"%(time, julian_day))

    elif (int(a) == 2):
        time = input("Please input the time like year-julian day!\n")
        month, day = obtain_date(time)
        print("The month is %s and the day is %s"%(month, day))

    else:
        print("There is no option that you input!")

以上是代码分享,希望能帮到需要的人,有错误的地方望告知。
作为编程小白,希望能在这里记录自己的一步步成长。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值