home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: saytime.icn
- #
- # Subject: Procedure to produce the time in English
- #
- # Author: Robert J. Alexander
- #
- # Date: May 20, 1992
- #
- ###########################################################################
- #
- # saytime() -- Computes the time in natural English. If an argument is
- # supplied it is used as a test value to check the operation of the
- # program.
- #
- ############################################################################
-
- procedure saytime(time)
- local hour,min,mod,near,numbers,out,sec
- #
- # Extract the hours, minutes, and seconds from the time.
- #
- /time := &clock
- time ? {
- hour := integer(tab(find(":") | 0)) | fail
- move(1)
- min := tab(find(":") | 0)
- move(1)
- sec := tab(0)
- }
- min := integer(min) | 0
- sec := integer(sec) | 0
- #
- # Now start the processing in earnest.
- #
- near := ["just gone","just after","nearly","almost"]
- if sec > 29 then min +:= 1 # round up minutes
- mod := min % 5 # where we are in 5 minute bracket
- out := near[mod] || " " | "" # start building the result
- if min > 32 then hour +:= 1 # we are TO the hour
- min +:= 2 # shift minutes to straddle the 5-minute point
- #
- # Now special-case the result for Noon and Midnight hours.
- #
- if hour % 12 = 0 & min % 60 <= 4 then {
- return if hour = 12 then out || "noon"
- else out || "midnight"
- }
- min -:= min % 5 # find the nearest 5 mins
- if hour > 12 then hour -:= 12 # get rid of 25-hour clock
- else if hour = 0 then hour := 12 # .. and allow for midnight
- #
- # Determine the phrase to use for each 5-minute segment.
- #
- case min of {
- 0: {} # add "o'clock" later
- 60: min=0 # ditto
- 5: out ||:= "five past"
- 10: out ||:= "ten past"
- 15: out ||:= "a quarter past"
- 20: out ||:= "twenty past"
- 25: out ||:= "twenty-five past"
- 30: out ||:= "half past"
- 35: out ||:= "twenty five to"
- 40: out ||:= "twenty to"
- 45: out ||:= "a quarter to"
- 50: out ||:= "ten to"
- 55: out ||:= "five to"
- }
- numbers := ["one","two","three","four","five","six",
- "seven","eight","nine","ten","eleven","twelve"]
- out ||:= (if *out = 0 then "" else " ") || numbers[hour]
- # add the hour number
- if min = 0 then out ||:= " o'clock" # .. and o'clock if exact
- return out # return the final result
- end
-