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'] = 29month_mapping[2] = 29elif (year % 4 == 0 and year % 100 != 0):day_number['Feb'] = 29month_mapping[2] = 29else:day_number['Feb'] = 28month_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 = 0for count in range(1, month):julian_day += month_mapping.get(count)julian_day += dayreturn julian_daydef 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 = indexday = julian + month_mapping.get(index)break# modify the formatif month < 10:month = '0' + str(month)if day < 10:day = '0' + str(day)return month, dayif (__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!")

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

Python 儒略日和公历互转代码相关推荐

  1. python处理速度_如何让Python处理速度翻倍?内含代码

    原标题:如何让Python处理速度翻倍?内含代码 作为在日常开发生产中非常实用的语言,有必要掌握一些python用法,比如爬虫.网络请求等场景,很是实用.但python是单线程的,如何提高python ...

  2. python登录代码思路_用python登录Dr.com思路以及代码分享

    用python登录Dr.com思路以及代码分享 发布于 2014-08-28 22:31:52 | 192 次阅读 | 评论: 0 | 来源: 网友投递 Python编程语言Python 是一种面向对 ...

  3. python 终止、结束、退出 代码

    python 终止.结束.退出 代码 import sysprint("执行代码") # 终止程序 sys.exit(0) # 0:正常退出 1:异常退出 print(" ...

  4. python中的doc_基于Python获取docx/doc文件内容代码解析

    这篇文章主要介绍了基于Python获取docx/doc文件内容代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 整体思路: 下载文件并修改后缀 ...

  5. python代码示例图形-纯干货:手把手教你用Python做数据可视化(附代码)

    原标题:纯干货:手把手教你用Python做数据可视化(附代码) 导读:制作提供信息的可视化(有时称为绘图)是数据分析中的最重要任务之一.可视化可能是探索过程的一部分,例如,帮助识别异常值或所需的数据转 ...

  6. python编程小游戏代码-Python小游戏之300行代码实现俄罗斯方块

    前言 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很 ...

  7. python画代码-Python教程_Python画Mandelbrot集 代码

    Python教程_Python画Mandelbrot集 代码 作者:Comet 来源: 课课家 www.kokojia.com点击数:278发布时间:2015-06-19 11:17:19 曼德勃罗集 ...

  8. python代码翻译-用python实现百度翻译的示例代码

    用python实现百度翻译,分享给大家,具体如下: 首先,需要简单的了解一下爬虫,尽可能简单快速的上手,其次,需要了解的是百度的API的接口,搞定这个之后,最后,按照官方给出的demo,然后写自己的一 ...

  9. python七段数码管设计图案-Python绘制七段数码管实例代码

    七段数码管(seven-segmentindicator)由7段数码管拼接而成,每段有亮或不亮两种情况,改进型的七段数码管还包括一个小数点位置 绘制模式: input:输入当前日期的数字形式 proc ...

最新文章

  1. 继承和多态 3.0 -- 菱形继承
  2. CUDA占用率计算方法
  3. 转:Node.js和testacular的安装与配置
  4. java web应用程序_如何构建Java Web 应用程序 - Spring Boot?
  5. Python打包 pyinstaller
  6. Visual Studio IDE下载地址
  7. c++扫雷游戏代码_C语言学习教程,用C语言编写扫雷游戏
  8. LuaForUnity7.1:Lua“类与对象”
  9. springboot mongo查询固定字段_SpringBoot中文参考指南(2.1.6)32、使用 NoSQL 技术
  10. 存储器——存储器容量扩充
  11. xposed框架在分机安装失败_pokemon go 和 虚拟gps
  12. 深入理解Android相机体系结构之八
  13. 湖北武汉施工员报考排给水和电气的施工方法建筑七大员报考
  14. 创立仅一年GMV突破3亿!这个新锐品牌如何在快手实现爆发?
  15. 计算机基础-数据存储
  16. 统一网关Geteway
  17. Python中%是什么意思?如何使用?
  18. python调用通达信数据_[python]沪深龙虎榜数据导入通达信的自选板块并标注于k线图上...
  19. sketchup 计算机配置,流畅运行SU草图大师软件的最低电脑配置要求
  20. Flask 教程 第十三章:国际化和本地化 1

热门文章

  1. [渝粤教育] 西南科技大学 电子测量与仪表 在线考试复习资料(1)
  2. c语言ch1与ch2什么意思,ch1和ch2是什么意思
  3. python plc fx5u_三菱PLC FX3U和FX5U有什么区别
  4. libuv之msys2环境下编译
  5. Android JSON解析并展现在listactivity实例
  6. 幕测平台_使用Junit测试异常,测试NextDay函数
  7. FFMPEG框架学习——(2)视频的提取和解码
  8. iOS录屏直播(一)初识ReplayKit
  9. 生成android 证书
  10. sql转置两种实现方式记录