Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

script for seconds in given month?

34 views
Skip to first unread message

edfialk

unread,
Apr 16, 2007, 12:22:36 PM4/16/07
to
Hi, does anyone happen to know of a script that would return the
number of seconds in a month if I give it a month and a year?

My python is a little weak, but if anyone could offer some suggestions
I think I could handle it myself, or if anyone happens to know of a
script already written that performs this I would be extremely
grateful.

Thanks!
-Ed

Jim

unread,
Apr 16, 2007, 12:45:29 PM4/16/07
to
Probably there are sophisticated answers, but have you tried
something like:

monthDays={'Jan':31,'Feb':28, ..}
secs=60*60*24*monthDays[thisMonth]
if (thisMonth=='Feb'
and isLeap(thisYear)):
secs+=60*60*24
return secs

?

python...@gmail.com

unread,
Apr 16, 2007, 12:56:44 PM4/16/07
to
On Apr 16, 6:22 pm, "edfialk" <edfi...@gmail.com> wrote:
> Hi, does anyone happen to know of a script that would return the
> number of seconds in a month if I give it a month and a year?
>

something like this might work, it should event handle DST correctly.
You could read up on mktime() if you want to make sure.

from time import mktime
def secondsInMonth(year, month):
s1 = mktime((year,month,1,0,0,0,0,0,-1))
s2 = mktime((year,month+1,1,0,0,0,0,0,-1))
return s2-s1

/Matt

sk...@pobox.com

unread,
Apr 16, 2007, 1:18:29 PM4/16/07
to python...@gmail.com, pytho...@python.org

Matt> from time import mktime
Matt> def secondsInMonth(year, month):
Matt> s1 = mktime((year,month,1,0,0,0,0,0,-1))
Matt> s2 = mktime((year,month+1,1,0,0,0,0,0,-1))
Matt> return s2-s1

Probably won't work if month==12. ;-)

Skip

Paul McGuire

unread,
Apr 16, 2007, 1:28:25 PM4/16/07
to

Do you need to handle leap seconds too? (not a joke)

-- Paul

edfialk

unread,
Apr 16, 2007, 1:49:33 PM4/16/07
to
Jim: I need years too, basically from 1960-2000. Don't want to
hardcode all those days :)

Matt: Thanks, I will try this out.

Paul: I don't believe we need leap seconds. Leap days definitely.

I'll let you know how Matt's code works. Any other suggestions feel
free to let me know.

Thanks all!
-Ed

attn.st...@gmail.com

unread,
Apr 16, 2007, 2:11:08 PM4/16/07
to

Actually, mktime as described in the C Standard does not
restrict members such as tm_mon of struct tm to the
range 0-11 (1-12 for the corresponding entry in the
time tuple in Python). So the underlying struct in
CPython may be normalized and return a perfectly valid
time even if month is > 12.

--
Regards,
Steven


Shane Geiger

unread,
Apr 16, 2007, 3:42:29 PM4/16/07
to sk...@pobox.com, python...@gmail.com, pytho...@python.org

import datetime

def first_day_of_next_month( year, month ):
"""returns the first day of the next month
>>> first_day_of_next_month(2007,5)
datetime.datetime(2007, 6, 1, 0, 0)
>>> first_day_of_next_month(2007,12)
datetime.datetime(2008, 1, 1, 0, 0)
"""
oneday = datetime.timedelta(days=1)
day = datetime.datetime(year, month, 1)
if day.day == 1:
day += oneday
while day.day != 1:
day += oneday
return day


from time import mktime
def secondsInMonth(year, month):
s1 = mktime((year,month,1,0,0,0,0,0,-1))

day = first_day_of_next_month(year, month)
year = day.year
month = day.month


s2 = mktime((year,month+1,1,0,0,0,0,0,-1))
return s2-s1


year = 2007
month = 2
print secondsInMonth(year, month)

sk...@pobox.com wrote:
> Matt> from time import mktime
> Matt> def secondsInMonth(year, month):
> Matt> s1 = mktime((year,month,1,0,0,0,0,0,-1))
> Matt> s2 = mktime((year,month+1,1,0,0,0,0,0,-1))
> Matt> return s2-s1
>
> Probably won't work if month==12. ;-)
>
> Skip
>

--
Shane Geiger
IT Director
National Council on Economic Education
sge...@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

sgeiger.vcf

Paul McGuire

unread,
Apr 16, 2007, 7:01:36 PM4/16/07
to

I googled for "NIST leap second" and found this table online of leap
seconds, if you do in fact need them: http://tf.nist.gov/pubs/bulletin/leapsecond.htm

-- Paul

Alex Martelli

unread,
Apr 17, 2007, 12:41:00 AM4/17/07
to
edfialk <edf...@gmail.com> wrote:

import calendar

