Forum OpenACS Q&A: how to subtract very large integers in tcl

i have to subtract a value stored in variable $newtime whose value is "20040702164532" from the variable $filedt2 whose value is "20030304138704".
to execut i am writing the command

".......
set datedif [expr $newtime - $filedt2]
......."

but upon execution it is giving an error .....
"integer value too large to represent
  while executing "expr $newtime - $filedt2"
......

I am a newbie to tcl programming.Any guidance from your side
will be of great help to me.
thanks
abhishek

Collapse
Posted by Ola Hansson on
You could try:

append newtime e0
# ...or...
# append newtime .
set datedif [expr $newtime - $filedt2]

See: http://wiki.tcl.tk/567

Collapse
Posted by russ m on
are these actually plain integers, or are they datestamps? if they're datestamps then doing integer subtraction won't give you meaningful results... for example,

 20040101120000 (noon on Jan 1, 2004)
-20031231120000 (noon on Dec 31, 2003)
=    8870000000 (88 months and 70 days)

you need to treat them as dates if that's what they are...

Collapse
Posted by abhishek kk on
these are actually datestamps!!
but is there any built-in tcl function for date subtraction???
if so do let me know!!
abhishek
Collapse
Posted by russ m on
if you format the timestamps as ISO 8601 point-in-time strings by inserting a "T" between the day and hours (eg "20040101T120000") then you can use [clock scan] to convert to the number of seconds since the epoch and calculate the difference there.

% expr [clock scan "20040101T120000"] - [clock scan "20031231T120000"]
86400

gives you the correct number of seconds between these timestamps.

Collapse
Posted by Niranjan Nandakumar on
try out mpexpr instead of expr ... but u need to include the package "Mpexpr" first !