473,778 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find day of week from month and year

Hi Gurus,

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?

Thanks a bunch,
Laguna

Requirements:

d0 = expiration(9, 2005) # d0 would be 16
d1 = expiration(6, 2003) # d1 would be 20
d2 = expiration(2, 2006) # d2 would be 17

Sep 2 '05
20 5045
Carsten Haese wrote:
On Fri, 2005-09-02 at 16:46, Laguna wrote:
def expiration(year , month):
weekday = calendar.weekda y(year, month, 1)
table = [19, 18, 17, 16, 15, 21, 20]
return table[weekday]

This, of course, can be "optimized" into

def expiration(year , month):
return [19,18,17,16,15, 21,20][calendar.weekda y(year,month,1)]

;)


True, but do you find that more readable? If I saw that in code I was
maintaining I would likely rewrite it, probably to look a lot like the
first one (though likely with a more descriptive name than "table"...
maybe expirationTable ?).

(And, if I were "optimizing ", I would of course dispense with the
dynamic creation of the static table upon every execution of
expiration(), and move it outside the function.)

-Peter
Sep 3 '05 #11
Peter Hansen <pe***@engcorp. com> writes:
(And, if I were "optimizing ", I would of course dispense with the
dynamic creation of the static table upon every execution of
expiration(), and move it outside the function.)


Replacing it with a tuple might be enough for that.
Sep 3 '05 #12
Peter Hansen wrote:
Carsten Haese wrote:
On Fri, 2005-09-02 at 16:46, Laguna wrote:
def expiration(year , month):
weekday = calendar.weekda y(year, month, 1)
table = [19, 18, 17, 16, 15, 21, 20]
return table[weekday]

This, of course, can be "optimized" into

def expiration(year , month):
return [19,18,17,16,15, 21,20][calendar.weekda y(year,month,1)]

;)

True, but do you find that more readable? If I saw that in code I was
maintaining I would likely rewrite it, probably to look a lot like the
first one (though likely with a more descriptive name than "table"...
maybe expirationTable ?).

(And, if I were "optimizing ", I would of course dispense with the
dynamic creation of the static table upon every execution of
expiration(), and move it outside the function.)

-Peter


An alternative:

def expiration(year , month):
return 21 - (calendar.weekd ay(year,month,1 ) + 2) % 7
Sep 3 '05 #13
Donn,

You didn't look closely enough at those results. The OP's point was
that he did not know how to set all the tuple values correctly. Here's
a clearer example, I think:

import time
print time.asctime((2 005,9,1,0,0,0,0 ,0,0))
print time.asctime((2 005,9,1,0,0,0,1 ,0,0))
print time.asctime((2 005,9,1,0,0,0,2 ,0,0))
print time.asctime((2 005,9,1,0,0,0,3 ,0,0))

Prints:
Mon Sep 01 00:00:00 2005
Tue Sep 01 00:00:00 2005
Wed Sep 01 00:00:00 2005
Thu Sep 01 00:00:00 2005

No matter what time zone you're in, Sep 1, 2005 can't be all those days
of the week! :)

Your code works because you use mktime, which appears to ignore the
dayOfWeek element of the input tuple, as shown by the following:

print time.asctime(ti me.gmtime(time. mktime((2005,9, 1,0,0,0,0,0,0)) ))
print time.asctime(ti me.gmtime(time. mktime((2005,9, 1,0,0,0,1,0,0)) ))
print time.asctime(ti me.gmtime(time. mktime((2005,9, 1,0,0,0,2,0,0)) ))
print time.asctime(ti me.gmtime(time. mktime((2005,9, 1,0,0,0,3,0,0)) ))

Prints:
Thu Sep 01 06:00:00 2005
Thu Sep 01 06:00:00 2005
Thu Sep 01 06:00:00 2005
Thu Sep 01 06:00:00 2005

-- Paul

Sep 3 '05 #14
Paul Rubin wrote:
Peter Hansen <pe***@engcorp. com> writes:
(And, if I were "optimizing ", I would of course dispense with the
dynamic creation of the static table upon every execution of
expiration( ), and move it outside the function.)


Replacing it with a tuple might be enough for that.


You're right, and in fact that would actually be even faster since then
it's a LOAD_CONST instead of a LOAD_GLOBAL.

-Peter
Sep 3 '05 #15
On Fri, 02 Sep 2005 20:53:44 -0400, Peter Hansen <pe***@engcorp. com> wrote:
Carsten Haese wrote:
On Fri, 2005-09-02 at 16:46, Laguna wrote:
def expiration(year , month):
weekday = calendar.weekda y(year, month, 1)
table = [19, 18, 17, 16, 15, 21, 20]
return table[weekday]
.... True, but do you find that more readable? If I saw that in code I was
maintaining I would likely rewrite it, probably to look a lot like the
first one (though likely with a more descriptive name than "table"...
maybe expirationTable ?).


