Difficulty in importing dates and times with functions like

  • Follow


Dear group, I have a difficulty in interpreting dates and times with
functions like AbsoluteTime.

When a date string is in the format that Mathematica understands
automatically, the fractions of second are appropriately read:

In:= AbsoluteTime["2008/12/01 12:34:56.454"]
Out= 3.43712369645400000000000*10^9

Instead, when the format has to be specifies, the fractions of second
are ignored and I could not find a way to prevent this:

In:= AbsoluteTime[{"2008-12-01 12.34.56.454",  {"Year", "Month",
"Day", "Hour", "Minute", "Second"} }]
Out= 3437123696

This happens both in versions 6 and 7 for Windows. Does anyone know
how to make this work ?

Moreover, date importing functions appear to me very slow and very
clumsy in their syntax. Does know anyone about something better which
can be applied?

Regards

Al

0
Reply alberto.dilullo (79) 12/11/2008 8:48:07 AM

abstime[t_String] := AbsoluteTime[ToExpression[StringSplit[t, "-" | "
" | ".", 6]]];

Do[r1 = abstime["2008-12-01 12.34.56.454"], {10000}] // Timing
==> 0.2 sec

Do[r2 = AbsoluteTime[{"2008-12-01 12.34.56.454", {"Year", "Month",
"Day", "Hour", "Minute", "Second"}}], {10000}] // Timing ==> 27 sec

Floor[r1] == r2  ==> True

0
Reply raffy (116) 12/11/2008 12:28:28 PM


Thank you for the good suggestion. I also noticed that regular
expressions are quite faster than string patterns.
On my pc (notice ten times more evaluations than in your example):

In[]:= abstime[t_String] :=  AbsoluteTime[ToExpression[StringSplit[t,
"-" | " " | ".", 6]]];
In[]:= Do[r1 = abstime["2008-12-01 12.34.56.454"], {100000}] // Timing
Out[]= {3.869,Null}

while

In[]:= abstime[t_String] := ToExpression[StringSplit[t,
RegularExpression["[- .]"], 6]];
In[]:= Do[ r1 = abstime["2008-12-01 12.34.56.454"], {100000}] //
Timing
Out[]= {2.823,Null}


Thanks
Al


On 11 Dic, 13:28, Raffy <ra...@mac.com> wrote:
> abstime[t_String] := AbsoluteTime[ToExpression[StringSplit[t, "-" | "
> " | ".", 6]]];
>
> Do[r1 = abstime["2008-12-01 12.34.56.454"], {10000}] // Timing
> ==> 0.2 sec
>
> Do[r2 = AbsoluteTime[{"2008-12-01 12.34.56.454", {"Year", "Month",
> "Day", "Hour", "Minute", "Second"}}], {10000}] // Timing ==> 27 sec
>
> Floor[r1] == r2  ==> True


0
Reply alberto.dilullo (79) 12/12/2008 11:57:50 AM

2 Replies
25 Views

(page loaded in 0.078 seconds)


Reply: