ProSoft Technology ProSoft i-View User Manual
Page 58

Data Source Files
ProSoft i-View ♦ Mobile Process Monitoring and Control Application
User Manual
Version 2.0.2
Page 58 of 106
ProSoft Technology, Inc.
September 29, 2011
Putting it all together. Advanced Expressions Examples
Converting an arbitrary number of seconds to hh:mm:ss format
The following expression shows how to get a string in the form ‘hh:mm:ss’ from a numeric
value containing seconds.In this example x contains the total number of seconds to be
converted to the desired format.
value = [(x/3600).to_s("%02d"), ((x%3600)/60).to_s("%02d"), (x%60).to_s("%02d")].join(":") ;
The operators % and / are used to calculate hours, minutes, seconds as numeric values.
These are then truncated to integer with the to_i method and successively converted to
formatted strings with to_s. The resulting individual strings are embedded into an array
and then joined by means of the the join method using ‘:’ as separator.
Given the following two rows:
x
DINT
HR1
label = "Total Seconds";
t
STRING INTERNAL label = "Time"; value =
[(x/3600).to_s("%02d"),
((x%3600)/60).to_s("%02d"),
(x%60).to_s("%02d")].join(":") ;
when HR1 holds 3661, which is 3600 seconds (1 hour) + 60 seconds (1 minute) + 1
second,
the second row will display 01:01:01
Instead of using the join method we could have used the format function in a posibly
more convenient way. Consider the following:
value = format("%02d:%02d:%02d”, x/3600, (x%3600)/60, x%60) ;
in this case the format specifiers in the format string are just replaced with the relevant
time values.
Calculating seconds from a string having the hh:mm:ss format.
Just to illustrate what expressions allow to do let’s try now to get the original seconds
value from a string already in the hh:mm:ss format. To do so we can use the following
expression:
value=3600*t.split(":").fetch(-3,0).to_i + 60*t.split(":").fetch(-2,0).to_i + t.split(":").fetch(-1,0).to_i;