Nautical bell: Difference between revisions
(→{{header|D}}: updated output of test run) |
(→{{header|D}}: daemonize) |
||
Line 11: | Line 11: | ||
class NauticalBell : Thread { |
class NauticalBell : Thread { |
||
private bool stopped; |
private shared bool stopped; |
||
this() { |
this() { |
||
Line 34: | Line 34: | ||
if (next.minute == time.minute && |
if (next.minute == time.minute && |
||
next.hour == time.hour) { |
next.hour == time.hour) { |
||
immutable bells = |
immutable bells = numBells == 1 ? "bell" : "bells"; |
||
writefln("%s : %d %s |
writefln("%s : %d %s", time, numBells, bells); |
||
setNextBellTime(); |
setNextBellTime(); |
||
} |
} |
||
Line 43: | Line 43: | ||
} |
} |
||
void stop() |
void stop() { |
||
this.stopped = true; |
this.stopped = true; |
||
} |
} |
||
Line 50: | Line 50: | ||
void main() { |
void main() { |
||
auto bells = new NauticalBell(); |
auto bells = new NauticalBell(); |
||
bells.isDaemon(true); |
|||
bells.start(); |
bells.start(); |
||
try { |
|||
bells.join(); |
|||
} catch (ThreadException e) { |
|||
writeln(e.msg); |
|||
} |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
Revision as of 08:45, 2 March 2013
The task is to write a small program that emulates a nautical bell producing a ringing bell pattern at certain times throughout the day. The bell timing should be in accordance with Greenwich Mean Time, unless locale dictates otherwise.
It is permissible for the program to daemonize, or to slave off a scheduler, and it is permissible to use alternative notification methods (such as producing a written notice "Two Bells Gone"), if these are more usual for the system type.
D
This code uses local time instead of Greenwich Mean Time. <lang d>import std.stdio, core.thread, std.datetime;
class NauticalBell : Thread {
private shared bool stopped;
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 (!this.stopped) { time = cast(TimeOfDay)Clock.currTime(); if (next.minute == time.minute && next.hour == time.hour) { immutable bells = numBells == 1 ? "bell" : "bells"; writefln("%s : %d %s", time, numBells, bells); setNextBellTime(); } sleep(dur!"msecs"(100)); yield(); } }
void stop() { this.stopped = true; }
}
void main() {
auto bells = new NauticalBell(); bells.isDaemon(true); bells.start(); try { bells.join(); } catch (ThreadException e) { writeln(e.msg); }
}</lang>
- Output:
09:30:00 : 3 bells 10:00:00 : 4 bells 10:30:00 : 5 bells 11:00:00 : 6 bells 11:30:00 : 7 bells 12:00:00 : 8 bells 12:30:00 : 1 bell 13:00:00 : 2 bells 13:30:00 : 3 bells 14:00:00 : 4 bells 14:30:00 : 5 bells 15:00:00 : 6 bells 15:30:00 : 7 bells 16:00:00 : 8 bells 16:30:00 : 1 bell 17:00:00 : 2 bells 17:30:00 : 3 bells 18:00:00 : 4 bells 18:30:00 : 5 bells 19:00:00 : 6 bells 19:30:00 : 7 bells 20:00:00 : 8 bells 20:30:00 : 1 bell 21:00:00 : 2 bells 21:30:00 : 3 bells 22:00:00 : 4 bells 22:30:00 : 5 bells 23:00:00 : 6 bells 23:30:00 : 7 bells 00:00:00 : 8 bells 00:30:00 : 1 bell 01:00:00 : 2 bells 01:30:00 : 3 bells 02:00:00 : 4 bells 02:30:00 : 5 bells 03:00:00 : 6 bells 03:30:00 : 7 bells 04:00:00 : 8 bells 04:30:00 : 1 bell 05:00:00 : 2 bells 05:30:00 : 3 bells 06:00:00 : 4 bells 06:30:00 : 5 bells 07:00:00 : 6 bells 07:30:00 : 7 bells 08:00:00 : 8 bells 08:30:00 : 1 bell 09:00:00 : 2 bells
REXX
The local time is used instead of Greenwich mean time.
If any arguments are specified, that text is used as a prefix to the times shown (once a minute). Also, the number of bells sounded are shown (if any arguments are specified). If no arguments are specified, no times are shown. In all cases, the PC speaker is used to sound the bells (albeit a poorly sounded bell).
This REXX program makes use of DELAY
BIF, which delays (sleeps) for a specified amount of seconds (some REXXes doen't have a DELAY
BIF, so one is included here), and SOUND
BIF, which produces sounds via the PC speaker (some REXXes doen't have a SOUND
BIF, so one is included here).
<lang rexx>/*REXX pgm sounds "bells" (using PC speaker) when running (perpetually).*/
echo= arg()\==0 /*echo time & bells if any args. */
signal on halt /*allow a clean way to stop prog.*/
t.1 = '00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00'
t.2 = '04:30 05:00 05:30 06:00 06:30 07:00 07:30 08:00'
t.3 = '08:30 09:00 09:30 10:00 10:30 11:00 11:30 12:00'
do forever; t=time(); ss=right(t,2); mn=right(t,2) /*times.*/ ct=time('C') /*[↓] add leading zero.*/ hhmmc=left( right( ct, 7, 0), 5) /*HH:MM (leading zero).*/ if echo then say center(arg(1) ct, 79) /*echo arg1 with time ?*/ if ss\==00 & mn\==00 & mn\==30 then /*wait for next min ? */ do; call delay 60-ss; iterate; end /*delay fraction of min*/
/*[↓] # bells to peel.*/ do j=1 for 3 until $\==0; $=wordpos(hhmmc,t.j); end /*j*/
if $\==0 & echo then say center($ "bells", 79) /*echo bells? */
do k=1 for $; call sound 650,1; call delay 1+(k//2==0); end /*k*/ /*[↑] peel and pause.*/ call delay 60 /*ensure don't re-peel.*/ end /*forever*/
halt: /*stick a fork in it, we're done.*/</lang> output when using the input of: the time is:
the time is: 1:48pm the time is: 1:49pm the time is: 1:50pm the time is: 1:51pm the time is: 1:52pm the time is: 1:53pm the time is: 1:54pm the time is: 1:55pm the time is: 1:56pm the time is: 1:57pm the time is: 1:58pm the time is: 1:59pm the time is: 2:00pm 4 bells the time is: 2:01pm the time is: 2:02pm ∙ ∙ ∙