That doesn't explain anything, IMHO. What 'table' really is is a list of
day-of-month candidates for the third Friday.

For some pieces of code, I find that it's better to document what it /does/
than to try to document /how/ it does it. And maybe add a bunch of unit
tests to /demonstrate/ that it seems to work.

The next programmer can then choose to either (a) understand the code or (b)
rip it out and replace it.

I would leave the body alone, but rename the function 'third_friday_o f_month',
and do 'expiration = third_friday_of _month'.

/Jorgen

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Sep 3 '05 #16

Laguna wrote:
Hi Gurus,

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?

Thanks a bunch,
Laguna

Requirements:

d0 = expiration(9, 2005) # d0 would be 16
d1 = expiration(6, 2003) # d1 would be 20
d2 = expiration(2, 2006) # d2 would be 17


Sep 14 '05 #17
Laguna wrote:
Hi Gurus,

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?

Thanks a bunch,
Laguna

Requirements:

d0 = expiration(9, 2005) # d0 would be 16
d1 = expiration(6, 2003) # d1 would be 20
d2 = expiration(2, 2006) # d2 would be 17

import calendar
[y[4] for y in calendar.monthc alendar(2005, 9) if y[4]!=0][2] 16 [y[4] for y in calendar.monthc alendar(2003, 6) if y[4]!=0][2] 20 [y[4] for y in calendar.monthc alendar(2006, 2) if y[4]!=0][2]

17

Sep 14 '05 #18
>>> import calendar
[y[4] for y in calendar.monthc alendar(2005, 9) if y[4]!=0][2] 16 [y[4] for y in calendar.monthc alendar(2003, 6) if y[4]!=0][2] 20 [y[4] for y in calendar.monthc alendar(2006, 2) if y[4]!=0][2]

17

Sep 14 '05 #19
Laguna wrote:
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

first of the month using some version of the the standard formula (see
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
n = int((n/7.0- n//7)*7.0 + .5)
# n=0 is Saturday, making 3rd Friday the 21st.
return 21 - n
Requirements:
d0 = expiration(9, 2005) # d0 would be 16
d1 = expiration(6, 2003) # d1 would be 20
d2 = expiration(2, 2006) # d2 would be 17

for m,y in ((9,2005), (6,2003), (2,2006)): print friday3(m,y)

....
16
20
17

Terry J. Reedy

Sep 14 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5355
by: bad | last post by:
i´ve two variables the year and the week (2003 and 5) that means the 5th week of the year 2003. now i need the start- and enddate of the 5th week of year 2003. i hope someone can help me to solve this problem.
6
6788
by: N8tor | last post by:
I'm a programming neophyte so needless to say I need some help on some VB scripting. I need to create some code to find what month it will be this coming Saturday. I also need to do the same for the week # of the month this Saturday. And finally, the Year of the month this Saturday. Could someone help me with this?
4
5740
by: chennakeshava_ramesh | last post by:
hi, I have a problem, I am not able to find out which day of the week it is using the calendar class. I am using set() function to set the date and want to find out which day i.e mon,tue etc of the week it is . Can anyone help me out with this, regards
13
3620
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not sure if it can be done, but all help welcomed. E.g. I have December and would like to see the last 2 weeks.. So this doesnt mean the last 15 days. What i mean by this is...
2
1444
by: TGEAR | last post by:
I would like to increase by one week and if it reaches 5th week, month will be increased by one and also it will show a different text when it reaches every year automatically. For example, 1-month: Week 1 1-month: Week 2 1-month: Week 3 1-month: Week 4
2
3119
by: tasmontique | last post by:
I am working on an access 2002 flight schedule database. I am new to access but have some basic understanding of sql and vb6 code. I have learned a lot from this website. Thanks much Hopefully you can help me with this one. This database handles a varying number of flights for a varying number of days for a varying number of months. I have a number of tables as follows. I tried to normalize to the best of my ability.
6
4231
by: =?Utf-8?B?UGF1bA==?= | last post by:
HI I have a stored procedure that returns data with a date field in the form of a DateTime type. I need to place data in variables based on days of the week starting with the first thursday of the month. So the week would be week 1= (first thursday of the month through the next wed). So for example for July 07 the first thursday is july5th so the first week would be thursday july 5th , friday july 6th, sat july 7th, sun july 8th, mon...
5
2839
by: cssExp | last post by:
the problem is, i have a dynamic database driven site, each data is entered with year, week etc.. 2 months ago assuming I'll create sort option in future i put everything, i.e year, week, hour, minute etc.. but forgot month. Now i'm adding sort option but i need month but not available during database entry i use the following. $year = date('Y'); $week = date('W'); $day = date('d'); $hour = date('H'); $minute = date('i');.
3
5840
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, is there a way if you are given a Year and a Week an easy way to go back say 26 weeks ago from given year/week. for example, given: 2008/16 26 weeks prior is: 2007/43 thanks,
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9470
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10127
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8957
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.