Time

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

    #1

    Time

    I need to convert the string below into epoch seconds so that I can
    perform substractions and additions. I assume I will need to break it
    up into a time_t struct and use mktime. Two questions if you will
    please:

    Is there a way to use multiple separator characters for split similar
    to awk's [|] style?

    Could you point to an example of a python time_t struct?

    05/11/2007 15:30

    Thanks,

    jvh

  • HMS Surprise

    #2
    Re: Time

    >
    Could you point to an example of a python time_t struct?
    >
    Or maybe that should be a tm struct???

    Comment

    • HMS Surprise

      #3
      Re: Time

      Sorry, reading a little closer I see that the time tuple is apparently
      an ordinary list.

      jvh

      Comment

      • John Machin

        #4
        Re: Time

        On May 12, 7:46 am, HMS Surprise <j...@datavoice int.comwrote:

        [first message]

        HS ==I need to convert the string below into epoch seconds so that I
        can
        perform substractions and additions.

        JM ==I presume you mean "seconds since the epoch". You don't need to
        do that.

        HS ==I assume I will need to break it
        up into a time_t struct and use mktime.

        JM ==You assume wrongly. The time module exists (IMVHO) solely as a
        crutch for people who are converting C etc code that uses the time.h
        functions from the C standard library. If you are starting off from
        scratch, use the Python datetime module -- especially if you need to
        store and manipulate pre-1970 dates; e.g. the date of birth of anyone
        aged more than about 37.5 years :-)

        HS ==Two questions if you will
        please:

        Is there a way to use multiple separator characters for split similar
        to awk's [|] style?

        JM ==Only if you can find such a way in the manual.

        HS ==Could you point to an example of a python time_t struct?

        JM ==Python doesn't have that; it's a C concept


        HS ==05/11/2007 15:30

        [second message]

        HS== Could you point to an example of a python time_t struct?

        Or maybe that should be a tm struct???

        JM ==See previous answer.

        [third message]

        HS ==Sorry, reading a little closer I see that the time tuple is
        apparently
        an ordinary list.

        JM ==Huh? A tuple is a tuple. A tuple is not a list, not even a very
        extraordinary one.

        If you are desperate to use the time module, try this:
        >>import time
        >>s = "05/11/2007 15:30"
        >>fmt = "%m/%d/%Y %H:%M"
        # Given the current date, I'm presuming that your example indicates
        that you adhere to the "month-first-contrary-to-common-sense"
        religion :-)
        >>time.strptime (s, fmt)
        (2007, 5, 11, 15, 30, 0, 4, 131, -1)

        otherwise:
        >>import datetime
        >>d1 = datetime.dateti me.strptime(s, fmt)
        >>d1
        datetime.dateti me(2007, 5, 11, 15, 30)
        >>d2 = datetime.dateti me(2007, 5, 1)
        >>d2
        datetime.dateti me(2007, 5, 1, 0, 0)
        >>delta = d1 - d2
        >>delta
        datetime.timede lta(10, 55800)
        >>days_diff = delta.days + delta.seconds / 60. / 60. / 24.
        >>days_diff
        10.645833333333 334

        Do read the datetime module documentation for more info ... in
        particular the timedelta object has a microseconds attribute; in
        general there is a whole heap of functionality in there.

        HTH,
        John

        Comment

        • HMS Surprise

          #5
          Re: Time

          Thanks for posting. I sure am sorry that I wasted your time. I should
          have started the post stating I am using jython 2.2.3 and apparently
          it has no datetime module. But I will keep datetime in mind for future
          reference.

          Since I had no datetime I cobbled out the following. Seems to work
          thus far. Posted here for the general amusement of the list.

          Regards,

          jvh

          ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~

          from time import *
          s = '05/11/2007 1:23 PM'
          t = s.split()
          mdy = t[0].split('/')

          hrMn = t[1].split(':')
          if t[2] == 'PM':
          hrMn[0] = int(hrMn[0]) + 12

          tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
          0,0,0,0)
          print tuple

          eTime = mktime(tuple)
          print 'eTime', eTime

          Comment

          • kyosohma@gmail.com

            #6
            Re: Time

            On May 14, 9:00 am, HMS Surprise <j...@datavoice int.comwrote:
            Thanks for posting. I sure am sorry that I wasted your time. I should
            have started the post stating I am using jython 2.2.3 and apparently
            it has no datetime module. But I will keep datetime in mind for future
            reference.
            >
            Since I had no datetime I cobbled out the following. Seems to work
            thus far. Posted here for the general amusement of the list.
            >
            Regards,
            >
            jvh
            >
            ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
            >
            from time import *
            s = '05/11/2007 1:23 PM'
            t = s.split()
            mdy = t[0].split('/')
            >
            hrMn = t[1].split(':')
            if t[2] == 'PM':
            hrMn[0] = int(hrMn[0]) + 12
            >
            tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
            0,0,0,0)
            print tuple
            >
            eTime = mktime(tuple)
            print 'eTime', eTime
            Since jython works with Java, why not use Java's time/datetime
            modules? Various links abound. Here are a few:





            Maybe those will give you some hints.

            Mike

            Comment

            • HMS Surprise

              #7
              Re: Time

              if t[2] == 'PM':
              hrMn[0] = int(hrMn[0]) + 12
              >

              Oops, should be:
              hrMn[0] = int(hrMn[0]
              if t[2] == 'PM':
              hrMn[0] += 12


              Comment

              • HMS Surprise

                #8
                Re: Time

                On May 14, 9:09 am, kyoso...@gmail. com wrote:
                On May 14, 9:00 am, HMS Surprise <j...@datavoice int.comwrote:
                >
                >
                >
                Thanks for posting. I sure am sorry that I wasted your time. I should
                have started the post stating I am using jython 2.2.3 and apparently
                it has no datetime module. But I will keep datetime in mind for future
                reference.
                >
                Since I had no datetime I cobbled out the following. Seems to work
                thus far. Posted here for the general amusement of the list.
                >
                Regards,
                >
                jvh
                >
                ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~
                >
                from time import *
                s = '05/11/2007 1:23 PM'
                t = s.split()
                mdy = t[0].split('/')
                >
                hrMn = t[1].split(':')
                if t[2] == 'PM':
                hrMn[0] = int(hrMn[0]) + 12
                >
                tuple =(int(mdy[2]), int(mdy[0]), int(mdy[1]), hrMn[0], int(hrMn[1]),
                0,0,0,0)
                print tuple
                >
                eTime = mktime(tuple)
                print 'eTime', eTime
                >
                Since jython works with Java, why not use Java's time/datetime
                modules? Various links abound. Here are a few:
                >
                http://www.raditha.com/blog/archives...erence-in-date...
                >
                Maybe those will give you some hints.
                >
                Mike
                Excellent idea.

                Thanks Mike.


                jvh

                Comment

                • HMS Surprise

                  #9
                  Re: Time

                  On May 14, 9:22 am, HMS Surprise <j...@datavoice int.comwrote:
                  if t[2] == 'PM':
                  hrMn[0] = int(hrMn[0]) + 12
                  >
                  Oops, should be:
                  hrMn[0] = int(hrMn[0]
                  if t[2] == 'PM':
                  hrMn[0] += 12
                  Oops +=1, should be:
                  hrMn[0] = int(hrMn[0]
                  if t[2] == 'PM':
                  hrMn[0] += 12

                  Need more starter fluid, coffee please!!!

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: Time

                    En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <john@datavoice int.com>
                    escribió:
                    On May 14, 9:22 am, HMS Surprise <j...@datavoice int.comwrote:
                    >
                    Oops +=1, should be:
                    hrMn[0] = int(hrMn[0]
                    if t[2] == 'PM':
                    hrMn[0] += 12
                    >
                    Need more starter fluid, coffee please!!!
                    Still won't work for 12 AM nor 12 PM...

                    --
                    Gabriel Genellina

                    Comment

                    • Grant Edwards

                      #11
                      Re: Time

                      On 2007-05-14, Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
                      En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <john@datavoice int.com>
                      escribió:
                      >
                      >On May 14, 9:22 am, HMS Surprise <j...@datavoice int.comwrote:
                      >>
                      >Oops +=1, should be:
                      > hrMn[0] = int(hrMn[0]
                      > if t[2] == 'PM':
                      > hrMn[0] += 12
                      >>
                      >Need more starter fluid, coffee please!!!
                      >
                      Still won't work for 12 AM nor 12 PM...
                      Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
                      do they?

                      --
                      Grant Edwards grante Yow! HELLO, everybody,
                      at I'm a HUMAN!!
                      visi.com

                      Comment

                      • Grant Edwards

                        #12
                        Re: Time

                        On 2007-05-14, Grant Edwards <grante@visi.co mwrote:
                        On 2007-05-14, Gabriel Genellina <gagsl-py2@yahoo.com.a rwrote:
                        >En Mon, 14 May 2007 11:27:35 -0300, HMS Surprise <john@datavoice int.com>
                        >escribió:
                        >>
                        >>On May 14, 9:22 am, HMS Surprise <j...@datavoice int.comwrote:
                        >>>
                        >>Oops +=1, should be:
                        >> hrMn[0] = int(hrMn[0]
                        >> if t[2] == 'PM':
                        >> hrMn[0] += 12
                        >>>
                        >>Need more starter fluid, coffee please!!!
                        >>
                        >Still won't work for 12 AM nor 12 PM...
                        >
                        Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
                        do they?



                        --
                        Grant Edwards grante Yow! I am a jelly donut.
                        at I am a jelly donut.
                        visi.com

                        Comment

                        • Gabriel Genellina

                          #13
                          Re: Time

                          En Mon, 14 May 2007 16:20:27 -0300, Grant Edwards <grante@visi.co m>
                          escribió:
                          >>>On May 14, 9:22 am, HMS Surprise <j...@datavoice int.comwrote:
                          >>>>
                          >>>Oops +=1, should be:
                          >>> hrMn[0] = int(hrMn[0]
                          >>> if t[2] == 'PM':
                          >>> hrMn[0] += 12
                          >>>>
                          >>>Need more starter fluid, coffee please!!!
                          >>>
                          >>Still won't work for 12 AM nor 12 PM...
                          >>
                          >Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
                          >do they?
                          >
                          http://www.astronomy.net/articles/13/
                          No. Even ignoring that exact instant at Noon or Midnight, 12:01 PM
                          translates into itself, 12:01, on a 24 hr clock; and 12:01 AM becomes 0:01
                          on a 24 hr clock. For hours between 01 PM and 11 PM, yes, you can follow
                          the rule "add 12".

                          --
                          Gabriel Genellina

                          Comment

                          • HMS Surprise

                            #14
                            Re: Time

                            >
                            Do you mean 12 Noon or 12 Midnight? 12AM and 12PM don't exist,
                            do they?
                            >
                            >>t = (2007, 5, 14, 12, 0,0,0,0,0)
                            >>strftime('%p' , t)
                            'PM'
                            >>t = (2007, 5, 14, 0,0,0,0,0,0)
                            >>strftime('%p' , t)
                            'AM'
                            >>>

                            Comment

                            Working...