Nautical bell: Difference between revisions

m
(→‎{{header|C}}: Mark incorrect)
m (→‎{{header|Wren}}: Minor tidy)
 
(39 intermediate revisions by 16 users not shown)
Line 17:
 
=={{header|AppleScript}}==
 
===Continuous repeat===
 
This version uses local time and speaks the bell rings using OS X's built-in speech synthesizer.
 
<langsyntaxhighlight lang="applescript">repeat
set {hours:h, minutes:m} to (current date)
if {0, 30} contains m then
Line 33 ⟶ 36:
end if
delay 60
end repeat</langsyntaxhighlight>
 
==='idle' handler===
 
For actions which are to be carried out at set intervals throughout the day, it's more usual to use a stay-open script applet with an 'idle' handler. When such an applet's launched, it sits there mostly doing nothing until it receives an 'idle' command from the system, whereupon it performs the actions in its 'idle' handler. If this handler returns a positive number (or something that can be interpreted as one), the system takes this as the number of seconds to wait before sending another 'idle' command to the applet. Otherwise the default is thirty seconds. This hogs the processor far less than having the script run on a continuous repeat. Unfortunately, depending on how busy the computer is at the time, 'idle' calls may be slightly delayed, so any time checks performed by the handler have to allow for this.
 
<syntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
 
property soundName : "Glass" -- The nearest system sound to a bell!
property leeway : 8 -- Number of seconds either side of the sounding time in which the bells are allowed to start.
property postNoreBritish : true -- 1, 2, 3, and 8 bells during last dog watch?
property halfHour : 30 * minutes
 
on idle
local m, d, t, elapsed, bells, bellSound
-- Get the month, day, and time (in seconds) of the current GMT date, shifted forward by 'leeway' seconds.
set {month:m, day:d, time:t} to (current date) + (leeway - (time to GMT))
-- How far is this into a half-hour?
set elapsed to t mod halfHour
-- If too far, just reset the idle and don't sound this time.
if (elapsed mod halfHour > 2 * leeway) then return halfHour - (elapsed - leeway)
-- Otherwise work out how many bells are required and sound them.
if ((t < halfHour) and (d is 1) and (month is January)) then
set bells to 16 -- New Year.
else
set bells to (t mod (4 * hours) div halfHour + 7) mod 8 + 1
if ((postNoreBritish) and (t > 18 * hours) and (t < 20 * hours)) then set bells to bells - 4
end if
set bellSound to current application's class "NSSound"'s soundNamed:(soundName)
repeat (bells div 2) times
repeat 2 times
tell bellSound to play()
delay 0.7
tell bellSound to |stop|()
end repeat
delay 1
end repeat
repeat bells mod 2 times
tell bellSound to play()
delay 0.7
tell bellSound to |stop|()
end repeat
-- Request another call in half an hour's time, less however long the above took.
return halfHour - (time of (current date)) mod halfHour
end idle</syntaxhighlight>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">NauticalBell(hh, mm){
Hr := 0, min := 30, Bells := [], pattern := []
Loop 8 ; genrate 8 patterns
{
num := A_Index , code := ""
while (num/2 >=1)
code .= "** ", num := num-2
code .= mod(A_Index, 2) ? "*" : ""
pattern[A_Index] := code
}
loop, 48 ; 24 hours * 2 for every half an hour
{
numBells := !mod(A_Index, 8) ? 8 : mod(A_Index, 8) , min := 30
if !Mod(A_Index, 2)
hr++ , min := 00
Bells[SubStr("0" hr, -1) ":" min] := numBells
}
Bells[00 ":" 00] := Bells[24 ":" 00] , numBells := Bells[hh ":" mm]
return {"bells": numBells, "pattern": Pattern[numBells]}
}</syntaxhighlight>
Example:<syntaxhighlight lang="autohotkey">res := ""
loop, 24
{
hr := SubStr("0" A_Index -1, -1)
Loop 60
{
min := SubStr("0" A_Index -1, -1)
if (min = 0 || min = 30)
res .= hr ":" min "`t" NauticalBell(hr, min).bells "`t" NauticalBell(hr, min).pattern "`n"
}
}
MsgBox, 262144, , % "Time`tBells`tPattern`n" res
return</syntaxhighlight>
Outputs:<pre>Time Bells Pattern
00:00 8 ** ** ** **
00:30 1 *
01:00 2 **
01:30 3 ** *
02:00 4 ** **
02:30 5 ** ** *
03:00 6 ** ** **
03:30 7 ** ** ** *
04:00 8 ** ** ** **
04:30 1 *
05:00 2 **
05:30 3 ** *
06:00 4 ** **
06:30 5 ** ** *
07:00 6 ** ** **
07:30 7 ** ** ** *
08:00 8 ** ** ** **
08:30 1 *
09:00 2 **
09:30 3 ** *
10:00 4 ** **
10:30 5 ** ** *
11:00 6 ** ** **
11:30 7 ** ** ** *
12:00 8 ** ** ** **
12:30 1 *
13:00 2 **
13:30 3 ** *
14:00 4 ** **
14:30 5 ** ** *
15:00 6 ** ** **
15:30 7 ** ** ** *
16:00 8 ** ** ** **
16:30 1 *
17:00 2 **
17:30 3 ** *
18:00 4 ** **
18:30 5 ** ** *
19:00 6 ** ** **
19:30 7 ** ** ** *
20:00 8 ** ** ** **
20:30 1 *
21:00 2 **
21:30 3 ** *
22:00 4 ** **
22:30 5 ** ** *
23:00 6 ** ** **
23:30 7 ** ** ** *</pre>
 
