figuring week of the day....

T

tekion

Is there a module where you could figure week of the day, like where
it starts and end. I need to do this for a whole year. Thanks.
 
R

r

Is there a module where you could figure week of the day, like where
it starts and end. I need to do this for a whole year. Thanks.

import datetime
help(datetime)

import time
help(time)
 
T

Tim Chase

tekion said:
Is there a module where you could figure week of the day, like where
it starts and end. I need to do this for a whole year. Thanks.

sounds like you want the standard library's "calendar" module,
particularly the monthcalendar() which gets you pretty close.

For a lazy version just using the stdlib with minimal fuss:
>>> import calendar as c
>>> week = lambda y,m,d: [w for w in c.monthcalendar(y, m) if d in w][0]
>>> week(2009, 1, 8)
[5, 6, 7, 8, 9, 10, 11]


the monthcalendar() call returns the whole month's calendar which
may be more what you want for the big-picture.

-tkc
 
T

Tim Chase

Tim said:
the monthcalendar() call returns the whole month's calendar which
may be more what you want for the big-picture.

And if you want a whole year's worth, you can get pretty close with:

import itertools as i
import calendar as c
for month in range(1,13):
for week in c.monthcalendar(2009, month):
print repr(w)

You don't detail how you want the month-boundaries to behave, so
this gives "calendar"'s default behavior of filling in zeros on
month-boundaries, so November through the 1st week in Dec 2009
comes back as

...
[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 0, 0, 0, 0, 0, 0],
[0, 1, 2, 3, 4, 5, 6],
...

rather than

...
[26, 27, 28, 29, 30, 31, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 1, 2, 3, 4, 5, 6],
...

-tkc
 
T

tekion

the monthcalendar() call returns the whole month's calendar which
may be more what you want for the big-picture.

And if you want a whole year's worth, you can get pretty close with:

   import itertools as i
   import calendar as c
   for month in range(1,13):
     for week in c.monthcalendar(2009, month):
       print repr(w)

You don't detail how you want the month-boundaries to behave, so
this gives "calendar"'s default behavior of filling in zeros on
month-boundaries, so November through the 1st week in Dec 2009
comes back as

   ...
   [0, 0, 0, 0, 0, 0, 1],
   [2, 3, 4, 5, 6, 7, 8],
   [9, 10, 11, 12, 13, 14, 15],
   [16, 17, 18, 19, 20, 21, 22],
   [23, 24, 25, 26, 27, 28, 29],
   [30, 0, 0, 0, 0, 0, 0],
   [0, 1, 2, 3, 4, 5, 6],
   ...

rather than

   ...
   [26, 27, 28, 29, 30, 31, 1],
   [2, 3, 4, 5, 6, 7, 8],
   [9, 10, 11, 12, 13, 14, 15],
   [16, 17, 18, 19, 20, 21, 22],
   [23, 24, 25, 26, 27, 28, 29],
   [30, 1, 2, 3, 4, 5, 6],
   ...

-tkc

Thanks, this is exactly what I am looking for. I will give it a try.
Do you what argument or documentation I should read up on how to get
the month's boundary rather than the default. I would assume it's
just an argument I give when creating the month object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top