The following LiveCode function I written works well for me.
I included commented out optional code for the following:
OPTIONAL CODE #1: Round up seconds by one if decimal > 0.4
OPTIONAL CODE #2: Threshold to throw an error message if user exceeds the threshold value.
OPTIONAL CODE #3: Code to include number of days in the Time String. Adds a DAY column. Format D:H:M:S
NOTE: Optional Code three is in two parts (A) and (B) and are used together.
OPTIONAL CODE #4: Code to build a Time String formatted without a DAY column. Format H:M:S
Default settings are
Remove all commas from input before algorithms are ran.
No Rounding Up
Format is H:M:S
No Threshold
NOTICE #1 Comments are preceded with a # while commented out code is preceded with a --
NOTICE #2 I have repeated the code again after screenshots with minimum comments so you have your choice on how you wish to view/copy the code.
BEGIN CODE EXAMPLE
(with comments)
function hms s #Function to convert seconds into Hours:Minutes:Seconds format.
set itemdelimiter to "." # Set the item delimiter to a period. Used to separate integers (whole numbers) and decimals.
replace comma with empty in s # Remove any commas that the user may have inserted into the number.
# CODE CLEANUP [BEGIN] - NOTICE: this code may not be required if input is from a subroutine instead of a user.
# Checks to see if input is a number. If not then loop through each character to remove any nonconforming characters
if isNumber(s) is false then # Test if s is a true numeral, if not then…
repeat with i = 1 to number of chars in s # Loop through each character in s to sift out non numerals
put char i of s into ckNum # Put a single character into the variable to run tests on
if ckNum is "." then # If character is a decimal point then…
put ckNum after tmp # Keep decimal point
end if
if isNumber(ckNum) is true then # If character is a decimal point then…
put ckNum after tmp # Keep Number
end if
end repeat
put tmp into s # Replace user corrupted seconds with cleaned up version
end if
# CODE CLEANUP [END]
# OPTIONAL CODE #1 [BEGIN] -
# This code gives you the choice to remove the decimal or round it up or not.
# Remove the -- to round up seconds to next second if a decimal > 0.4 is encountered
-- put item 2 of s into d
-- if char 1 of d > 4 then
-- add 1 to item 1 of s
-- end if
# OPTIONAL CODE #1 [END]
if s is empty then # Check to see if s is an empty string
return "00:00:00" # If s is an empty string then return "00:00:00" formatted string.
end if
if s < 60 then # If seconds is less than a minute then return format 00:00:seconds
put item 1 of s into s # Keep only the integer or whole number
return "00:00:" & pad2(s) # Return the finished formatted time string
end if
#OPTIONAL CODE #2 [BEGIN] - ERROR display for exceeding a set threshold
# THRESHOLD SETTING, This code evaluates the number of seconds and if it exceeds the threshold then return an 'ERROR' message
-- if s > 359999 then
-- return "ERROR"
-- end if
# Second values can be whatever threshold you want. For example:
# Greater than: 85399 for one day. 86,400 seconds is equivalent to 24 hours or 24:00:00 (One day)
# Greater than: 89999 which is equivalent to 24:59:59
# Greater than: 359999 which is equivalent for 99:59:99 the max for a 00:00:00 format
# Greater than: 8639999 which is equivalent to 99:23:59:59. (99 days, 23 hours, 59 minutes and 59 seconds) The maximum…
# ...for a 00:00:00:00. If you go beyond this then the days will be incremented to an even larger number into…
# ...the hundreds. You would have to adjust your display accordingly. The hours and seconds will not…
# ...go beyond the 23:59:59 format.
OPTIONAL CODE #2 [END]
# OPTIONAL CODE #3a [BEGIN] - [To be used with Optional Code #3b]
# Code for adding a Day column to to the formatted time string. Uncomment the next five lines to use.
-- if s > 86359 then # (Optional Code 3a; Line 1)
-- put (s/86400) into dy -- Get number of days # (Optional Code 3a; Line 2)
-- put item 1 of dy into dy -- Keep only the integer or whole number # (Optional Code 3a; Line 3)
-- put s-(dy*86400) into s -- Adjust seconds for days removed # (Optional Code 3a; Line 4)
--else
--put 0 into dy
-- end if -- (Optional Code 3a; Line 5)
# NOTICE: If Optional Code #3a is used be sure and use Optional Code #3b also, and Optional Code #4 is disused.
# OPTIONAL CODE #3a [END
if s> 3599 then # If seconds is more than a hour then…
put (s/3600) into hr # Get number of hours
put item 1 of hr into hr # Keep only the integer or whole number
put s-(hr*3600) into s # Adjust seconds to having hours removed
else
put "00" into hr # If Seconds are less than a hour then set the hr to padded zeros.
end if
if s > 59 then # If seconds is more than a minute then…
put (s/60) into mn # Get number of minutes
put item 1 of mn into mn # Keep only the integer
put s-(mn*60) into s # Adjust seconds to having minutes removed
else
put "00" into mn # If Seconds are less than a minute then set the mn to padded zeros.
end if
put item 1 of s into s # Keep only the integer or whole number
# If seconds end up being less than a whole number or a empty string then set it to "00"
if s < 1 then put "00" into s
if s is empty then put "00" into s
#OPTIONAL CODE #3b [BEGIN]
# NOTICE: If Optional Code #2a is used be sure and use Optional Code #3b also.
# Uncomment the following line for adding DAY column to formatted string.
-- put pad2(dy) & ":" & pad2(hr) & ":" & pad2(mn) & ":" & pad2(s) into tmp # Build time formatted string with DAY column to return.
# NOTICE: Be sure to comment out the above line if Optional Code #4 is used.
#OPTIONAL CODE #3b [END]
#OPTIONAL CODE #4 [BEGIN]
# Uncomment the following line if no DAY column formatted string is desired.
put pad2(hr) & ":" & pad2(mn) & ":" & pad2(s) into tmp # Build time formatted string without DAY column to return.
# NOTICE: Be sure to comment out the above line if Optional Code #3 is used.
# OPTIONAL CODE #4 [END]
# NOTICE: the "pad2" is a function used to pad any number less than 10 with a leading zero.
return tmp # Return the formatted time string
end hms
The following function pads numbers under 10 with a zero. It is called by function hms()
function pad2 n
if number of chars in n < 2 then # Test string to see if the number of characters is less than 2
return "0" & n # If string is only one character then pad it with a leading zero and return it.
else
return n # If string is more than a single character, then return string unpadded.
end if
end pad2
Below are some examples of the hms function being called on a stack populated with a text field for user input, a field to
display the formatted time string in either a 00:00:00 (H:M:S) or 00:00:00:00 (D:H:M:S) format.
Code used in button Call HMS
on mouseUp pMouseButton #Button 'callHMS'
# Get user entered value from field "enterSeconds", process it
# with function hms. Then take the returned string and put it
# into the field "viewHMS" to be displayed to the user.
put hms (field "enterSeconds") into field "viewHMS"
end mouseUp
HMS called with a empty string
HMS called with 1 second
HMS called with 90 seconds
HMS call with a decimal number with Optional Code #1 Round Up in use.
HMS called with 22,254 seconds. Notice I inserted a comma.
The "replace comma with empty" line removed it for accurate processing.
The Code "Cleanup" parses all non conforming user input. In this case everything was removed where only 63 seconds remain which was rendered into 1 minute and 3 seconds
HMS called with 86,400 seconds which is the number of seconds in a 24 hour period.
Optional Code #2 for threshold is in in use for this test.
360,000 exceeded the threshold set to 359,999; The equivalent of 99:59:59 the maximum for a 00:00:00 display.
Optional Code #3 is being used to format four columns for a
Days:Hours:Minutes:Seconds display.
BEGIN DUPLICATE CODE EXAMPLE
(with minimum comments)
(Only comments are optional code.)
function hms s
set itemdelimiter to "."
replace comma with empty in s
if isNumber(s) is false then
repeat with i = 1 to number of chars in s
put char i of s into ckNum
if ckNum is "." then
put ckNum after tmp
end if
if isNumber(ckNum) is true then
put ckNum after tmp
end if
end repeat
put tmp into s
end if
# OPTIONAL CODE #1 [BEGIN] - Round Up Seconds
-- put item 2 of s into d
-- if char 1 of d > 4 then
-- add 1 to item 1 of s
-- end if
# OPTIONAL CODE #1 [END]
if s is empty then
return "00:00:00"
end if
if s < 60 then
put item 1 of s into s
return "00:00:" & pad2(s)
end if
#OPTIONAL CODE #2 [BEGIN] - ERROR display for exceeding 99:59:59
-- if s > 359999 then
-- return "ERROR"
-- end if
#OPTIONAL CODE #2 [END]
#OPTIONAL CODE #3a [BEGIN] - FOR ADDING DAY COLUMN (To be used with Optional Code #3b)
-- if s > 86359 then
-- put (s/86400) into dy
-- put item 1 of dy into dy
-- put s-(dy*86400) into s
--else
--put 0 into dy
-- end if
# NOTICE: If Optional Code #3a is used, be sure disuse/comment out Optional Code #4
# OPTIONAL CODE #3a [END]
if s> 3599 then
put (s/3600) into hr
put item 1 of hr into hr
put s-(hr*3600) into s
else
put "00" into hr
end if
if s > 59 then
put (s/60) into mn
put item 1 of mn into mn
put s-(mn*60) into s
else
put "00" into mn
end if
put item 1 of s into s
if s < 1 then put "00" into s
if s is empty then put "00" into s
#OPTIONAL CODE #3b [BEGIN] - Format Time String WITH a DAY column
# NOTICE: Optional Code #3b is to be used with Optional Code #3a.
-- put pad2(dy) & ":" & pad2(hr) & ":" & pad2(mn) & ":" & pad2(s) into tmp
# NOTICE: Comment out above line if Optional Code #4 is used.
#OPTIONAL CODE #3b [END]
#OPTIONAL CODE #4 [BEGIN] - Format Time String WITHOUT a DAY column
put pad2(hr) & ":" & pad2(mn) & ":" & pad2(s) into tmp
# NOTICE: Comment out above line if Optional Code #3 is used.
#OPTIONAL CODE #4 -[END]
return tmp
end hms
function pad2 n
if number of chars in n < 2 then
return "0" & n
else
return n
end if
end pad2