def seconds_in_month(month, year):
nomatter, daysinmonth = calendar.monthrange(year, month)
return daysinmonth * 24 * 60 * 60


Alex

Hendrik van Rooyen

unread,
Apr 17, 2007, 1:44:55 AM4/17/07
to pytho...@python.org
"Paul McGuire" <pt...g@austin.rr.com> wrote:

>From some assembler, here are some values.
Stick them in a two dicts for normal and leap year.
the first one is a cumulative table, I included it in
case you can use it...

; SECONDS TABLES (LITTLE ENDIAN)


MSECTAB_NORM:

DB 080H,033H,0E1H,001H ;MONTH ZERO HAS FULL YEAR SECONDS
DB 000H,000H,000H,000H ;JANUARY
DB 080H,0DEH,028H,000H ;FEBRUARY
DB 080H,0C8H,04DH,000H ;MARCH
DB 000H,0A7H,076H,000H ;APRIL
DB 000H,034H,09EH,000H ;MAY
DB 080H,012H,0C7H,000H ;JUNE
DB 080H,09FH,0EEH,000H ;JULY
DB 000H,07EH,017H,001H ;AUGUST
DB 080H,05CH,040H,001H ;SEPTEMBER
DB 080H,0E9H,067H,001H ;OCTOBER
DB 000H,0C8H,090H,001H ;NOVEMBER
DB 000H,055H,0B8H,001H ;DECEMBER


MSECTAB_LEAP:

DB 000H,085H,0E2H,001H ;MONTH ZERO HAS FULL LEAP YEAR SECONDS
DB 000H,000H,000H,000H ;JANUARY
DB 080H,0DEH,028H,000H ;FEBRUARY
DB 000H,01AH,04FH,000H ;MARCH
DB 080H,0F8H,077H,000H ;APRIL
DB 080H,085H,09FH,000H ;MAY
DB 000H,064H,0C8H,000H ;JUNE
DB 000H,0F1H,0EFH,000H ;JULY
DB 080H,0CFH,018H,001H ;AUGUST
DB 000H,0AEH,041H,001H ;SEPTEMBER
DB 000H,03BH,069H,001H ;OCTOBER
DB 080H,019H,092H,001H ;NOVEMBER
DB 080H,0A6H,0B9H,001H ;DECEMBER


; NUMBER OF SECONDS IN MONTH (LITTLE ENDIAN)


MSTAB_NORM:

DB 000H,000H,000H,000H ;MONTH ZERO HAS NO SECONDS
DB 080H,0DEH,028H,000H ;JANUARY
DB 000H,0EAH,024H,000H ;FEBRUARY
DB 080H,0DEH,028H,000H ;MARCH
DB 000H,08DH,027H,000H ;APRIL
DB 080H,0DEH,028H,000H ;MAY
DB 000H,08DH,027H,000H ;JUNE
DB 080H,0DEH,028H,000H ;JULY
DB 080H,0DEH,028H,000H ;AUGUST
DB 000H,08DH,027H,000H ;SEPTEMBER
DB 080H,0DEH,028H,000H ;OCTOBER
DB 000H,08DH,027H,000H ;NOVEMBER
DB 080H,0DEH,028H,000H ;DECEMBER

MSTAB_LEAP:

DB 000H,000H,000H,000H ;MONTH ZERO HAS NO SECONDS
DB 080H,0DEH,028H,000H ;JANUARY
DB 080H,03BH,026H,000H ;FEBRUARY
DB 080H,0DEH,028H,000H ;MARCH
DB 000H,08DH,027H,000H ;APRIL
DB 080H,0DEH,028H,000H ;MAY
DB 000H,08DH,027H,000H ;JUNE
DB 080H,0DEH,028H,000H ;JULY
DB 080H,0DEH,028H,000H ;AUGUST
DB 000H,08DH,027H,000H ;SEPTEMBER
DB 080H,0DEH,028H,000H ;OCTOBER
DB 000H,08DH,027H,000H ;NOVEMBER
DB 080H,0DEH,028H,000H ;DECEMBER


; *****************************************************************

hth - Hendrik

python...@gmail.com

unread,
Apr 17, 2007, 3:47:11 AM4/17/07
to
On Apr 17, 6:41 am, a...@mac.com (Alex Martelli) wrote:

> edfialk <edfi...@gmail.com> wrote:
> > Hi, does anyone happen to know of ascriptthat would return the
> > number ofsecondsin amonthif I give it amonthand a year?

>
> > My python is a little weak, but if anyone could offer some suggestions
> > I think I could handle it myself, or if anyone happens to know of a
> >scriptalready written that performs this I would be extremely

> > grateful.
>
> import calendar
>
> def seconds_in_month(month, year):
> nomatter, daysinmonth = calendar.monthrange(year,month)
> return daysinmonth * 24 * 60 * 60
>
> Alex

