script for seconds in given month?

John Machin sjmachin at lexicon.net
Mon Apr 23 18:26:22 EDT 2007


On Apr 24, 6:39 am, Carsten Haese <cars... at uniqsys.com> wrote:
> On Mon, 2007-04-23 at 13:28 -0700, edfialk wrote:
> > 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. :)
>
> 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
>

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]




More information about the Python-list mailing list