Nautical bell: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(4 intermediate revisions by 3 users not shown) | |||
Line 22:
This version uses local time and speaks the bell rings using OS X's built-in speech synthesizer.
<
set {hours:h, minutes:m} to (current date)
if {0, 30} contains m then
Line 36:
end if
delay 60
end repeat</
==='idle' handler===
Line 42:
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.
<
use framework "Foundation"
use scripting additions
Line 85:
-- 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</
=={{header|AutoHotkey}}==
<
Hr := 0, min := 30, Bells := [], pattern := []
Loop 8 ; genrate 8 patterns
Line 107:
Bells[00 ":" 00] := Bells[24 ":" 00] , numBells := Bells[hh ":" mm]
return {"bells": numBells, "pattern": Pattern[numBells]}
}</
Example:<
loop, 24
{
Line 120:
}
MsgBox, 262144, , % "Time`tBells`tPattern`n" res
return</
Outputs:<pre>Time Bells Pattern
00:00 8 ** ** ** **
Line 174:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f NAUTICAL_BELL.AWK
BEGIN {
Line 226:
return(new_str)
}
</syntaxhighlight>
<p>Output:</p>
<pre>
Line 283:
Implementation corrected, sounds the system bell as per nautical standards, sounds bell as per local system time.
<syntaxhighlight lang="c">
#include<unistd.h>
#include<stdio.h>
Line 338:
}
</syntaxhighlight>
=={{header|C++}}==
This version uses local time.
<
#include <iostream>
#include <string>
Line 412:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
Output:
<pre>
Line 467:
=={{header|D}}==
This code uses local time instead of Greenwich Mean Time.
<
class NauticalBell : Thread {
Line 516:
writeln(e.msg);
}
}</
This output is from an actual test run.
<pre>
Line 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.
<
import (
Line 616 ⟶ 655:
time.Sleep(1 * time.Second)
}
}</
{{out}}
Line 631 ⟶ 670:
=={{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.
<
import Control.Concurrent
import Control.Monad
Line 710 ⟶ 749:
bellRinger = doWithScheduler (onInterval (30*60)) ringBells
</syntaxhighlight>
{{out}}
Line 724 ⟶ 763:
{{trans|Raku}}
'''Solution''':<
WATCH =: <;._1 ' Middle Morning Forenoon Afternoon Dog First'
Line 784 ⟶ 823:
end.
y
)</
'''Examples''': Invoke <tt>shipsWatch 0</tt>; the output is identical to Raku's.
Line 793 ⟶ 832:
This code uses UTC time.
{{trans|D}}
<
import java.text.SimpleDateFormat;
import java.util.TimeZone;
Line 842 ⟶ 881:
}
}
}</
Sample output:
Line 853 ⟶ 892:
=={{header|Julia}}==
<
"""
Line 894 ⟶ 933:
nauticalbelltask()
</
Nautical bell task starting -- next bell in 1201.726 seconds.
Nautical bell gong strikes 2 2 2
Line 919 ⟶ 958:
=={{header|Kotlin}}==
{{trans|Java}}
<
import java.text.DateFormat
Line 972 ⟶ 1,011:
}
}
}</
Sample output:
Line 985 ⟶ 1,024:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Works on version 11.2 ARM, a bug prevents this from working on version 11.3 Win64.
<
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}]]]</
=={{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>).
<
const
Line 1,033 ⟶ 1,072:
if m == 0:
nb(d.hour, d.minute)
sleep((1800 - m) * 1000) # In milliseconds.</
{{out}}
Line 1,040 ⟶ 1,079:
=={{header|OoRexx}}==
{{trans|REXX}}
<
Parse Arg msg
If msg='?' Then Do
Line 1,085 ⟶ 1,124:
Return res
halt:</
=={{header|Perl}}==
{{trans|Raku}}
<
binmode STDOUT, ":utf8";
use DateTime;
Line 1,132 ⟶ 1,171:
}
print "\n";
}</
{{out}}
<pre style="height:35ex">00:30 Middle watch, One Bell Gone: ♪
Line 1,189 ⟶ 1,228:
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>
Line 1,283 ⟶ 1,327:
=={{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">
nautical: procedure options (main); /* 29 October 2013 */
declare (hour, t, i) fixed binary;
Line 1,298 ⟶ 1,342:
end;
end nautical;
</syntaxhighlight>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function Get-Bell
{
Line 1,381 ⟶ 1,425:
Get-Bell -Hour $_ -Minute 30
}
</syntaxhighlight>
{{Out}}
<pre>
Line 1,415 ⟶ 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).
<
duration = 750 # Bell duration in ms
Line 1,477 ⟶ 1,521:
if __name__ == '__main__':
ships_bell()</
{{out}}
<pre>00:00, First watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
Line 1,533 ⟶ 1,577:
that displays \a by playing the system bell, it will play the system bell.
<
(require racket/date)
Line 1,590 ⟶ 1,634:
(wait-til s)
(format-and-print hours (add1 bells))))
</syntaxhighlight>
This might produce the following output:
Line 1,611 ⟶ 1,655:
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"
my @ordinal = <One Two Three Four Five Six Seven Eight>;
Line 1,651 ⟶ 1,695:
print "\n";
}
}</
{{out}}
<pre>00:00 First watch, Eight Bells Gone: ♫ ♫ ♫ ♫
Line 1,717 ⟶ 1,761:
Also, some REXXes don't have a '''sound''' BIF, which produces sounds via the PC speaker,
so one is included <big> [[SOUND.REX|here]]. </big>
<
echo= ( arg()\==0 ) /*echo time and bells if any arguments.*/
signal on halt /*allow a clean way to stop the program*/
Line 1,740 ⟶ 1,784:
call delay 60; if rc\==0 then leave /*ensure we don't re─peel. */
end /*forever*/
halt: /*stick a fork in it, we're all done. */</
{{out|output|text= when using the input of: <big> <tt> the time is: </tt> </big>}}
<pre>
Line 1,765 ⟶ 1,809:
=={{header|Ring}}==
<
# Project : Nautical bell
Line 1,786 ⟶ 1,830:
ok
next
</syntaxhighlight>
Output:
<pre>
Line 1,840 ⟶ 1,884:
=={{header|Ruby}}==
<
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,862 ⟶ 1,906:
end
sleep 1
end</
{{out|Sample output}}
Line 1,918 ⟶ 1,962:
=={{header|Seed7}}==
<
include "time.s7i";
include "duration.s7i";
Line 1,954 ⟶ 1,998:
flush(OUT);
end while;
end func;</
{{out}}
Line 2,010 ⟶ 2,054:
=={{header|Tcl}}==
This code was originally based on the [[#Raku|Raku]] version, but with numerous adaptations, alterations and (some) corrections.
<
# using the Snack library.
proc ringTheBell {} {
Line 2,084 ⟶ 2,128:
fconfigure stdout -buffering none
every 1000 nauticalBell
vwait forever; # Only needed if not running an event loop otherwise</
{{out|Sample output}}
<pre>
Line 2,102 ⟶ 2,146:
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.
<
import "timer" for Timer
import "./date" for Date
import "./fmt" for Fmt
var watches = ["First", "Middle", "Morning", "Forenoon", "Afternoon", "Dog", "First"]
Line 2,143 ⟶ 2,187:
Timer.sleep(1000)
now = now.addSeconds(1)
}</
{{out}}
|