J
John Machin
Terry said:first of the month using some version of the the standard formula (seeLaguna said:I want to find the expiration date of stock options (3rd Friday of the
month) for an any give month and year.
From year and month (and day=1) get the day of the week (n in [0,6]) of the
below) and look up the third friday date in a precalculated 7-element list,
or, with n=0 on Saturday, 3rd Friday is 21-n
Here is a translation of the guts of a 30-year-old Basic program:
def friday3(m,y): # ints
if m <= 2:
m += 12
y -= 1
d = 1
n = d + 2*m + int(.6*(m+1)) + y + y//4 - y//100 + y//400 + 2
Some simplification is possible:
>>> [2*m + int(.6*(m+1)) for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37]
>>> [(13*m+3)//5 for m in range(15)] [0, 3, 5, 8, 11, 13, 16, 18, 21, 24, 26, 29, 31, 34, 37]
>>>
n = int((n/7.0- n//7)*7.0 + .5)
n %= 7