Nautical bell: Difference between revisions

(→‎{{header|D}}: added D)
Line 8:
 
[[Category: Date and time]]
 
=={{header|D}}==
<lang d>import std.stdio, core.thread, std.datetime;
 
class NauticalBell : Thread {
 
this () {
super (&run);
}
 
void run() {
uint numBells;
auto time = cast(TimeOfDay)Clock.currTime();
auto next = TimeOfDay();
 
void setNextBellTime() {
next += minutes(30);
numBells = 1 + (numBells % 8);
}
 
while (next < time)
setNextBellTime();
 
while (true) {
Thread.sleep(dur!"seconds"(1));
time = cast(TimeOfDay)Clock.currTime();
if (next.minute == time.minute &&
next.hour == time.hour) {
auto bells = (numBells == 1) ? "bell" : "bells";
writefln("%s : %d %s ", time, numBells, bells);
setNextBellTime();
}
}
}
}
 
void main() {
auto bells = new NauticalBell();
//bells.isDaemon(true);
bells.start();
}</lang>
{{out}}
<pre>
22:00:00 : 4 bells
22:30:00 : 5 bells
23:00:00 : 6 bells</pre>
 
=={{header|REXX}}==
Anonymous user