设为首页 收藏本站
查看: 1217|回复: 0

[经验分享] Python实践——datetime日期操作脚本

[复制链接]

尚未签到

发表于 2017-5-3 13:07:10 | 显示全部楼层 |阅读模式
  #!/usr/bin/python# -*- coding: utf-8 -*-#countDays.py# version 0.10      edited by lingyue.wkl 20110819 11:00:00# version 0.11      modified by lingyue.wkl 20110820 11:37:00   add functions for days list#this script count days,between two date or one date and the days between them#考虑下,很多方法可以抽象出来,进一步优化,先期先实现功能吧#下一个版本  改进所有函数,优化之,抽象之import time,getopt,sys,datetimedef date_to_str(in_date):return str(in_date)[:10]#计算两个日期之间相隔天数def get_count_between_two_date(begin_date,end_date):b_date = begin_date.split("-")b_date = [int(num) for num in b_date]b_date_time = datetime.datetime(b_date[0],b_date[1],b_date[2])e_date = end_date.split("-")e_date = [int(num) for num in e_date]e_date_time = datetime.datetime(e_date[0],e_date[1],e_date[2])return (e_date_time - b_date_time).days#计算某个日期前n天是哪一天   默认日期是今天def get_n_days_before_or_after_oneday(n_days,in_date=str(datetime.date.today())[:10]):begin_date = in_date.split("-")begin_date = [int(num) for num in begin_date]return str(datetime.datetime(begin_date[0],begin_date[1],begin_date[2]) + datetime.timedelta(days=n_days))[:10]def get_year():return str(datetime.date.today())[:4]def get_month():return str(datetime.date.today())[5:7]def get_day():return str(datetime.date.today())[8:]def get_now():return datetime.datetime.now()def get_today():return datetime.date.today()def get_yesterday():return get_n_days_before_or_after_oneday(-1,str(datetime.date.today())[:10])def get_tomorrow():return get_n_days_before_or_after_oneday(1,str(datetime.date.today())[:10])#两个日期之间  n天的日期列表def get_n_daystimes_list_of_two_date(begin_date,end_date):b_date = begin_date.split("-")b_date = [int(num) for num in b_date]b_date_time = datetime.datetime(b_date[0],b_date[1],b_date[2])e_date = end_date.split("-")e_date = [int(num) for num in e_date]e_date_time = datetime.datetime(e_date[0],e_date[1],e_date[2])days = (e_date_time - b_date_time).daysn_days_list = []for i in range(0,days+1):n_days_list.append(str(b_date_time + datetime.timedelta(days=i)))return n_days_listdef get_n_days_list_of_two_date(begin_date,end_date):return [str(day)[:10] for day in get_n_daystimes_list_of_two_date(begin_date,end_date)]def get_n_dayswiththreetimes_list_of_two_date(begin_date,end_date):days =  get_n_days_list_of_two_date(begin_date,end_date)days_three_time_list = []for day in days:for i in range(0,3):if i == 0:days_three_time_list.append(day+" 00:00:00")elif i == 1:days_three_time_list.append(day+" 12:00:00")else:days_three_time_list.append(day+" 23:59:59")return days_three_time_list#某个日期之前n天  所有日期列表def get_n_daystimes_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):begin_date = get_n_days_before_or_after_oneday(n_days,end_date)return get_n_daystimes_list_of_two_date(begin_date,end_date)def get_n_days_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):begin_date = get_n_days_before_or_after_oneday(n_days,end_date)return get_n_days_list_of_two_date(begin_date,end_date)def get_n_dayswiththreetimes_list_before_or_after_one_day(n_days,end_date=str(datetime.date.today())[:10]):begin_date = get_n_days_before_or_after_oneday(n_days,end_date)return get_n_dayswiththreetimes_list_of_two_date(begin_date,end_date)def help_msg():print("功能:日期相关操作")print("选项:")print("\t 默认,无选项,输出当天日期,格式2011-08-20")print("\t -y   [可选,输出当前年份]")print("\t -m   [可选,输出当前月份]")print("\t -d   [可选,输出当前日]")print("\t -n +-数字  [可选,计算当前日期前后多少天的日期,数字为负表示往前]")print("\t -f 2011-10-22[可选,指定坐标日期,即以指定日期开始计算,若不指定,坐标日期为当天]")print("\t -t 2011-10-25  [可选,目标日期,可用于计算两个日期相隔天数]")print("\t -l [1|2|3]  [可选,是否列表,若选定,输出日期间的所有序列,1 2 3 代表三种不同格式]")sys.exit(0)def print_list(l):for i in l:print(i)#print(get_year())#print(get_month())#print(get_day())#print(get_now())#print(get_today())#print(get_yesterday())#print(get_tomorrow())#print(get_n_days_before_or_after_oneday(2,"2011-08-20"))#print(get_n_daystimes_list_of_two_date("2011-08-01","2011-08-05"))#print(get_n_days_list_of_two_date("2011-08-01","2011-08-05"))#print(get_n_dayswiththreetimes_list_of_two_date("2011-08-01","2011-08-05")) #print(get_n_daystimes_list_before_or_after_ond_day(-5,"2011-08-20"))#print(get_n_days_list_before_or_after_ond_day(-5,"2011-08-20"))#print(get_n_dayswiththreetimes_list_before_or_after_ond_day(-5,"2011-08-20"))#程序入口,读入参数,执行def main():is_list = Falsetry:opts,args = getopt.getopt(sys.argv[1:],"n:f:t:o:ymdhrl:")if len(opts) == 0:print(get_today())sys.exit(0)for op,value in opts:if op in ("-h","-H","--help"):help_msg()if op == "-y":print(get_year())sys.exit(0)elif op == "-m":print(get_month())sys.exit(0)elif op == "-d":print(get_day())sys.exit(0)elif op == "-n":n_days = int(value)elif op == "-f":from_date = valueelif op == "-t":to_date = valueelif op == "-l":is_list = Truelist_type = valueexcept getopt.GetoptError:print(sys.argv[0]+" : params are not defined well!")#if "n_days" not in dir() and "from_date" not in dir() and "to_date" not in dir():#     print(result_str)if "n_days" in dir() and "from_date" not in dir() and "to_date" not in dir():if not is_list:print(get_n_days_before_or_after_today(n_days))else:if list_type == "1":result_list = get_n_days_list_before_or_after_one_day(n_days)elif list_type == "2":result_list = get_n_daystimes_list_before_or_after_one_day(n_days)elif list_type == "3":result_list = get_n_dayswiththreetimes_list_before_or_after_one_day(n_days)print_list(result_list)if "n_days" in dir() and "from_date" in dir() and "to_date" not in dir():if not is_list:print(get_n_days_before_or_after_oneday(n_days,from_date))else:if list_type == "1":result_list = get_n_days_list_before_or_after_one_day(n_days,from_date)elif list_type == "2":result_list = get_n_daystimes_list_before_or_after_one_day(n_days,from_date)elif list_type == "3":result_list = get_n_dayswiththreetimes_list_before_or_after_one_day(n_days,from_date)print_list(result_list)if "n_days" not in dir() and "from_date" in dir() and "to_date" in dir():if not is_list:print(get_count_between_two_date(from_date,to_date))else:if list_type == "1":result_list = get_n_days_list_of_two_date(from_date,to_date)elif list_type == "2":result_list = get_n_daystimes_list_of_two_date(from_date,to_date)elif list_type == "3":result_list = get_n_dayswiththreetimes_list_of_two_date(from_date,to_date)print_list(result_list)main()
最近需要做一些日期的操作,写了一个脚本
  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-372628-1-1.html 上篇帖子: 服务器暂时无法响应您的请求 500 Internal Server Error 下篇帖子: Python实践——datetime日期操作脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表