Script de détermination des troisièmes mercredis du mois
De Wiki de Rhizomes.
- Ce script écrit en Python, permet de déterminer le 3e mercredi d'un mois.
Version originale par Benoit
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
## LICENSE
"""
A command-line utility to find the third Wednesday of a month
"""
import getopt, math,re
def usage(prg_name):
"""Usage: %(prg_name)s [options] [date]
returns the date of third wednesday of any month, 255 if something
went wrong, or 0 if no date has to be returned
-v --verbose verbose. Increases verbosity if set more than once
-h --help this help
date should be given as [[d]d/][m]m/y... where / can be replace at your
convenience with - or .
years before BC are not allowed"""
print usage.__doc__%{'prg_name':prg_name}
def gregorian_to_julian(year, month, day):
a = (14-month)/12
y = year+4800-a
m = month + 12*a - 3
return day + (153*m+2)/5 + y*365 + y/4 - y/100 + y/400 - 32045
def day(date):
return int(gregorian_to_julian(date['year'],
date['month'],
date['day'])+1) % 7
def main(args):
Weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
me=args[0]
if len(args)<2:
usage(me)
return 255
try:
options, args = getopt.getopt(args[1:],
'hv',
["help", "verbose"])
except:
usage(me); return 255
verbose=0
for k, v in options:
if k in ('-h', '--help'): usage(me); return 0
if k in ('-v', '--verbose'):
verbose+=1
# Parse date arg
splitDate={'sep':"[\./-]"}
validateDate="^([0-9]{1,2}%(sep)s)?[0-9]{1,2}%(sep)s[0-9]+$"%splitDate
if len(args)<1 or not re.match(validateDate,args[0]):
# At this point, we need a valid date
usage(me); return 255
# Get day, month, year
tag=re.compile(splitDate['sep'])
inputDate=tag.split(args[0])
date={'day':1}
if len(inputDate)>2:
date['day']=int(inputDate[0])
date['month']=int(inputDate[-2])
date['year']=int(inputDate[-1])
if not 0<date['month']<13:
usage(me); return 255
# Now, we can make Julian day
jd = day(date)
if (verbose==2):
print ("%(day)i/%(month)i/%(year)i si a "%date)+Weekdays[jd]
date['day']=1
fdom = day(date)
if (verbose==2):
print "The first day of the month is a: "+Weekdays[fdom]
if (fdom <= 3):
#first wednesday goes on the first week
date= (3-fdom)+2*7
if (verbose):
print "The third Wednesday is the "+str(date+1)+" this month"
else:
#first wednesday goes on the second week
date= (3-fdom)+3*7
if (verbose):
print "The third Wednesday is the "+str(date+1)+" this month"
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
Version modifiée par NicolasGrandjean
Modifiée pour permettre de retourner seulement le jour du mois indiqué (sous la forme MM/AAAA).
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
## LICENSE
"""
A command-line utility to find the third Wednesday of a month
"""
import getopt, math,re
def usage(prg_name):
"""Usage: %(prg_name)s [options] [date]
returns the date of third wednesday of any month, 255 if something
went wrong, or 0 if no date has to be returned
-v --verbose verbose. Increases verbosity if set more than once
-h --help this help
date should be given as [[d]d/][m]m/y... where / can be replace at your
convenience with - or .
years before BC are not allowed"""
print usage.__doc__%{'prg_name':prg_name}
def gregorian_to_julian(year, month, day):
a = (14-month)/12
y = year+4800-a
m = month + 12*a - 3
return day + (153*m+2)/5 + y*365 + y/4 - y/100 + y/400 - 32045
def day(date):
return int(gregorian_to_julian(date['year'],
date['month'],
date['day'])+1) % 7
def main(args):
Weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"]
me=args[0]
if len(args)<2:
usage(me)
return 255
try:
options, args = getopt.getopt(args[1:],
'hv',
["help", "verbose"])
except:
usage(me); return 255
verbose=0
for k, v in options:
if k in ('-h', '--help'): usage(me); return 0
if k in ('-v', '--verbose'):
verbose+=1
# Parse date arg
splitDate={'sep':"[\./-]"}
validateDate="^([0-9]{1,2}%(sep)s)?[0-9]{1,2}%(sep)s[0-9]+$"%splitDate
if len(args)<1 or not re.match(validateDate,args[0]):
# At this point, we need a valid date
usage(me); return 255
# Get day, month, year
tag=re.compile(splitDate['sep'])
inputDate=tag.split(args[0])
date={'day':1}
if len(inputDate)>2:
date['day']=int(inputDate[0])
date['month']=int(inputDate[-2])
date['year']=int(inputDate[-1])
if not 0<date['month']<13:
usage(me); return 255
# Now, we can make Julian day
jd = day(date)
if (verbose==2):
print ("%(day)i/%(month)i/%(year)i si a "%date)+Weekdays[jd]
date['day']=1
fdom = day(date)
if (verbose==2):
print ""+Weekdays[fdom]
if (fdom <= 3):
#first wednesday goes on the first week
date= (3-fdom)+2*7
if (verbose):
print ""+str(date+1)+""
else:
#first wednesday goes on the second week
date= (3-fdom)+3*7
if (verbose):
print ""+str(date+1)+""
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))