Find day of week from month and year
Paul Rubin
http
Fri Sep 2 15:33:29 EDT 2005
"Laguna" <ed_zeng at yahoo.com> writes:
> I want to find the expiration date of stock options (3rd Friday of the
> month) for an any give month and year. I have tried a few tricks with
> the functions provided by the built-in module time, but the problem was
> that the 9 element tuple need to be populated correctly. Can anyone
> help me out on this one?
It's probably simplest to use the calendar module:
http://docs.python.org/lib/module-calendar.html
see the weekday function.
> d0 = expiration(9, 2005) # d0 would be 16
> d1 = expiration(6, 2003) # d1 would be 20
> d2 = expiration(2, 2006) # d2 would be 17
# not completely tested
import calendar
def expiration(month, year):
w1 = calendar.weekday(year, month, 1) # weekday of 1st of month
f1d = 1 + (4-w1) % 7 # date of 1st friday
return f1d + 14 # date of 3rd friday
More information about the Python-list
mailing list