That works if you define the number of seconds in a month a the number
of days * 24 * 60 * 60.
If you have DST then that's not always the truth. The difference in
seconds (clock time) is according to your example, but the number of
seconds that passed during the month differs.
Not sure which one was requested in the original post though.

python...@gmail.com

unread,
Apr 17, 2007, 3:52:46 AM4/17/07
to
On Apr 16, 7:49 pm, "edfialk" <edfi...@gmail.com> wrote:
> Jim: I need years too, basically from 1960-2000. Don't want to
> hardcode all those days :)
>
> Matt: Thanks, I will try this out.
>
> Paul: I don't believe we need leapseconds. Leap days definitely.

>
> I'll let you know how Matt's code works. Any other suggestions feel
> free to let me know.
>
> Thanks all!
> -Ed

There's one thing I forgot to mention: mktime (like most things using
the standard C functions) will work reliably from 1970 to 2038 or so.
Also, I think posix explicitly says that leap seconds are ignored, so
those are not covered.

/Matt

edfialk

unread,
Apr 23, 2007, 4:28:11 PM4/23/07
to
Alex, very nice. That should be good enough for me.
The rest of you as well, thanks for all the help.

I, unfortunately, failed to realize the actual platform the script is
for is IronPython. When trying to import calendar in IronPython, I
get:

SyntaxError: future feature is not defined: with_statement (c:
\Python25\Lib\calendar.py, line 8)

so, incompatible. I have moved my question over to the IronPython
group, but if anyone is familiar with IronPython and knows how to
perform the same function, that's what I'm looking for now. :)

Thanks again!
-Ed

Carsten Haese

unread,
Apr 23, 2007, 4:39:11 PM4/23/07
to edfialk, pytho...@python.org

Try this on for size:

def seconds_in_month(mo,yr):
import datetime
start_date = datetime.date(yr,mo,1)
mo += 1
if mo==13: mo=1; yr += 1
end_date = datetime.date(yr,mo,1)
delta = end_date - start_date
return 24*60*60*delta.days

-Carsten


edfialk

unread,
Apr 23, 2007, 4:44:11 PM4/23/07
to
Alex, very nice. Thank you.

Unfortunately, I just found out the platform for using the script will
be IronPython, not Python. So, importing calendar in IronPython gives
me:


SyntaxError: future feature is not defined: with_statement (c:
\Python25\Lib\calendar.py, line 8)

so, compatibility issue.

I'm extremely grateful for everyone's help so far and I have moved my
question over to an IronPython group, but does anyone happen to know
of a way to make this work with IronPython?

Thanks!
-Ed

John Machin

unread,
Apr 23, 2007, 6:26:22 PM4/23/07
to

Look, Ma, no imports, no function calls, no attribute lookups, no
table lookups :-)

def days_in_month(year, month):
assert 1 <= month <= 12
if month >= 3:
return (month * 13 + 1) // 5 - (month * 13 + 3) // 5 + 31
if month == 1:
return 31
if year % 4: return 28
if year % 100: return 29
if year % 400: return 28
return 29

| >>> [days_in_month(yyyy, 2) for yyyy in (2007, 2008, 2100, 2000)]
| [28, 29, 28, 29]
| >>> [days_in_month(2007, mm) for mm in range(1, 13)]
| [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Gabriel Genellina

unread,
Apr 23, 2007, 9:00:35 PM4/23/07
to
En Mon, 23 Apr 2007 17:28:11 -0300, edfialk <edf...@gmail.com> escribió:

> Alex, very nice. That should be good enough for me.
> The rest of you as well, thanks for all the help.
>
> I, unfortunately, failed to realize the actual platform the script is
> for is IronPython. When trying to import calendar in IronPython, I
> get:
>
> SyntaxError: future feature is not defined: with_statement (c:
> \Python25\Lib\calendar.py, line 8)

You could download and use the calendar.py from the 2.4 installation.

--
Gabriel Genellina

Alex Martelli

unread,
Apr 23, 2007, 10:20:03 PM4/23/07
to
edfialk <edf...@gmail.com> wrote:

Just get calendar.py from Python 2.4 instead, e.g.
<http://svn.python.org/view/*checkout*/python/branches/release24-maint/L
ib/calendar.py?rev=38868> .


Alex

sk...@pobox.com

unread,
Apr 23, 2007, 9:04:25 PM4/23/07
to edfialk, pytho...@python.org

ed> I, unfortunately, failed to realize the actual platform the script
ed> is for is IronPython. When trying to import calendar in IronPython,
ed> I get:

ed> SyntaxError: future feature is not defined: with_statement
ed> (c:\Python25\Lib\calendar.py, line 8)

So try the 2.4 version of calendar.py...

Skip

0 new messages