script for seconds in given month?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • edfialk

    script for seconds in given month?

    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

    #2
    Re: script for seconds in given month?

    On Apr 16, 12:22 pm, "edfialk" <edfi...@gmail. comwrote:
    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.
    Probably there are sophisticated answers, but have you tried
    something like:

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

    ?

    Comment

    • pythoncurious@gmail.com

      #3
      Re: script for seconds in given month?

      On Apr 16, 6:22 pm, "edfialk" <edfi...@gmail. comwrote:
      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,mo nth,1,0,0,0,0,0 ,-1))
      s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
      return s2-s1

      /Matt

      Comment

      • skip@pobox.com

        #4
        Re: script for seconds in given month?


        Mattfrom time import mktime
        Mattdef secondsInMonth( year, month):
        Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
        Matt s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
        Matt return s2-s1

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

        Skip

        Comment

        • Paul McGuire

          #5
          Re: script for seconds in given month?

          On Apr 16, 11:22 am, "edfialk" <edfi...@gmail. comwrote:
          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
          Do you need to handle leap seconds too? (not a joke)

          -- Paul

          Comment

          • edfialk

            #6
            Re: script for seconds in given month?

            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

            Comment

            • attn.steven.kuo@gmail.com

              #7
              Re: script for seconds in given month?

              On Apr 16, 10:18 am, s...@pobox.com wrote:
              Mattfrom time import mktime
              Mattdef secondsInMonth( year, month):
              Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
              Matt s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
              Matt return s2-s1
              >
              Probably won't work if month==12. ;-)
              >
              Skip


              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


              Comment

              • Shane Geiger

                #8
                Re: script for seconds in given month?


                import datetime

                def first_day_of_ne xt_month( year, month ):
                """returns the first day of the next month
                >>first_day_of_ next_month(2007 ,5)
                datetime.dateti me(2007, 6, 1, 0, 0)
                >>first_day_of_ next_month(2007 ,12)
                datetime.dateti me(2008, 1, 1, 0, 0)
                """
                oneday = datetime.timede lta(days=1)
                day = datetime.dateti me(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,mo nth,1,0,0,0,0,0 ,-1))
                day = first_day_of_ne xt_month(year, month)
                year = day.year
                month = day.month
                s2 = mktime((year,mo nth+1,1,0,0,0,0 ,0,-1))
                return s2-s1


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





                skip@pobox.com wrote:
                Mattfrom time import mktime
                Mattdef secondsInMonth( year, month):
                Matt s1 = mktime((year,mo nth,1,0,0,0,0,0 ,-1))
                Matt s2 = mktime((year,mo nth+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@ncee.ne t | 402-438-8958 | http://www.ncee.net

                Leading the Campaign for Economic and Financial Literacy


                Comment

                • Paul McGuire

                  #9
                  Re: script for seconds in given month?

                  On Apr 16, 12:49 pm, "edfialk" <edfi...@gmail. comwrote:
                  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
                  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

                  Comment

                  • Alex Martelli

                    #10
                    Re: script for seconds in given month?

                    edfialk <edfialk@gmail. comwrote:
                    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.
                    import calendar

                    def seconds_in_mont h(month, year):
                    nomatter, daysinmonth = calendar.monthr ange(year, month)
                    return daysinmonth * 24 * 60 * 60


                    Alex

                    Comment

                    • Hendrik van Rooyen

                      #11
                      Re: script for seconds in given month?

                      "Paul McGuire" <pt...g@austin. rr.comwrote:
                      On Apr 16, 11:22 am, "edfialk" <edfi...@gmail. comwrote:
                      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
                      >
                      Do you need to handle leap seconds too? (not a joke)
                      >
                      -- Paul
                      >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

                      Comment

                      • pythoncurious@gmail.com

                        #12
                        Re: script for seconds in given month?

                        On Apr 17, 6:41 am, a...@mac.com (Alex Martelli) wrote:
                        edfialk <edfi...@gmail. comwrote:
                        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_mont h(month, year):
                        nomatter, daysinmonth = calendar.monthr ange(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.



                        Comment

                        • pythoncurious@gmail.com

                          #13
                          Re: script for seconds in given month?

                          On Apr 16, 7:49 pm, "edfialk" <edfi...@gmail. comwrote:
                          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

                          Comment

                          • edfialk

                            #14
                            Re: script for seconds in given month?

                            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\c alendar.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

                            Comment

                            • Carsten Haese

                              #15
                              Re: script for seconds in given month?

                              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\c alendar.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_mont h(mo,yr):
                              import datetime
                              start_date = datetime.date(y r,mo,1)
                              mo += 1
                              if mo==13: mo=1; yr += 1
                              end_date = datetime.date(y r,mo,1)
                              delta = end_date - start_date
                              return 24*60*60*delta. days

                              -Carsten


                              Comment

                              Working...