Alternatively, you could remove the forever loop and set it up to run every half hour via launchd or cron.
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NAUTICAL_BELL.AWK
BEGIN {
Line 89 ⟶ 226:
return(new_str)
}
</syntaxhighlight>
</lang>
<p>Output:</p>
<pre>
Line 144 ⟶ 281:
=={{header|C}}==
 
Implementation corrected, sounds the system bell as per nautical standards, sounds bell as per local system time.
{{incorrect|C|A nautical bell ''never'' sounds more than eight times. The number of chimes indicate how many half-hour increments have passed on a four hour 'watch'.}}
 
Sounds the bell once every half hour (12:30, 1:30 etc.) and for as many times on the hour (12 times on 12 PM/AM, 3 times on 3 AM/PM etc.)
<lang C>
/*Abhishek Ghosh, 20th September 2017*/
 
<syntaxhighlight lang="c">
#include<unistd.h>
#include<stdio.h>
#include<time.h>
 
#define DELAYSHORTLAG 30001000
#define LONGLAG 2000
 
int main(){
int i,times,hour,min,sec,min1,min2;
time_t t;
Line 166 ⟶ 301:
currentTime = localtime(&t);
timeshour = (currentTime->tm_hour%12==0)?12:currentTime->tm_hour%12;
min = currentTime->tm_min;
sec = currentTime->tm_sec;
hour = 12;
if(currentTime->tm_min==0 && currentTime->tm_sec==0){
min = 0;
printf("\nIt is now %d:00 %s. Sounding the bell %d times.",times,(currentTime->tm_hour>11)?"PM":"AM",times);
sec = 0;
if((min==0 || min==30) && sec==0)
for(i=0;i<times;i++){
times = ((hour*60 + min)%240)%8;
printf("\a");
if(times==0){
sleep(DELAY);
}times = 8;
}
 
if(min==0){
min1 = 0;
min2 = 0;
}
else{
else if(currentTime->tm_min==30 && currentTime->tm_sec==0){
min1 = 3;
printf("\nIt is now %d:30 %s. Sounding the bell once.",times,(currentTime->tm_hour>11)?"PM":"AM");
printf("\a")min2 = 0;
}
if((min==0 || min==30) && sec==0){
printf("\nIt is now %d:%d%d %s. Sounding the bell %d times.",hour,min1,min2,(hour>11)?"PM":"AM",times);
for(i=1;i<=times;i++){
printf("\a");
(i%2==0)?sleep(LONGLAG):sleep(SHORTLAG);
}
}
}
return 0;
}
 
</lang>
</syntaxhighlight>
 
=={{header|C++}}==
This version uses local time.
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <string>
Line 259 ⟶ 412:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
Output:
<pre>
Line 314 ⟶ 467:
=={{header|D}}==
This code uses local time instead of Greenwich Mean Time.
<langsyntaxhighlight lang="d">import std.stdio, core.thread, std.datetime;
 
class NauticalBell : Thread {
Line 363 ⟶ 516:
writeln(e.msg);
}
}</langsyntaxhighlight>
This output is from an actual test run.
<pre>
Line 414 ⟶ 567:
08:30:00 : 1 bell
09:00:00 : 2 bells</pre>
 
<syntaxhighlight lang="freebasic">Dim As Byte m = 0
For n As Byte = 0 To 23
If n = 23 Then
Print " 23" + ":30" + " = " + "7 bells"
Else
m += 1
Print ""; n Mod 23; ":30"; " ="; m; " bells"
End If
If n = 23 Then
Print " 00" + ":00" + " = " + "8 bells"
Else
m += 1
Print ""; (n Mod 23+1); ":00"; " ="; m; " bells"
If m = 8 Then m = 0
End If
Next n
Sleep</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|Ring}}
<syntaxhighlight lang="vb">Dim As Byte m = 0
For n As Byte = 0 To 23
If n = 23 Then
Print " 23" + ":30" + " = " + "7 bells"
Else
m += 1
Print ""; n Mod 23; ":30"; " ="; m; " bells"
End If
If n = 23 Then
Print " 00" + ":00" + " = " + "8 bells"
Else
m += 1
Print ""; (n Mod 23+1); ":00"; " ="; m; " bells"
If m = 8 Then m = 0
End If
Next n
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Provided your terminal bell is enabled, this should beep an appropriate number of times before displaying its output. It uses local time.
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"strings"
"time"
)
 
