formatting times for the user

Ken Ray kray at sonsothunder.com
Sat Jun 4 13:35:54 EDT 2005


> Are there any simple ways to take times (like Player currentPositions or
> Durations) and format them for the user so that they look like
> "h:mm:ss.hh"?  I looked in the help for Format, but no joy.

You can use my handy-dandy 'stsConvertTime' function (watch for line wraps);
since I think the currentPosition/durations are in milliseconds, you can do
this:

on mouseUp
  put the duration of player 1 into tDur
  put stsConvertTime(tDur,"MS","HH:MM:SS.MS") into tResult
  answer tResult
end mouseUp

function stsConvertTime pValue,pInFormat,pOutFormat
  -- First, convert incoming value to milliseconds
  if isNumber(pValue) is false then
    return "Error: Incoming value is not a real number."
    exit stsConvertTime
  end if
  
  if pInFormat = "" then put "MS" into pInFormat
  if pOutFormat = "" then put "HH:MM:SS.MS" into pOutFormat
  
  switch pInFormat
  case "H"
    put (pValue*60*60*1000) into pMS
    break
  case "M"
    put (pValue*60*1000) into pMS
    break
  case "S"
    put (pValue*1000) into pMS
    break
  case "T"
    put (pValue/60)*1000 into pMS
    break
  case "MS"
    put pValue into pMS
    break
  end switch
  put 0 into tSecs
  put 0 into tMins
  put 0 into tHrs
  put pMS div 1000 into tSecs
  put pMS mod 1000 into tMS
  put tMS into tVal
  if tSecs <> 0 then
    put tSecs div 60 into tMins
    put tSecs mod 60 into tSecs
    if tMins <> 0 then
      put tMins div 60 into tHrs
      put tMins mod 60 into tMins
    end if
  end if
  -- No format return value
  if "MS" is in pOutFormat then replace "MS" with tMS in pOutFormat
  if "HH" is in pOutFormat then replace "HH" with _PadZero(tHrs) in
pOutFormat
  if "H" is in pOutFormat then replace "H" with tHrs in pOutFormat
  if "MM" is in pOutFormat then replace "MM" with _PadZero(tMins) in
pOutFormat
  if "M" is in pOutFormat then replace "M" with tMins in pOutFormat
  if "MM" is in pOutFormat then replace "MM" with _PadZero(tMins) in
pOutFormat
  if "M" is in pOutFormat then replace "M" with tMins in pOutFormat
  if "SS" is in pOutFormat then replace "SS" with _PadZero(tSecs) in
pOutFormat
  if "S" is in pOutFormat then replace "S" with tSecs in pOutFormat
  return pOutFormat
end stsConvertTime

function _PadZero pVal
  if pVal < 10 then return "0" & pVal
  else return pVal
end _PadZero 

Have fun!


Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list