2

An absolute time is given in stored in this format:

time = "000:03:07.447"

How can this string be converted to seconds in an elegant way?

Update:

As suggested by Harper89

3600*#1 + 60*#2 + #3 & @@ ToExpression[StringSplit["000:00:04.424", ":"]]

Szabolcs suggested to use AbsoluteTime From Mathematica help: AbsoluteTime gives the total number of seconds since the beginning of January 1, 1900, in your time zone.

AbsoluteTime[{"000:03:07.447", {"Hour", ":", "Minute", ":", "Second", 
".", "Millisecond"}}] 
-  AbsoluteTime[{"000:00:00.000", {"Hour", ":", "Minute", ":", "Second",
 ".", "Millisecond"}}]

This works both

1
  • 1
    It seems AbsoluteTime has changed between versions 7 and 8.
    – Szabolcs
    Feb 14, 2012 at 15:20

3 Answers 3

4

Just as harper89 described, in Mathematica:

FromDigits[ToExpression /@ StringSplit[time, ":"], 60]
0
4

Parse the string to form an array between each of the : using something like split()

  1. For the first set multiply by the number of seconds in an hour
  2. For the second set multiply by the number of seconds in a minute
  3. For the third set add the number to the total

In other words

totalseconds = array(0)*3600 + array(1)*60 + array(2)

Or in vb.net Code

  Dim time As String = "000:3:7"        
  Dim a() As String  
  a = longstring.Split(":")
  Dim TotalSeconds as Integer = (a(0) * 86400) + (a(1) * 3600) + a(2))
  Trace.WriteLine(TotalSeconds.toString)

From the Tag definition of mathmatica

Not to be confused with mathematics (math).

OOPs..

2
  • 3
    @Szabolcs you cannot blame him; one may assume that all Mathematica questions will be posted where they belong :-)
    – Mr.Wizard
    Feb 14, 2012 at 15:11
  • 1
    Ahh, I read it as he was looking for techniques that could be used. He edited and I understand exactly what he means now. My fault. (But hey now he knows to split!)
    – sealz
    Feb 14, 2012 at 15:17
3

Try the following:

AbsoluteTime[{"000:03:07.447", 
      {"Hour", ":", "Minute", ":", "Second", ".", "Millisecond"}}]

(* ==> 187.447 *)

The key was giving an explicit date format (see the docs of DateList[])

This solution works in Mathematica 8. It appears that in version 7 (and possibly also in version 6) one needs to correct the result, like this:

AbsoluteTime[{"000:03:07.447", 
      {"Hour", ":", "Minute", ":", "Second", ".", "Millisecond"}}] -
AbsoluteTime[{"0", {"Hour"}}]
4
  • I don't think he wants the Mathematica epoch for this, but his own.
    – Mr.Wizard
    Feb 14, 2012 at 15:09
  • @Mr.Wizard My code converts it to seconds correctly, just try it. I am aware that Mathematica considers 1900 january 1 as start of all time, but this is not an issue here.
    – Szabolcs
    Feb 14, 2012 at 15:10
  • This returns 3.534364987447000000000000*10^9 -- I think he simply wants 187.447.
    – Mr.Wizard
    Feb 14, 2012 at 15:12
  • @Mr.Wizard I get 187.447. Did we come across a version difference again? Let's go to chat
    – Szabolcs
    Feb 14, 2012 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.