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
monthDays={'Jan':31,'Feb':28, ..}
secs=60*60*24*monthDays[thisMonth]
if (thisMonth=='Feb'
and isLeap(thisYear)):
secs+=60*60*24
return secs
?
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
Probably won't work if month==12. ;-)
Skip
Do you need to handle leap seconds too? (not a joke)
-- Paul
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
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
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
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
import calendar
def seconds_in_month(month, year):
nomatter, daysinmonth = calendar.monthrange(year, month)
return daysinmonth * 24 * 60 * 60
Alex
>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
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.
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
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
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
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
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]
> 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
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
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