|
|
|
| 25 March 2002 | SerialTime() |
| Author | W.M. Hinsch (New Mexico Mark) |
| Action | Convert time to numbers (and back) for the purpose of performing time math. |
| Syntax | SerialTime ( "strTime" ) -or- SerialTime ( intSeconds ) |
| Parameters | |
| Remarks | This function is a complement to the SerialDate() function, when one needs to work with dates AND times. |
| Returns | If supplied a time, returns seconds since midnight. If supplied seconds since midnight, returns time. |
| Dependencies | Abs(), Mod(), Word() |
| Examples |
$Value = ""
DO
IF $Value <> ""
? SerialTime($Value) ?
ENDIF
"Enter time or serial string less than 86399, Q to quit: "
GETS $Value ? ?
UNTIL $Value = "Q"
; "01:00" returns 3600
; "23:59:59" returns 86399
; "0:0:1" returns 1
; "7200" returns "02:00:00"
|
| Source |
FUNCTION SerialTime ($tors)
; Syntax: SerialTime(Time | Serial)
; Where Time is a time string in HH:MM[:SS] format or
; Serial is a time serial number (seconds since midnight) 1 - 86399
; If Time is supplied, SerialTime will return the seconds since midnight.
; If seconds since midnight is supplied, SerialTime will return a time.
; DEPENDENCIES: word(), abs(), mod()
DIM $hours, $minutes, $seconds, $serial, $error
$error = 0
IF InStr($tors,":") ; Time supplied
$tors = "" + $tors ; Cast to string
IF (Len($tors) > 4) AND (Len($tors) < 9)
$hours = 0 + word($tors,1,":") ; Hours as integer for calculation
IF ($hours > 23)
$error = 1
ENDIF
$minutes = 0 + word($tors,2,":") ; Minutes as integer for calculation
IF ($minutes > 59)
$error = 1
ENDIF
$seconds = 0 + word($tors,3,":") ; Seconds as integer for calculation
IF ($seconds > 59)
$error = 1
ENDIF
$serial = ($hours * 3600) + ($minutes * 60) + $seconds
IF ($serialtime > 86399)
$error = 1
ENDIF
IF $error
$serialtime = "-1" ; error
ELSE
$serialtime = "" + $serial ; Return as string
ENDIF
ELSE
$serialtime = "-1" ; error
ENDIF
ELSE ; Seconds supplied
$tors = 0 + $tors ; Cast to integer for calculation
IF ($tors <= 86399) AND ($tors >= 0)
$hours = $tors / 3600
$tors = $tors - ($hours * 3600)
$hours = "" + $hours
$hours = Substr("00",1,2 - Len($hours)) + $hours
$minutes = $tors / 60
$tors = $tors - ($minutes * 60)
$minutes = "" + $minutes
$minutes = Substr("00",1,2 - Len($minutes)) + $minutes
$seconds = "" + $tors
$seconds = Substr("00",1,2 - Len($seconds)) + $seconds
$serialtime = $hours + ":" + $minutes + ":" + $seconds ; Return as string
ELSE
$serialtime = "-1" ; error
ENDIF
ENDIF
ENDFUNCTION ; - SerialTime -
|
|
|
|