func main() {
watches := []string{
"First", "Middle", "Morning", "Forenoon",
"Afternoon", "Dog", "First",
}
for {
t := time.Now()
h := t.Hour()
m := t.Minute()
s := t.Second()
if (m == 0 || m == 30) && s == 0 {
bell := 0
if m == 30 {
bell = 1
}
bells := (h*2 + bell) % 8
watch := h/4 + 1
if bells == 0 {
bells = 8
watch--
}
sound := strings.Repeat("\a", bells)
pl := "s"
if bells == 1 {
pl = " "
}
w := watches[watch] + " watch"
if watch == 5 {
if bells < 5 {
w = "First " + w
} else {
w = "Last " + w
}
}
fmt.Printf("%s%02d:%02d = %d bell%s : %s\n", sound, h, m, bells, pl, w)
}
time.Sleep(1 * time.Second)
}
}</syntaxhighlight>
 
{{out}}
Abbreviated output:
<pre>
...
15:30 = 7 bells : Afternoon watch
16:00 = 8 bells : Afternoon watch
16:30 = 1 bell : First Dog watch
17:00 = 2 bells : First Dog watch
...
</pre>
 
=={{header|Haskell}}==
This solution first creates a general way of scheduling tasks on a time interval, and then schedules a "ringing" task. If used in a terminal it will also produce noise. Local time is used.
<langsyntaxhighlight lang="haskell">
import Control.Concurrent
import Control.Monad
Line 496 ⟶ 749:
bellRinger = doWithScheduler (onInterval (30*60)) ringBells
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 508 ⟶ 761:
 
=={{header|J}}==
{{trans|Perl6Raku}}
 
'''Solution''':<langsyntaxhighlight lang="j">require 'strings printf'
 
WATCH =: <;._1 ' Middle Morning Forenoon Afternoon Dog First'
Line 570 ⟶ 823:
end.
y
)</langsyntaxhighlight>
 
'''Examples''': Invoke <tt>shipsWatch 0</tt>; the output is identical to Perl6Raku's.
 
'''Notes''': I tested the <tt>clock2ship</tt>, <tt>callWatch</tt>, and <tt>ringBell</tt> functions, but didn't actually have the patience to test <tt>shipsWatch</tt> over a 24-hour period. Use at your own risk (but don't use it to keep watch on your galleon, please).
Line 579 ⟶ 832:
This code uses UTC time.
{{trans|D}}
<langsyntaxhighlight lang="java">import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
Line 628 ⟶ 881:
}
}
}</langsyntaxhighlight>
 
Sample output:
Line 637 ⟶ 890:
14:00:00 : 4 bells
...</pre>
 
=={{header|Julia}}==
<syntaxhighlight 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, 16, 20]
return mn == 00 ? "2 2 2 2" : "1"
elseif hr in [1, 5, 9, 13, 17, 21]
return mn == 00 ? "2" : "2 1"
elseif hr in [2, 6, 10, 14, 18, 22]
return mn == 00 ? "2 2" : "2 2 1"
elseif hr in [3, 7, 11, 15, 19, 23]
return mn == 00 ? "2 2 2" : "2 2 2 1"
else
return "Gong pattern error: time $dt, hour $hr, minutes $mn"
end
end
 
function nauticalbelltask()
untilnextbell = ceil(now(), Dates.Minute(30)) - now()
delay = untilnextbell.value / 1000
println("Nautical bell task starting -- next bell in $delay seconds.")
# The timer wakes its task every half hour. May drift very slightly so restart yearly.
timer = Timer(delay; interval=1800)
while true
wait(timer)
gong = nauticalbells(now())
println("Nautical bell gong strikes ", gong)
end
end
 
nauticalbelltask()
</syntaxhighlight> {{output}} <pre>
Nautical bell task starting -- next bell in 1201.726 seconds.
Nautical bell gong strikes 2 2 2
Nautical bell gong strikes 2 2 2 1
Nautical bell gong strikes 2 2 2 2
Nautical bell gong strikes 1
Nautical bell gong strikes 2
Nautical bell gong strikes 2 1
Nautical bell gong strikes 2 2
Nautical bell gong strikes 2 2 1
Nautical bell gong strikes 2 2 2
Nautical bell gong strikes 2 2 2 1
Nautical bell gong strikes 2 2 2 2
Nautical bell gong strikes 1
Nautical bell gong strikes 2
Nautical bell gong strikes 2 1
Nautical bell gong strikes 2 2
Nautical bell gong strikes 2 2 1
Nautical bell gong strikes 2 2 2
Nautical bell gong strikes 2 2 2 1
,,,
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.3
 
import java.text.DateFormat
Line 693 ⟶ 1,011:
}
}
}</langsyntaxhighlight>
 
Sample output:
Line 703 ⟶ 1,021:
....
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Works on version 11.2 ARM, a bug prevents this from working on version 11.3 Win64.
<syntaxhighlight lang="mathematica">LocalSubmit[ScheduledTask[
EmitSound[Sound[Table[{
SoundNote["C",750/1000,"TubularBells"],SoundNote[None,500/1000,"TubularBells"]
},Mod[Round[Total[DateList[][[{4,5}]]{2,1/30}]],8,1]]]]
,DateObject[{_,_,_,_,_,30|0}]]]</syntaxhighlight>
 
