Jump to content

Nautical bell: Difference between revisions

m (→‎{{header|AppleScript}}: Minor change to headings.)
Line 992:
},Mod[Round[Total[DateList[][[{4,5}]]{2,1/30}]],8,1]]]]
,DateObject[{_,_,_,_,_,30|0}]]]</lang>
 
=={{header|Nim}}==
{{trans|Phix}}
Using UTC time but the program can easily be adapted to use local time (replace <code>getTime().utc()</code> with <code>getTime().local()</code> or <code>now()</code>).
<lang Nim>import os, strformat, times
 
const
Watches = ["First", "Middle", "Morning", "Forenoon", "Afternoon", "First dog", "Last dog", "First"]
WatchEnds = [(0, 0), (4, 0), (8, 0), (12, 0), (16, 0), (18, 0), (20, 0), (23, 59)]
Bells = array[1..8, string](["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"])
Ding = "ding!"
 
 
proc nb(h, m: Natural) =
var bell = (h * 60 + m) div 30 mod 8
if bell == 0: bell = 8
let hm = (h, m)
var watch = 0
while hm > WatchEnds[watch]: inc watch
let plural = if bell == 1: ' ' else: 's'
var dings = Ding
for i in 2..bell:
dings.add (if (i and 1) == 0: "" else: " ") & Ding
echo &"{h:02d}:{m:02d} {Watches[watch]:>9} watch {Bells[bell]:>5} bell{plural} {dings}"
 
 
proc simulateOneDay() =
for h in 0..23:
for m in [0, 30]:
nb(h, m)
nb(0, 0)
 
 
when isMainModule:
 
simulateOneDay()
 
while true:
let d = getTime().utc()
var m = d.second + (d.minute mod 30) * 60
if m == 0:
nb(d.hour, d.minute)
sleep((1800 - m) * 1000) # In milliseconds.</lang>
 
{{out}}
Same as Phix output.
 
=={{header|OoRexx}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.