473,769 Members | 1,934 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 #1
20 5043
"Laguna" <ed*****@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(mont h, year):
w1 = calendar.weekda y(year, month, 1) # weekday of 1st of month
f1d = 1 + (4-w1) % 7 # date of 1st friday
return f1d + 14 # date of 3rd friday
Sep 2 '05 #2
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?


mx.DateTime provides a RelativeDateTim e constructor that handles things
like this.

http://www.egenix.com/files/python/mxDateTime.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 2 '05 #3
In article <11************ **********@g47g 2000cwa.googleg roups.com>,
"Laguna" <ed*****@yahoo. com> wrote:
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? .... 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


What do you mean by, "the 9 element tuple need to be populated
correctly"? Do you need someone to tell you what values it
needs? What happens if you use (2005, 9, 1, 0, 0, 0, 0, 0, 0),
for example? If you make this tuple with localtime or gmtime,
do you know what the 7th (tm[6]) element of the tuple is?
What tricks did you try, exactly?

Donn Cave, do**@u.washingt on.edu
Sep 2 '05 #4
> What do you mean by, "the 9 element tuple need to be populated
correctly"? Do you need someone to tell you what values it
needs? What happens if you use (2005, 9, 1, 0, 0, 0, 0, 0, 0),
for example? If you make this tuple with localtime or gmtime,
do you know what the 7th (tm[6]) element of the tuple is?
What tricks did you try, exactly?

Donn Cave, do**@u.washingt on.edu


Thanks for pointing out. tm[6] = weekday, and tm[7] = Julian data, but
I wouldn't know these values when my input values are month and year.

I will try out the more constructive suggestions from Paul and Robert.

Following is what I have tried. As you can tell, the results are wrong!
import time
time.asctime((2 003, 9, 1, 0, 0, 0, 0, 0, 0)) 'Mon Sep 01 00:00:00 2003' time.asctime((2 003, 8, 1, 0, 0, 0, 0, 0, 0)) 'Mon Aug 01 00:00:00 2003' time.asctime((2 003, 7, 1, 0, 0, 0, 0, 0, 0))

'Mon Jul 01 00:00:00 2003'

Sep 2 '05 #5
Paul,

Thanks for the suggestion on calendar module. Here is my solution and
it works:

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

Cheers,
Laguna

Sep 2 '05 #6
On Fri, 2005-09-02 at 16:46, Laguna wrote:
Paul,

Thanks for the suggestion on calendar module. Here is my solution and
it works:

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

Cheers,
Laguna


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)]

;)

-Carsten
Sep 2 '05 #7
Thanks for the "hint" :) I may use your solution if this becomes my
bottleneck!

I try to get away from Perl-ish syntax though.

Best,
L

Sep 2 '05 #8
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
"Laguna" <ed*****@yahoo. com> wrote:
What do you mean by, "the 9 element tuple need to be populated
correctly"? Do you need someone to tell you what values it
needs? What happens if you use (2005, 9, 1, 0, 0, 0, 0, 0, 0),
for example? If you make this tuple with localtime or gmtime,
do you know what the 7th (tm[6]) element of the tuple is?
What tricks did you try, exactly?

Donn Cave, do**@u.washingt on.edu


Thanks for pointing out. tm[6] = weekday, and tm[7] = Julian data, but
I wouldn't know these values when my input values are month and year.

I will try out the more constructive suggestions from Paul and Robert.

Following is what I have tried. As you can tell, the results are wrong!
import time
time.asctime((2 003, 9, 1, 0, 0, 0, 0, 0, 0)) 'Mon Sep 01 00:00:00 2003' time.asctime((2 003, 8, 1, 0, 0, 0, 0, 0, 0)) 'Mon Aug 01 00:00:00 2003' time.asctime((2 003, 7, 1, 0, 0, 0, 0, 0, 0))

'Mon Jul 01 00:00:00 2003'


Well, sure, that tm value will certainly not be the
3rd Friday, but it does correctly represent the first
day of the month. With localtime() you can find out
the day of the week, on the first day of the month.
When you know that, the 3rd Friday is simple arithmetic.

Since other followups have already spoon-fed you a
solution (assuming it works, haven't tried), here's an
example of what I mean -

import time
for m in range(1, 13):
c1 = time.mktime((20 05, m, 1, 0, 0, 0, 0, 0, 0))
d1 = time.localtime( c1)[6]
if d1 > 4:
f3 = 26 - d1
else:
f3 = 19 - d1
# f3 = 19 + (d1 // 5) * 7 - d1
c3 = time.mktime((20 05, m, f3, 0, 0, 0, 0, 0, 0))
print time.ctime(c3)

I don't know if you intend to go on to do much more
programming after this, but that's who I normally
assume we're talking to here, programmers. No one
knows everything and misses nothing, certainly not
me, but it's nice when people come to comp.lang.pytho n
and can account for at least the beginning of some
analysis of their problem. When that's missing, it's
hard to know what's really constructive.

Donn Cave, do**@u.washingt on.edu
Sep 2 '05 #9
Hey Donn,

I don't mean to offend anyone here. I was just saying that the other
solution is better suited for my problem. I truly appreciate your
analysis and suggestions.

BTW, I am not a programmer :( and I like the simplest solution whenever
possible.

Cheers,
L

Sep 2 '05 #10

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
5739
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
3617
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
4230
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
9579
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
9420
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,...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
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
6662
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
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.