=={{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>).
<syntaxhighlight 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:
if i mod 2 != 0: dings.add ' '
dings.add 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.</syntaxhighlight>
 
{{out}}
Same as Phix output.
 
=={{header|OoRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">/*REXX pgm beep's "bells" (using PC speaker) when running (perpetually).*/
Parse Arg msg
If msg='?' Then Do
Line 751 ⟶ 1,124:
Return res
 
halt:</langsyntaxhighlight>
 
=={{header|Perl 6}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use utf8;
binmode STDOUT, ":utf8";
use DateTime;
 
$| = 1; # to prevent output buffering
Perl 6 uses [[wp:Coordinated_Universal_Time|UTC]] (GMT) time internally and by default. This will display the current UTC time and on the half hour, display a graphical representation of the bell. If run in a terminal with the system bell enabled, will also chime the system alarm bell.
 
<lang perl6>my @watch = <Middle Morning Forenoon Afternoon Dog First>;
my @ordinal = <One Two Three Four Five Six Seven Eight>;
 
my $thishour;
my $thisminute = '';
 
while () {
loop {
my $utc = DateTime.new->now(time time_zone => 'UTC' );
if ($utc.->minute ~=~ any/^(0,00|30)$/ and $utc.->minute != $thisminute) {
$thishour = $utc.->hour;
$thisminute = $utc.->minute;
bell($thishour, $thisminute);
}
printf "%s%02d:%02d:%02d", "\r", $utc.->hour, $utc.->minute, $utc.->second;
sleep(1);
}
 
sub bell ($hour, $minute) {
my($hour, $minute) = @_;
 
my $bells = (($hour % 4) * 2 + $minute div 30) || 8;
my $bells = (($hour % 4) * 2 + int $minute/30) || 8;
printf "%s%02d:%02d %9s watch, %6s Bell%s Gone: \t", "\b" x 9, $hour, $minute,
@ $watch[(int($hour div /4) - !?(0==($minute + $hour % 4)) + 6) % 6],
@ $ordinal[$bells - 1], $bells == 1 ?? '' !!: 's';
chime($bells);
}
 
sub chime ($count) {
sub chime {
for 1..$count div 2 {
my($count) = shift;
print "\a♫ ";
for (1..int($count/2)) {
sleep .25;
print "\aa♫ "; sleep .25;
print "\a"; sleep .75;
}
if $count % 2 {
print "\a♪";
sleep 1;
}
print "\n";
}
if ($count % 2) {
}</lang>
print "\a♪"; sleep 1;
}
print "\n";
}</syntaxhighlight>
{{out}}
<pre style="height:35ex">00:0030 First Middle watch, Eight Bells One Bell Gone:
0001:3000 Middle watch, One BellTwo Bells Gone:
01:0030 Middle watch, TwoThree Bells Gone:
0102:3000 Middle watch, Three Four Bells Gone:
02:0030 Middle watch, Four Five Bells Gone: ♫ ♫
0203:3000 Middle watch, Five Six Bells Gone: ♫ ♫
03:0030 Middle watch, SixSeven Bells Gone: ♫ ♫ ♫
0304:3000 Middle watch, Seven Eight Bells Gone: ♫ ♫ ♫
04:0030 Middle Morning watch, Eight Bells One Bell Gone:
0405:3000 Morning watch, One BellTwo Bells Gone:
05:0030 Morning watch, TwoThree Bells Gone:
0506:3000 Morning watch, Three Four Bells Gone:
06:0030 Morning watch, Four Five Bells Gone: ♫ ♫
0607:3000 Morning watch, Five Six Bells Gone: ♫ ♫
07:0030 Morning watch, SixSeven Bells Gone: ♫ ♫ ♫
0708:3000 Morning watch, Seven Eight Bells Gone: ♫ ♫ ♫
08:0030 Morning Forenoon watch, Eight Bells One Bell Gone:
0809:3000 Forenoon watch, One BellTwo Bells Gone:
09:0030 Forenoon watch, TwoThree Bells Gone:
0910:3000 Forenoon watch, Three Four Bells Gone:
10:0030 Forenoon watch, Four Five Bells Gone: ♫ ♫
1011:3000 Forenoon watch, Five Six Bells Gone: ♫ ♫
11:0030 Forenoon watch, SixSeven Bells Gone: ♫ ♫ ♫
1112:3000 Forenoon watch, Seven Eight Bells Gone: ♫ ♫ ♫
12:0030 Forenoon Afternoon watch, Eight Bells One Bell Gone:
1213:3000 Afternoon watch, One BellTwo Bells Gone:
13:0030 Afternoon watch, TwoThree Bells Gone:
1314:3000 Afternoon watch, Three Four Bells Gone:
14:0030 Afternoon watch, Four Five Bells Gone: ♫ ♫
1415:3000 Afternoon watch, Five Six Bells Gone: ♫ ♫
15:0030 Afternoon watch, SixSeven Bells Gone: ♫ ♫ ♫
1516:3000 Afternoon watch, Seven Eight Bells Gone: ♫ ♫ ♫
16:0030 Afternoon Dog watch, Eight Bells One Bell Gone:
1617:3000 Dog watch, One BellTwo Bells Gone:
17:0030 Dog watch, TwoThree Bells Gone:
1718:3000 Dog watch, Three Four Bells Gone:
18:0030 Dog watch, Four Five Bells Gone: ♫ ♫
1819:3000 Dog watch, Five Six Bells Gone: ♫ ♫
19:0030 Dog watch, SixSeven Bells Gone: ♫ ♫ ♫
1920:3000 Dog watch, Seven Eight Bells Gone: ♫ ♫ ♫
20:0030 DogFirst watch, Eight Bells One Bell Gone:
2021:3000 First watch, One BellTwo Bells Gone:
21:0030 First watch, TwoThree Bells Gone:
2122:3000 First watch, Three Four Bells Gone:
22:0030 First watch, Four Five Bells Gone: ♫ ♫
2223:3000 First watch, Five Six Bells Gone: ♫ ♫
23:0030 First watch, SixSeven Bells Gone: ♫ ♫ ♫
2324:3000 First watch, Seven Eight Bells Gone: ♫ ♫ ♫ </pre>
 
=={{header|Phix}}==
{{trans|Ruby}}
{{trans|AWK}}
Uses GMT, can easily be switched to local time by simply removing DT_GMT, ie invoking <code>date()</code>
instead of <code>date(DT_GMT)</code>.<br>
Uses a full-length sleep of up to 1800 seconds (half an hour), as it should.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">watches</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"First"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Middle"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Morning"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Forenoon"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Afternoon"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First dog"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Last dog"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">watch_ends</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"00:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"04:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"08:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"12:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"16:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"18:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"20:00"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"23:59"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">bells</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"One"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Two"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Three"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Four"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Five"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Six"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Seven"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Eight"</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">ding</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ding!"</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">nb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">bell</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">h</span><span style="color: #0000FF;">*</span><span style="color: #000000;">60</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">30</span><span style="color: #0000FF;">),</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">bell</span><span style="color: #0000FF;">==</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">bell</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">8</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">hm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%02d:%02d"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">watch</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">hm</span><span style="color: #0000FF;">></span><span style="color: #000000;">watch_ends</span><span style="color: #0000FF;">[</span><span style="color: #000000;">watch</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span> <span style="color: #000000;">watch</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">plural</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bell</span><span style="color: #0000FF;">==</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">dings</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ding</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">bell</span> <span style="color: #008080;">do</span> <span style="color: #000000;">dings</span> <span style="color: #0000FF;">&=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">ding</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %9s watch %5s bell%s %s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">hm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">watches</span><span style="color: #0000FF;">[</span><span style="color: #000000;">watch</span><span style="color: #0000FF;">],</span><span style="color: #000000;">bells</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bell</span><span style="color: #0000FF;">],</span><span style="color: #000000;">plural</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dings</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">simulate1day</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">23</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">30</span> <span style="color: #008080;">by</span> <span style="color: #000000;">30</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">nb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">nb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (again)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">simulate1day</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (no sleep() function)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">date</span><span style="color: #0000FF;">(</span><span style="color: #004600;">DT_GMT</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_SECOND</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_MINUTE</span><span style="color: #0000FF;">],</span><span style="color: #000000;">30</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">60</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">nb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_HOUR</span><span style="color: #0000FF;">],</span><span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #004600;">DT_MINUTE</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">sleep</span><span style="color: #0000FF;">(</span><span style="color: #000000;">30</span><span style="color: #0000FF;">*</span><span style="color: #000000;">60</span><span style="color: #0000FF;">-</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
00:00 First watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
00:30 Middle watch One bell ding!
01:00 Middle watch Two bells ding!ding!
01:30 Middle watch Three bells ding!ding! ding!
02:00 Middle watch Four bells ding!ding! ding!ding!
02:30 Middle watch Five bells ding!ding! ding!ding! ding!
03:00 Middle watch Six bells ding!ding! ding!ding! ding!ding!
03:30 Middle watch Seven bells ding!ding! ding!ding! ding!ding! ding!
04:00 Middle watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
04:30 Morning watch One bell ding!
05:00 Morning watch Two bells ding!ding!
05:30 Morning watch Three bells ding!ding! ding!
06:00 Morning watch Four bells ding!ding! ding!ding!
06:30 Morning watch Five bells ding!ding! ding!ding! ding!
07:00 Morning watch Six bells ding!ding! ding!ding! ding!ding!
07:30 Morning watch Seven bells ding!ding! ding!ding! ding!ding! ding!
08:00 Morning watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
08:30 Forenoon watch One bell ding!
09:00 Forenoon watch Two bells ding!ding!
09:30 Forenoon watch Three bells ding!ding! ding!
10:00 Forenoon watch Four bells ding!ding! ding!ding!
10:30 Forenoon watch Five bells ding!ding! ding!ding! ding!
11:00 Forenoon watch Six bells ding!ding! ding!ding! ding!ding!
11:30 Forenoon watch Seven bells ding!ding! ding!ding! ding!ding! ding!
12:00 Forenoon watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
12:30 Afternoon watch One bell ding!
13:00 Afternoon watch Two bells ding!ding!
13:30 Afternoon watch Three bells ding!ding! ding!
14:00 Afternoon watch Four bells ding!ding! ding!ding!
14:30 Afternoon watch Five bells ding!ding! ding!ding! ding!
15:00 Afternoon watch Six bells ding!ding! ding!ding! ding!ding!
15:30 Afternoon watch Seven bells ding!ding! ding!ding! ding!ding! ding!
16:00 Afternoon watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
16:30 First dog watch One bell ding!
17:00 First dog watch Two bells ding!ding!
17:30 First dog watch Three bells ding!ding! ding!
18:00 First dog watch Four bells ding!ding! ding!ding!
18:30 Last dog watch Five bells ding!ding! ding!ding! ding!
19:00 Last dog watch Six bells ding!ding! ding!ding! ding!ding!
19:30 Last dog watch Seven bells ding!ding! ding!ding! ding!ding! ding!
20:00 Last dog watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
20:30 First watch One bell ding!
21:00 First watch Two bells ding!ding!
21:30 First watch Three bells ding!ding! ding!
22:00 First watch Four bells ding!ding! ding!ding!
22:30 First watch Five bells ding!ding! ding!ding! ding!
23:00 First watch Six bells ding!ding! ding!ding! ding!ding!
23:30 First watch Seven bells ding!ding! ding!ding! ding!ding! ding!
00:00 First watch Eight bells ding!ding! ding!ding! ding!ding! ding!ding!
13:00 Afternoon watch Two bells ding!ding!
13:30 Afternoon watch Three bells ding!ding! ding!
</pre>
 
=={{header|PL/I}}==
This program sounds the bell as well as displaying "gong" an appropriate number of times. It operates on local time.
<syntaxhighlight lang="pl/i">
<lang PL/I>
nautical: procedure options (main); /* 29 October 2013 */
declare (hour, t, i) fixed binary;
Line 865 ⟶ 1,342:
end;
end nautical;
</syntaxhighlight>
</lang>
 
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-Bell
{
Line 949 ⟶ 1,425:
Get-Bell -Hour $_ -Minute 30
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 983 ⟶ 1,459:
=={{header|Python}}==
As well as typing output to stdout, this program plays a sound for each bell as the ␇ characters are printed (The spaces between the ␇ characters are mirrored as varying delays between each ring).
<langsyntaxhighlight lang="python">import time, calendar, sched, winsound
 
duration = 750 # Bell duration in ms
Line 1,045 ⟶ 1,521:
if __name__ == '__main__':
ships_bell()</langsyntaxhighlight>
{{out}}
<pre>00:00, First watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
Line 1,101 ⟶ 1,577:
that displays \a by playing the system bell, it will play the system bell.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require racket/date)
Line 1,158 ⟶ 1,634:
(wait-til s)
(format-and-print hours (add1 bells))))
</syntaxhighlight>
</lang>
 
This might produce the following output:
Line 1,174 ⟶ 1,650:
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Raku uses [[wp:Coordinated_Universal_Time|UTC]] (GMT) time internally and by default. This will display the current UTC time and on the half hour, display a graphical representation of the bell. If run in a terminal with the system bell enabled, will also chime the system alarm bell.
 
<syntaxhighlight lang="raku" line>my @watch = <Middle Morning Forenoon Afternoon Dog First>;
my @ordinal = <One Two Three Four Five Six Seven Eight>;
my $thishour;
my $thisminute = '';
loop {
my $utc = DateTime.new(time);
if $utc.minute ~~ any(0,30) and $utc.minute != $thisminute {
$thishour = $utc.hour;
$thisminute = $utc.minute;
bell($thishour, $thisminute);
}
printf "%s%02d:%02d:%02d", "\r", $utc.hour, $utc.minute, $utc.second;
sleep(1);
}
sub bell ($hour, $minute) {
my $bells = (($hour % 4) * 2 + $minute div 30) || 8;
printf "%s%02d:%02d %9s watch, %6s Bell%s Gone: \t", "\b" x 9, $hour, $minute,
@watch[($hour div 4 - !?($minute + $hour % 4) + 6) % 6],
@ordinal[$bells - 1], $bells == 1 ?? '' !! 's';
chime($bells);
sub chime ($count) {
for 1..$count div 2 {
print "\a♫ ";
sleep .25;
print "\a";
sleep .75;
}
if $count % 2 {
print "\a♪";
sleep 1;
}
print "\n";
}
}</syntaxhighlight>
{{out}}
<pre>00:00 First watch, Eight Bells Gone: ♫ ♫ ♫ ♫
00:30 Middle watch, One Bell Gone: ♪
01:00 Middle watch, Two Bells Gone: ♫
01:30 Middle watch, Three Bells Gone: ♫ ♪
02:00 Middle watch, Four Bells Gone: ♫ ♫
02:30 Middle watch, Five Bells Gone: ♫ ♫ ♪
03:00 Middle watch, Six Bells Gone: ♫ ♫ ♫
03:30 Middle watch, Seven Bells Gone: ♫ ♫ ♫ ♪
04:00 Middle watch, Eight Bells Gone: ♫ ♫ ♫ ♫
04:30 Morning watch, One Bell Gone: ♪
05:00 Morning watch, Two Bells Gone: ♫
05:30 Morning watch, Three Bells Gone: ♫ ♪
06:00 Morning watch, Four Bells Gone: ♫ ♫
06:30 Morning watch, Five Bells Gone: ♫ ♫ ♪
07:00 Morning watch, Six Bells Gone: ♫ ♫ ♫
07:30 Morning watch, Seven Bells Gone: ♫ ♫ ♫ ♪
08:00 Morning watch, Eight Bells Gone: ♫ ♫ ♫ ♫
08:30 Forenoon watch, One Bell Gone: ♪
09:00 Forenoon watch, Two Bells Gone: ♫
09:30 Forenoon watch, Three Bells Gone: ♫ ♪
10:00 Forenoon watch, Four Bells Gone: ♫ ♫
10:30 Forenoon watch, Five Bells Gone: ♫ ♫ ♪
11:00 Forenoon watch, Six Bells Gone: ♫ ♫ ♫
11:30 Forenoon watch, Seven Bells Gone: ♫ ♫ ♫ ♪
12:00 Forenoon watch, Eight Bells Gone: ♫ ♫ ♫ ♫
12:30 Afternoon watch, One Bell Gone: ♪
13:00 Afternoon watch, Two Bells Gone: ♫
13:30 Afternoon watch, Three Bells Gone: ♫ ♪
14:00 Afternoon watch, Four Bells Gone: ♫ ♫
14:30 Afternoon watch, Five Bells Gone: ♫ ♫ ♪
15:00 Afternoon watch, Six Bells Gone: ♫ ♫ ♫
15:30 Afternoon watch, Seven Bells Gone: ♫ ♫ ♫ ♪
16:00 Afternoon watch, Eight Bells Gone: ♫ ♫ ♫ ♫
16:30 Dog watch, One Bell Gone: ♪
17:00 Dog watch, Two Bells Gone: ♫
17:30 Dog watch, Three Bells Gone: ♫ ♪
18:00 Dog watch, Four Bells Gone: ♫ ♫
18:30 Dog watch, Five Bells Gone: ♫ ♫ ♪
19:00 Dog watch, Six Bells Gone: ♫ ♫ ♫
19:30 Dog watch, Seven Bells Gone: ♫ ♫ ♫ ♪
20:00 Dog watch, Eight Bells Gone: ♫ ♫ ♫ ♫
20:30 First watch, One Bell Gone: ♪
21:00 First watch, Two Bells Gone: ♫
21:30 First watch, Three Bells Gone: ♫ ♪
22:00 First watch, Four Bells Gone: ♫ ♫
22:30 First watch, Five Bells Gone: ♫ ♫ ♪
23:00 First watch, Six Bells Gone: ♫ ♫ ♫
23:30 First watch, Seven Bells Gone: ♫ ♫ ♫ ♪</pre>
 
=={{header|REXX}}==
Line 1,179 ⟶ 1,750:
 
If any arguments are specified, that text is used as a prefix to the times shown (once a minute).
<br>Also, the number of bells sounded are shown &nbsp; (if any arguments are specified).
<br>If no arguments are specified, no times are shown.
 
Line 1,186 ⟶ 1,757:
This REXX program makes use of &nbsp; '''delay''' &nbsp; BIF, &nbsp; which delays (sleeps) for a specified amount of seconds.
 
Some REXXes don't have a &nbsp; '''delay''' &nbsp; BIF, &nbsp; so one is included &nbsp; <big> [[DELAY.REX|here]]. </big>
 
Also, some REXXes don't have a &nbsp; '''sound''' &nbsp; BIF, &nbsp; which produces sounds via the PC speaker,
&nbsp; so one is included &nbsp; <big> [[SOUND.REX|here]]. </big>
<langsyntaxhighlight lang="rexx">/*REXX program sounds "ship's bells" (using PC speaker) when executing (perpetually).*/
echo= ( arg()\==0) ) /*echo time and bells if any arguments.*/
signal on halt /*allow a clean way to stop the program*/
t.1= '00:30 01:00 01:30 02:00 02:30 03:00 03:30 04:00'
Line 1,197 ⟶ 1,768:
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=substr(t, 4, 2) /*the current time. */
ct=time('C') /*[↓] maybe add leading zero to time. */
hhmmc=left( right( ct, 7, 0), 5) /*HH:MM (maybe with a leading zero). */
if echo then say center(arg(1) ct, 79) /*echo the 1st argargument with the time? */
if ss\==00 & mn\==00 & mn\==30 then do; call delay /*wait for60-ss; the next minute ? */iterate
callend /* [↑] delay 60-ss;for fraction of a iterateminute.*/
end /* [] delaythe number of bells to minutepeel fraction{$}*/
do j=1 for 3 until $\==0; $=wordpos(hhmmc, /* [↓] number bells to peelt.*/j)
end do /*j=1 for 3 until $\==0; $=wordpos(hhmmc,t.j)*/
end /*j*/
 
if $\==0 & echo then say center($ "bells", 79) /*echo the number of bells ? */
 
do k=1 for $; call sound 650,1; call delay 1 + (k//2==0)
end /*k*/ /*[↑] peel, and then pause. for effect.*/
call delay 60; if rc\==0 then leave /*ensure we don't re─peel. /*ensure we don't re-peel. */
end /*forever*/
halt: /*stick a fork in it, we're all done. */</langsyntaxhighlight>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <big> <tt> the time is: </tt> </big>}}
<pre>
the time is: 1:48pm
Line 1,236 ⟶ 1,806:
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Nautical bell
 
m = 0
for n = 0 to 23
if n = 23
see "23" + ":30" + " = " + "7 bells" + nl
else
m = m + 1
see "" + n%23 + ":30" + " = " + m + " bells" + nl
ok
if n = 23
see "00" + ":00" + " = " + "8 bells" + nl
else
m = m + 1
see "" + (n%23+1) + ":00" + " = " + m + " bells" + nl
if m = 8
m = 0
ok
ok
next
</syntaxhighlight>
Output:
<pre>
0:30 = 1 bells
1:00 = 2 bells
1:30 = 3 bells
2:00 = 4 bells
2:30 = 5 bells
3:00 = 6 bells
3:30 = 7 bells
4:00 = 8 bells
4:30 = 1 bells
5:00 = 2 bells
5:30 = 3 bells
6:00 = 4 bells
6:30 = 5 bells
7:00 = 6 bells
7:30 = 7 bells
8:00 = 8 bells
8:30 = 1 bells
9:00 = 2 bells
9:30 = 3 bells
10:00 = 4 bells
10:30 = 5 bells
11:00 = 6 bells
11:30 = 7 bells
12:00 = 8 bells
12:30 = 1 bells
13:00 = 2 bells
13:30 = 3 bells
14:00 = 4 bells
14:30 = 5 bells
15:00 = 6 bells
15:30 = 7 bells
16:00 = 8 bells
16:30 = 1 bells
17:00 = 2 bells
17:30 = 3 bells
18:00 = 4 bells
18:30 = 5 bells
19:00 = 6 bells
19:30 = 7 bells
20:00 = 8 bells
20:30 = 1 bells
21:00 = 2 bells
21:30 = 3 bells
22:00 = 4 bells
22:30 = 5 bells
23:00 = 6 bells
23:30 = 7 bells
00:00 = 8 bells
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">watches = [ "First", "Middle", "Morning", "Forenoon", "Afternoon", "First dog", "Last dog", "First" ]
watch_ends = [ "00:00", "04:00", "08:00", "12:00", "16:00", "18:00", "20:00", "23:59" ]
words = ["One","Two","Three","Four","Five","Six","Seven","Eight"]
Line 1,261 ⟶ 1,906:
end
sleep 1
end</langsyntaxhighlight>
 
{{out|Sample output}}
Line 1,317 ⟶ 1,962:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "time.s7i";
include "duration.s7i";
Line 1,353 ⟶ 1,998:
flush(OUT);
end while;
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,408 ⟶ 2,053:
 
=={{header|Tcl}}==
This code was originally based on the [[#Perl 6Raku|Perl 6Raku]] version, but with numerous adaptations, alterations and (some) corrections.
<langsyntaxhighlight lang="tcl"># More sophisticated versions are possible, such as playing a bell sample
# using the Snack library.
proc ringTheBell {} {
Line 1,483 ⟶ 2,128:
fconfigure stdout -buffering none
every 1000 nauticalBell
vwait forever; # Only needed if not running an event loop otherwise</langsyntaxhighlight>
{{out|Sample output}}
<pre>
Line 1,492 ⟶ 2,137:
18:30 Last dog watch, Five Bells Gone: ♫ ♫ ♪
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-date}}
{{libheader|Wren-fmt}}
As Wren-cli currently has no way of telling what the local time is, we ask the user to input this when executing the script.
 
On my system (Ubuntu 20.04), the bell only rings once no matter how many \a's are concatenated togther, though it may be different on other systems.
<syntaxhighlight lang="wren">import "os" for Process
import "timer" for Timer
import "./date" for Date
import "./fmt" for Fmt
 
var watches = ["First", "Middle", "Morning", "Forenoon", "Afternoon", "Dog", "First"]
 
var args = Process.arguments
if (args.count == 0) {
System.print("Please enter current time in the format (24 hour clock): hh:mm:ss.")
return
}
 
var now = Date.parse(args[0], Date.isoTime)
while (true) {
var h = now.hour
var m = now.minute
var s = now.second
if ((m == 0 || m == 30) && s == 0) {
var bell = (m == 30) ? 1 : 0
var bells = (h*2 + bell) % 8
var watch = (h/4).floor + 1
if (bells == 0) {
bells = 8
watch = watch - 1
}
var sound = "\a" * bells
var pl = (bells != 1) ? "s" : ""
var w = watches[watch] + " watch"
if (watch == 5) {
if (bells < 5) {
w = "First " + w
} else {
w = "Last " + w
}
}
Fmt.lprint("$s$02d:$02d = $d bell$s : $s", [sound, h, m, bells, pl, w])
}
Timer.sleep(1000)
now = now.addSeconds(1)
}</syntaxhighlight>
 
{{out}}
<pre>
Similar to Go example.
</pre>
9,476

edits