script for seconds in given month?

Shane Geiger sgeiger at ncee.net
Mon Apr 16 15:42:29 EDT 2007


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)





skip at 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
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070416/157455f0/attachment.vcf>


More information about the Python-list mailing list