Nautical bell: Difference between revisions

(Added Go)
Line 798:
14:00:00 : 4 bells
...</pre>
 
=={{header|Julia}}==
<lang julia>using Dates
 
"""
nauticalbells(DateTime)
Return a string according to the "simpler system" of nautical bells
listed in the table in Wikipedia at
en.wikipedia.org/wiki/Ship%27s_bell#Simpler_system.
Note the traditional time zone was determined by local sun position
and so should be local time without daylight savings time.
"""
function nauticalbells(dt::DateTime)
hr = hour(dt)
mn = minute(dt)
if hr in [00, 12, 4, 8]
return mn == 00 ? "2 2 2 2" : "1"
elseif hr in [1, 5, 9]
return mn == 00 ? "2" : "2 1"
elseif hr in [2, 6, 10]
return mn = 00 ? "2 2" : "2 2 1"
elseif hr in [3, 7, 11]
return mn == 00 ? "2 2 2" : "2 2 2 1"
else
return "Gong pattern error: time $dt"
end
end
 
function nauticalbelltask()
# parse for milliseconds til next hour, start timer with that delay
untilnexthour = now() - DateTime(match(r"^([^:]+)", string(now())).captures[1])
delay = untilnexthour.value / 1000
println("Nautical bell task starting -- next bell in $delay seconds.")
# The timer wakes its task every half hour. May drifts slightly so restart yearly.
timer = Timer(delay; interval=1800)
while true
wait(timer)
gong = time2bells(now())
@async(println("Nautical bell gong strikes $gong!")) # or do hardware dependent audible tones
end
end
 
nauticalbelltask()
</lang>
 
 
=={{header|Kotlin}}==
4,105

edits