time conversions [hh:mm:ss.ms <-> sec(.ms)

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

    #1

    time conversions [hh:mm:ss.ms <-> sec(.ms)

    Hi,

    I was looking at python & datetime and hoping that it would already
    have
    a method/func to translate time formats. I need to translate seconds to
    hh:mm:ss.ms and vice versa and would like the ability to do some basic
    arithmetic in these formats. I think that there just has to be a
    package
    or module out there that already does this with reasonable speed and
    accuracy.

    Sadly, i do indeed need both reasonable speed and accuracy since i will
    be adding up huge masses of small events (around 10 to 60 ms
    milliseconds
    in length) to create cloud like textures for real time audio/video at
    high sampling rates.

    i googled for hh:mm:ss.ms + python and didn't find much...

    best,

    -kp---

    [mac os x w/ python 2.4.1]

  • John Machin

    #2
    Re: time conversions [hh:mm:ss.ms &lt;-&gt; sec(.ms)

    On 28/04/2006 6:39 PM, kpp9c wrote:[color=blue]
    > Hi,
    >
    > I was looking at python & datetime and hoping that it would already
    > have
    > a method/func to translate time formats. I need to translate seconds to
    > hh:mm:ss.ms and vice versa and would like the ability to do some basic
    > arithmetic in these formats.[/color]

    Could you please be a little more specific? I guess that you have
    seconds-and-a-fraction in a float eg 3723.456 seconds, which is
    equivalent to 1 hour, 2 minutes and 3.456 seconds. How do you want that
    represented? You say hh:mm:ss.ms which could be interpreted as a string
    "01:02:03.4 56" -- but this format is not very useful for "basic
    arithmetic". OTOH a tuple representation like (1, 2, 3.456) can
    accommodate "arithmetic " of some sort or other more easily -- is that
    what you had in mind? Next question: exactly what basic arithmetic
    operations do you want to do in the hour-minute-second format, and why
    do you want to do them in that format, and not the seconds-only format?
    [color=blue]
    > I think that there just has to be a
    > package
    > or module out there that already does this with reasonable speed and
    > accuracy.[/color]

    Get the specification right first. Get the accuracy right second. Then
    worry about the speed. The "arithmetic " of which you speak can't be so
    mind-boggling that you can't write it in Python and test it yourself.
    You may find the specification changes under the influence of the
    implementation :-)
    [color=blue]
    >
    > Sadly, i do indeed need both reasonable speed and accuracy since i will
    > be adding up huge masses of small events (around 10 to 60 ms
    > milliseconds
    > in length) to create cloud like textures for real time audio/video at
    > high sampling rates.[/color]

    So why not keep it in seconds (or milliseconds)?

    Have you considered an extension, using C or Pyrex?
    [color=blue]
    >
    > i googled for hh:mm:ss.ms + python and didn't find much...
    >
    > best,
    >
    > -kp---
    >
    > [mac os x w/ python 2.4.1][/color]

    Comment

    • Dave Hughes

      #3
      Re: time conversions [hh:mm:ss.ms &lt;-&gt; sec(.ms)

      kpp9c wrote:
      [color=blue]
      > Hi,
      >
      > I was looking at python & datetime and hoping that it would already
      > have
      > a method/func to translate time formats. I need to translate seconds
      > to hh:mm:ss.ms and vice versa and would like the ability to do some
      > basic arithmetic in these formats. I think that there just has to be a
      > package
      > or module out there that already does this with reasonable speed and
      > accuracy.
      >
      > Sadly, i do indeed need both reasonable speed and accuracy since i
      > will be adding up huge masses of small events (around 10 to 60 ms
      > milliseconds
      > in length) to create cloud like textures for real time audio/video at
      > high sampling rates.
      >
      > i googled for hh:mm:ss.ms + python and didn't find much...
      >
      > best,
      >
      > -kp---
      >
      > [mac os x w/ python 2.4.1][/color]

      Hmmm ... not difficult to do, but it's the "speed" bit that's tricky.
      Here's quick and dirty implementation, but I wouldn't place any bets on
      it being fast (or accurate enough for that matter!)

      #!/bin/env python
      # vim: set noet sw=4 ts=4:

      import datetime

      def secs2time(s):
      ms = int((s - int(s)) * 1000000)
      s = int(s)
      # Get rid of this line if s will never exceed 86400
      while s >= 24*60*60: s -= 24*60*60
      h = s / (60*60)
      s -= h*60*60
      m = s / 60
      s -= m*60
      return datetime.time(h , m, s, ms)

      def time2secs(d):
      return d.hour*60*60 + d.minute*60 + d.second + \
      (float(d.micros econd) / 1000000)

      if __name__ == "__main__":
      for i in (80000.123, 0.0, 5873, 12345.6789):
      print "%12.6f -> %15s -> %12.6f" % (
      i,
      secs2time(i),
      time2secs(secs2 time(i))
      )
      assert i == time2secs(secs2 time(i))
      from timeit import Timer
      timer = Timer(
      setup="from __main__ import time2secs, secs2time",
      stmt="time2secs (secs2time(1234 5.6789))")
      count = 1000000
      total = timer.timeit(co unt)
      print "Time for %d invocations: %.10fs" % (count, total)
      print "Time per invocation: %.10fs" % (total / count,)


      And the output:

      80000.123000 -> 22:13:20.123000 -> 80000.123000
      0.000000 -> 00:00:00 -> 0.000000
      5873.000000 -> 01:37:53 -> 5873.000000
      12345.678900 -> 03:25:45.678900 -> 12345.678900
      Time for 1000000 invocations: 9.3959178925s
      Time per invocation: 0.0000093959s


      Huh ... what d'ya know ... good thing I ain't a betting man!


      Dave.

      --

      Comment

      • Faber

        #4
        Re: time conversions [hh:mm:ss.ms &lt;-&gt; sec(.ms)

        kpp9c wrote:
        [color=blue]
        > I was looking at python & datetime and hoping that it would already
        > have a method/func to translate time formats. I need to translate seconds
        > to hh:mm:ss.ms and vice versa and would like the ability to do some basic
        > arithmetic in these formats.[/color]

        Have a look at datetime.timede lta:

        from datetime import timedelta

        seconds_value = 4237.63
        td = timedelta(secon ds=seconds_valu e)

        print td # Shows 1:10:37.630000
        print td.seconds # Shows 4237

        other_td = td + timedelta(secon ds=13)

        print other_td # Shows 1:10:50.630000
        print other_td.second s # Shows 4250
        [color=blue]
        > I think that there just has to be a package or module out there that
        > already does this with reasonable speed and accuracy.[/color]

        The accuracy seems perfect, don't know about speed - take some test :)

        Regards

        --
        Faber

        Get real-time visibility into your key parking metrics, and make data-driven decisions to optimise occupancy, pricing, and revenue. Contact our team today.


        We live in a society exquisitely dependent on science and technology,
        in which hardly anyone knows anything about science and technology. -- Carl
        Sagan

        Comment

        • kpp9c

          #5
          Re: time conversions [hh:mm:ss.ms &lt;-&gt; sec(.ms)

          timedelta looks to be just the ticket! bravo, thank you... i guess this
          must be pretty new to Python. Nice... more "batteries included"
          stuff...

          cheers,

          -kevin--

          Comment

          • Faber

            #6
            Re: time conversions [hh:mm:ss.ms &lt;-&gt; sec(.ms)

            kpp9c wrote:
            [color=blue]
            > timedelta looks to be just the ticket! bravo, thank you... i guess this
            > must be pretty new to Python.[/color]

            Well, since Python 2.3 (July 2003):


            :-)

            --
            Faber

            Get real-time visibility into your key parking metrics, and make data-driven decisions to optimise occupancy, pricing, and revenue. Contact our team today.


            Only wimps use tape backup: _real_ men just upload their important stuff on
            ftp and let the rest of the world mirror it  -- Linus Torvalds

            Comment

            Working...