本篇博客记录cu上shell比赛的第六题:输入一个日期,然后程序打印出这个日期是这一年的第几天,然后再打印出昨天的日期是多少:
这个脚本还是十分容易的,我直接上脚本吧:
[root@centos6 shellbisai]# cat 6.py#coding=utf-8#!/usr/bin/env pythonimport datetimeimport sysdef describe(): print """此脚本需要您输入一个日期,运行格式如下:python script.py 2013-9-20脚本会显示您输入的这一年的第几天同时,脚本还将打印昨天是哪一天 """def check(): if len(sys.argv) != 2 :# describe() print "Script will be exit..." sys.exit() else: try: inday=str(sys.argv[1])#print inday formatDay=datetime.datetime(int(inday.split('-')[0]),int(inday.split('-')[1]),int(inday.split('-')[2]),0,0,0,0) resultNo=formatDay.timetuple().tm_yday print inday + "是这一年的第" + str(resultNo) + "天" except Exception: sys.exit() print "===========================" today=datetime.date.today() year=today.year month=today.month day=today.day - 1 print "昨天的日期是: " + str(year) + "-" + str(month) + "-" + str(day)if __name__ == "__main__": describe() check()[root@centos6 shellbisai]#
下面插入几张插图:
原文地址:记录CU上shell比赛日:打印日期, 感谢原作者分享。 世上没有绝望的处境,只有对处境绝望的人。