Averages/Mean time of day: Difference between revisions

Added Algol 68
No edit summary
(Added Algol 68)
 
(8 intermediate revisions by 7 users not shown)
Line 1:
{{task|Date and time}}
 
;Task
{{task heading}}
 
A particular activity of bats occurs at these times of the day:
Line 21:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">F mean_angle(angles)
V x = sum(angles.map(a -> cos(radians(a)))) / angles.len
V y = sum(angles.map(a -> sin(radians(a)))) / angles.len
Line 41:
R ‘#02:#02:#02’.format(h, m, s)
 
print(mean_time([‘23:00:17’, ‘23:40:20’, ‘00:12:45’, ‘00:17:19’]))</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
Line 49:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE PTR="CARD"
Line 138:
AverageTime(times,COUNT,t)
Print("Mean time is ") PrintTime(t)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Mean_time_of_day.png Screenshot from Atari 8-bit computer]
Line 146:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Calendar.Formatting;
with Ada.Command_Line;
with Ada.Numerics.Elementary_Functions;
Line 224:
Ada.Text_IO.Put_Line ("Usage: mean_time_of_day <time-1> ...");
Ada.Text_IO.Put_Line (" <time-1> ... 'HH:MM:SS' format");
end Mean_Time_Of_Day;</langsyntaxhighlight>
{{out}}
<pre>
% ./mean_time_of_day 23:00:17 23:40:20 00:12:45 00:17:19
23:47:43
</pre>
 
=={{header|ALGOL 68}}==
Uses code from the Averages/Mean angle task, included here for convenience.
<syntaxhighlight lang="algol68">
BEGIN # Mean time of day mapping time to angles #
 
# code from the Averages/Mean angle task - angles are in degrees #
PROC mean angle = ([]REAL angles)REAL:
(
INT size = UPB angles - LWB angles + 1;
REAL y part := 0, x part := 0;
FOR i FROM LWB angles TO UPB angles DO
x part +:= cos (angles[i] * pi / 180);
y part +:= sin (angles[i] * pi / 180)
OD;
arc tan2 (y part / size, x part / size) * 180 / pi
);
# end code from the Averages/Mean angle task #
 
MODE TIME = STRUCT( INT hh, mm, ss );
 
OP TOANGLE = ( TIME t )REAL: ( ( ( ( ( ss OF t / 60 ) + mm OF t ) / 60 ) + hh OF t ) * 360 ) / 24;
OP TOTIME = ( REAL a )TIME:
BEGIN
REAL t := ( a * 24 ) / 360;
WHILE t < 0 DO t +:= 24 OD;
WHILE t > 24 DO t -:= 24 OD;
INT hh = ENTIER t;
t -:= hh *:= 60;
INT mm = ENTIER t;
INT ss = ENTIER ( ( t - mm ) * 60 );
( hh, mm, ss )
END # TOTIME # ;
 
PROC mean time = ( []TIME times )TIME:
BEGIN
[ LWB times : UPB times ]REAL angles;
FOR i FROM LWB times TO UPB times DO angles[ i ] := TOANGLE times[ i ] OD;
TOTIME mean angle( angles )
END # mean time # ;
 
OP SHOW = ( TIME t )VOID:
BEGIN
PROC d2 = ( INT n )STRING: IF n < 10 THEN "0" ELSE "" FI + whole( n, 0 );
print( ( d2( hh OF t ), ":", d2( mm OF t ), ":", d2( ss OF t ) ) )
END # show time # ;
 
SHOW mean time( ( ( 23,00,17 ), ( 23,40,20 ), ( 00,12,45 ), ( 00,17,19 ) ) )
END
</syntaxhighlight>
{{out}}
<pre>
23:47:43
</pre>
Line 233 ⟶ 288:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox, % "The mean time is: " MeanTime(["23:00:17", "23:40:20", "00:12:45", "00:17:19"])
 
MeanTime(t, x=0, y=0) {
Line 253 ⟶ 308:
atan2(x, y) {
return dllcall("msvcrt\atan2", "Double",y, "Double",x, "CDECL Double")
}</langsyntaxhighlight>
{{out}}
<pre>The mean time is: 23:47:43</pre>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">#!/usr/bin/awk -f
{
c = atan2(0,-1)/(12*60*60);
Line 271 ⟶ 326:
if (p<0) p += 24*60*60;
print strftime("%T",p,1);
}</langsyntaxhighlight>
<pre>$ echo 23:00:17, 23:40:20, 00:12:45, 00:17:19 | awk -f mean_time_of_day.awk
23:47:43</pre>
Line 277 ⟶ 332:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> nTimes% = 4
DATA 23:00:17, 23:40:20, 00:12:45, 00:17:19
Line 315 ⟶ 370:
DEF FNatan2(y,x) : ON ERROR LOCAL = SGN(y)*PI/2
IF x>0 THEN = ATN(y/x) ELSE IF y>0 THEN = ATN(y/x)+PI ELSE = ATN(y/x)-PI</langsyntaxhighlight>
{{out}}
<pre>
Line 322 ⟶ 377:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include<stdlib.h>
#include<math.h>
#include<stdio.h>
Line 390 ⟶ 445:
meanTime.second);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 403 ⟶ 458:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Math;
 
namespace RosettaCode;
 
class Program
{
private const int SecondsPerDay = 60 * 60 * 24;
class Program
 
static void Main()
{
var digitimes = new List<TimeSpan>();
static void Main(string[] args)
{
Func<TimeSpan, double> TimeToDegrees = (time) =>
360 * time.Hours / 24.0 +
360 * time.Minutes / (24 * 60.0) +
360 * time.Seconds / (24 * 3600.0);
Func<List<double>, double> MeanAngle = (angles) =>
{
double y_part = 0.0d, x_part = 0.0d;
int numItems = angles.Count;
 
for Console.WriteLine(int"Enter itimes, =end 0;with ino <input: numItems"); i++)
while (true) {
string input x_part += MathConsole.CosReadLine(angles[i] * Math.PI / 180);
if (string.IsNullOrWhiteSpace(input)) break;
y_part += Math.Sin(angles[i] * Math.PI / 180);
if (TimeSpan.TryParse(input, out var digitime)) }{
digitimes.Add(digitime);
} else {
Console.WriteLine("Seems this is wrong input: ignoring time");
}
}
if(digitimes.Count() > 0)
Console.WriteLine($"The mean time is : {MeanTime(digitimes)}");
}
 
public static TimeSpan MeanTime(IEnumerable<TimeSpan> ts) => FromDegrees(MeanAngle(ts.Select(ToDegrees)));
return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI;
public static double ToDegrees(TimeSpan ts) => ts.TotalSeconds * 360d / SecondsPerDay;
};
public static TimeSpan FromDegrees(double degrees) => TimeSpan.FromSeconds((int)(degrees * SecondsPerDay / 360));
Func<double, TimeSpan> TimeFromDegrees = (angle) =>
new TimeSpan(
(int)(24 * 60 * 60 * angle / 360) / 3600,
((int)(24 * 60 * 60 * angle / 360) % 3600 - (int)(24 * 60 * 60 * angle / 360) % 60) / 60,
(int)(24 * 60 * 60 * angle / 360) % 60);
List<double> digitimes = new List<double>();
TimeSpan digitime;
string input;
 
public static double MeanAngle(IEnumerable<double> angles)
Console.WriteLine("Enter times, end with no input: ");
do{
var x = angles.Average(a {=> Cos(a * PI / 180));
var y = angles.Average(a => Sin(a * PI input/ = Console.ReadLine(180));
return (Atan2(y, x) * 180 / PI + if360) (!(string.IsNullOrWhiteSpace(input)))% 360;
{
if (TimeSpan.TryParse(input, out digitime))
digitimes.Add(TimeToDegrees(digitime));
else
Console.WriteLine("Seems this is wrong input: ignoring time");
}
} while (!string.IsNullOrWhiteSpace(input));
 
if(digitimes.Count() > 0)
Console.WriteLine("The mean time is : {0}", TimeFromDegrees(360 + MeanAngle(digitimes)));
}
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 471 ⟶ 512:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 536 ⟶ 577:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
Line 542 ⟶ 583:
=={{header|Common Lisp}}==
{{trans|Echo Lisp}}
<langsyntaxhighlight lang="lisp">;; * Loading the split-sequence library
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '("split-sequence")))
Line 582 ⟶ 623:
(reduce #'+ (mapcar (lambda (time)
(make-polar 1 (time->radian time))) times)))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 590 ⟶ 631:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.range, std.algorithm, std.complex, std.math,
std.format, std.conv;
 
Line 628 ⟶ 669:
void main() @safe {
["23:00:17", "23:40:20", "00:12:45", "00:17:19"].meanTime.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
Line 635 ⟶ 676:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Averages_Mean_time_of_day;
 
Line 691 ⟶ 732:
writeln(TimeToStr(MeanTime(ToTimes(Inputs))));
readln;
end.</langsyntaxhighlight>
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
func tm2deg t$ .
t[] = number strsplit t$ ":"
return 360 * t[1] / 24.0 + 360 * t[2] / (24 * 60.0) + 360 * t[3] / (24 * 3600.0)
.
func$ deg2tm deg .
len t[] 3
h = floor (24 * 60 * 60 * deg / 360)
t[3] = h mod 60
h = h div 60
t[2] = h mod 60
t[1] = h div 60
for h in t[]
if h < 10
s$ &= 0
.
s$ &= h
s$ &= ":"
.
return substr s$ 1 8
.
func mean ang[] .
for ang in ang[]
x += cos ang
y += sin ang
.
return atan2 (y / len ang[]) (x / len ang[])
.
in$ = "23:00:17 23:40:20 00:12:45 00:17:19"
for s$ in strsplit in$ " "
ar[] &= tm2deg s$
.
print deg2tm (360 + mean ar[])
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; string hh:mm:ss to radians
(define (time->radian time)
Line 715 ⟶ 793:
(mean-time '{"23:00:17" "23:40:20" "00:12:45" "00:17:19"})
→ "23:47:43"
</syntaxhighlight>
</lang>
 
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( mean_time_of_day ).
-export( [from_times/1, task/0] ).
Line 750 ⟶ 827:
Secs = Seconds - (Hours * 3600) - (Minutes * 60),
lists:flatten( io_lib:format("~2.10.0B:~2.10.0B:~2.10.0B", [Hours, Minutes, Secs]) ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 759 ⟶ 836:
=={{header|Euphoria}}==
{{works with|OpenEuphoria}}
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include std/console.e
include std/math.e
Line 823 ⟶ 900:
if getc(0) then end if
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 836 ⟶ 913:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
open System.Numerics
 
Line 856 ⟶ 933:
|> deg2time |> fun t -> t.ToString(@"hh\:mm\:ss")
|> printfn "%s: %s" msg
0</langsyntaxhighlight>
{{out}}
<pre>>RosettaCode 23:00:17 23:40:20 00:12:45 00:17:19
Line 862 ⟶ 939:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays formatting kernel math math.combinators
math.functions math.libm math.parser math.trig qw sequences
splitting ;
Line 887 ⟶ 964:
input dup mean-time "Mean time for %u is %s.\n" printf ;
 
MAIN: mean-time-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 895 ⟶ 972:
=={{header|Fortran}}==
{{works with|gfortran 5.1.0}}
<langsyntaxhighlight lang="fortran">
program mean_time_of_day
implicit none
Line 966 ⟶ 1,043:
end function
end program
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 973 ⟶ 1,050:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Const pi As Double = 3.1415926535897932
Line 1,026 ⟶ 1,103:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,034 ⟶ 1,111:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,083 ⟶ 1,160:
_, dayFrac := math.Modf(1 + math.Atan2(ssum, csum)/(2*math.Pi))
return mean.Add(time.Duration(dayFrac * 24 * float64(time.Hour))), nil
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,091 ⟶ 1,168:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">import static java.lang.Math.*
 
final format = 'HH:mm:ss', clock = PI / 12, millisPerHr = 3600*1000
Line 1,101 ⟶ 1,178:
def times = timeStrings.collect(parseTime)
formatTime(atan2( mean(times) { sin(it * clock) }, mean(times) { cos(it * clock) }) / clock)
}</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="groovy">println (meanTime("23:00:17", "23:40:20", "00:12:45", "00:17:19"))</langsyntaxhighlight>
 
{{out}}
Line 1,110 ⟶ 1,187:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Complex (cis, phase)
import Data.List.Split (splitOn)
import Text.Printf (printf)
Line 1,137 ⟶ 1,214:
main :: IO ()
main = putStrLn $ radiansToTime $ meanAngle $ map timeToRadians ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
</syntaxhighlight>
</lang>
{{out}}
<pre>23:47:43</pre>
Line 1,143 ⟶ 1,220:
=={{header|Icon}} and {{header|Unicon}}==
 
<syntaxhighlight lang="text">procedure main(A)
every put(B := [], ct2a(!A))
write(ca2t(meanAngle(B)))
Line 1,166 ⟶ 1,243:
every (sumCosines := 0.0) +:= cos(dtor(!A))
return rtod(atan(sumSines/*A,sumCosines/*A))
end</langsyntaxhighlight>
 
Sample run:
Line 1,178 ⟶ 1,255:
=={{header|J}}==
use <code>avgAngleR</code> from [[Averages/Mean angle#J]]
<langsyntaxhighlight Jlang="j">require 'types/datetime'
parseTimes=: ([: _&".;._2 ,&':');._2
secsFromTime=: 24 60 60 #. ] NB. convert from time to seconds
rft=: 2r86400p1 * secsFromTime NB. convert from time to radians
meanTime=: 'hh:mm:ss' fmtTime [: secsFromTime [: avgAngleR&.rft parseTimes</langsyntaxhighlight>
{{out|Example Use}}
<langsyntaxhighlight Jlang="j"> meanTime '23:00:17 23:40:20 00:12:45 00:17:19 '
23:47:43</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">public class MeanTimeOfDay {
static double meanAngle(double[] angles) {
Line 1,236 ⟶ 1,313:
System.out.println("Average time is : " + degreesToTime(mean));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,245 ⟶ 1,322:
=={{header|Javascript}}==
{{works with|Node.js}}
<langsyntaxhighlight Javascriptlang="javascript">var args = process.argv.slice(2);
 
function time_to_seconds( hms ) {
Line 1,285 ⟶ 1,362:
var seconds = Math.floor( sum / count )
console.log( 'Mean time is ', seconds_to_time(seconds));
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,295 ⟶ 1,372:
 
The "mean time" of two times that differ by 12 hours (e.g. ["00:00:00", "12:00:00"]) is not very well-defined, and is accordingly computed as null here.
<langsyntaxhighlight lang="jq"># input: array of "h:m:s"
def mean_time_of_day:
def pi: 4 * (1|atan);
Line 1,325 ⟶ 1,402:
| round
| secs2time
end ;</langsyntaxhighlight>
'''Examples'''
<langsyntaxhighlight lang="jq">["0:0:0", "12:0:0" ],
["0:0:0", "24:0:0" ],
["1:0:0", "1:0:0" ],
Line 1,334 ⟶ 1,411:
["23:0:0", "23:0:0" ],
["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
| mean_time_of_day</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -r -n -f Mean_time_of_day.jq
null
00:00:00
Line 1,343 ⟶ 1,420:
00:00:01
23:00:00
23:47:43</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|MATLAB}}
<langsyntaxhighlight lang="julia">using Statistics
 
function meantime(times::Array, dlm::String=":")
Line 1,367 ⟶ 1,444:
println("Times:")
println.(times)
println("Mean: $mtime")</langsyntaxhighlight>
 
{{out}}
Line 1,379 ⟶ 1,456:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun meanAngle(angles: DoubleArray): Double {
Line 1,414 ⟶ 1,491:
val mean = meanAngle(angles)
println("Average time is : ${degreesToTime(mean)}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,422 ⟶ 1,499:
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
global pi
pi = acs(-1)
Line 1,468 ⟶ 1,545:
atan2 = (y=0)*(x<0)*pi
End Function
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,481 ⟶ 1,558:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">
local times = {"23:00:17","23:40:20","00:12:45","00:17:19"}
 
Line 1,516 ⟶ 1,593:
 
print(angleToTime(meanAngle(times)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,524 ⟶ 1,601:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">meanTime[list_] :=
StringJoin@
Riffle[ToString /@
Line 1,531 ⟶ 1,608:
Exp[FromDigits[ToExpression@StringSplit[#, ":"], 60] & /@
list/(24*60*60) 2 Pi I]]]/(2 Pi)], ":"];
meanTime[{"23:00:17", "23:40:20", "00:12:45", "00:17:19"}]</langsyntaxhighlight>
{{Out}}
<pre>23:47:43</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function t = mean_time_of_day(t)
c = pi/(12*60*60);
for k=1:length(t)
Line 1,545 ⟶ 1,622:
if (d<0) d += 1;
t = datestr(d,"HH:MM:SS");
end; </langsyntaxhighlight>
<pre>mean_time_of_day({'23:00:17', '23:40:20', '00:12:45', '00:17:19'})
ans = 23:47:43
Line 1,552 ⟶ 1,629:
=={{header|Nim}}==
{{works with|Nim|0.20.0+}}
<langsyntaxhighlight lang="nim">import math, complex, strutils, sequtils
proc meanAngle(deg: openArray[float]): float =
Line 1,571 ⟶ 1,648:
align($h, 2, '0') & ":" & align($m, 2, '0') & ":" & align($s, 2, '0')
echo meanTime(["23:00:17", "23:40:20", "00:12:45", "00:17:19"])</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
Line 1,577 ⟶ 1,654:
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE AvgTimeOfDay;
IMPORT
Line 1,648 ⟶ 1,725:
 
END AvgTimeOfDay.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,655 ⟶ 1,732:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let pi_twice = 2.0 *. 3.14159_26535_89793_23846_2643
let day = float (24 * 60 * 60)
 
Line 1,691 ⟶ 1,768:
Printf.printf "The mean time of [%s] is: %s\n"
(String.concat "; " times)
(string_of_time (mean_time (List.map parse_time times)))</langsyntaxhighlight>
{{out}}
The mean time of [23:00:17; 23:40:20; 00:12:45; 00:17:19] is: 23:47:43
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 25.06.2014 Walter Pachl
*--------------------------------------------------------------------*/
Line 1,731 ⟶ 1,808:
f2: return right(format(arg(1),2,0),2,0)
 
::requires rxmath library</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">meanAngle(v)=atan(sum(i=1,#v,sin(v[i]))/sum(i=1,#v,cos(v[i])))%(2*Pi)
meanTime(v)=my(x=meanAngle(2*Pi*apply(u->u[1]/24+u[2]/1440+u[3]/86400, v))*12/Pi); [x\1, 60*(x-=x\1)\1, round(60*(60*x-60*x\1))]
meanTime([[23,0,17], [23,40,20], [0,12,45], [0,17,19]])</langsyntaxhighlight>
{{out}}
<pre>[23, 47, 43]</pre>
 
=={{header|Perl}}==
===Traditional===
Using the core module <code>Math::Complex</code> to enable use of complex numbers. The <code>POSIX</code> CPAN module provides the <code>fmod</code> routine for non-integer modulus calculations.
{{trans|Raku}}
<syntaxhighlight lang Perl="perl">use POSIX 'fmod'strict;
use warnings;
use POSIX 'fmod';
use Math::Complex;
use List::Util qw(sum);
Line 1,776 ⟶ 1,856:
 
@times = ("23:00:17", "23:40:20", "00:12:45", "00:17:19");
print mean_time(@times) . " is the mean time of " . join(' ', @times) . "\n";</langsyntaxhighlight>
{{out}}
<pre>23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19</pre>
===v5.36===
As previous, but using features from an up-to-date release of Perl, e.g. strict/warn/subroutine signatures without the <code>use</code> boilerplate.
<syntaxhighlight lang="perl">use v5.36;
use POSIX 'fmod';
use Math::Complex;
use List::Util 'sum';
use utf8;
 
use constant τ => 2 * 2 * atan2(1, 0);
 
sub R_to_ToD ($radians) { my $x = $radians * 86400 / τ; sprintf '%02d:%02d:%02d', fm($x/3600,24), fm($x/60,60), fm($x,60) }
sub ToD_to_R ($h,$m,$s) { (3600*$h + 60*$m + $s) * τ / 86400 }
sub fm ($n,$b) { my $x = fmod($n,$b); $x += $b if $x < 0 }
sub cis ($radians) { cos($radians) + i*sin($radians) }
sub phase ($Θ) { arg( $Θ ) }
sub mean_time(@t) { R_to_ToD phase sum map { cis ToD_to_R split ':', $_ } @t }
 
my @times = <23:00:17 23:40:20 00:12:45 00:17:19>;
say my $result = mean_time(@times) . ' is the mean time of ' . join ' ', @times;</syntaxhighlight>
{{out}}
<pre>23:47:43 is the mean time of 23:00:17 23:40:20 00:12:45 00:17:19</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">MeanAngle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">angles</span><span style="color: #0000FF;">)</span>
Line 1,816 ⟶ 1,917:
<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;">"Mean Time is %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">toHMS</span><span style="color: #0000FF;">(</span><span style="color: #000000;">MeanAngle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Times</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,823 ⟶ 1,924:
 
=={{header|PHP}}==
<syntaxhighlight lang="php">
<lang PHP>
<?php
function time2ang($tim) {
Line 1,861 ⟶ 1,962:
print "The mean time of day is $result (angle $ma).\n";
?>
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,869 ⟶ 1,970:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/math.l")
 
(de meanTime (Lst)
Line 1,878 ⟶ 1,979:
(sum '((S) (cos (*/ ($tim S) pi 43200))) Lst) )
43200 pi )
(tim$ (% (+ Tim 86400) 86400) T) ) )</langsyntaxhighlight>
{{out|Test}}
<langsyntaxhighlight PicoLisplang="picolisp">: (meanTime '("23:00:17" "23:40:20" "00:12:45" "00:17:19"))
-> "23:47:43"</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">*process source attributes xref;
avt: Proc options(main);
/*--------------------------------------------------------------------
Line 1,929 ⟶ 2,030:
End;
 
End;</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Get-MeanTimeOfDay
{
Line 2,001 ⟶ 2,102:
}
}
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
[timespan]$meanTimeOfDay = "23:00:17","23:40:20","00:12:45","00:17:19" | Get-MeanTimeOfDay
"Mean time is {0}" -f (Get-Date $meanTimeOfDay.ToString()).ToString("hh:mm:ss tt")
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,012 ⟶ 2,113:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from cmath import rect, phase
from math import radians, degrees
 
Line 2,035 ⟶ 2,136:
 
if __name__ == '__main__':
print( mean_time(["23:00:17", "23:40:20", "00:12:45", "00:17:19"]) )</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define (mean-angle/radians as)
Line 2,061 ⟶ 2,162:
(loop q (cons (~r r #:min-width 2 #:pad-string "0") ts))))))
(mean-time '("23:00:17" "23:40:20" "00:12:45" "00:17:19"))
</syntaxhighlight>
</lang>
{{out}}
<pre>"23:47:43"</pre>
Line 2,069 ⟶ 2,170:
{{works with|Rakudo|2015.12}}
 
<syntaxhighlight lang="raku" perl6line>sub tod2rad($_) { [+](.comb(/\d+/) Z* 3600,60,1) * tau / 86400 }
sub rad2tod ($r) {
Line 2,082 ⟶ 2,183:
my @times = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"];
say "{ mean-time(@times) } is the mean time of @times[]";</langsyntaxhighlight>
 
{{out}}
Line 2,090 ⟶ 2,191:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 25.06.2014 Walter Pachl
* taken from ooRexx using my very aged sin/cos/artan functions
Line 2,189 ⟶ 2,290:
End
Numeric Digits (prec)
Return r+0</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ → angles
≪ 0 1 angles SIZE '''FOR''' j
1 angles j GET R→C P→R + '''NEXT'''
angles SIZE / ARG
≫ ≫ ''''MEANG'''' STO
 
≪ → times
≪ { } 1 times SIZE '''FOR''' j
1 times j GET HMS→ 15 * + '''NEXT'''
'''MEANG''' 15 / 24 MOD →HMS 4 FIX RND STD
≫ ≫ ''''MTIME'''' STO
 
{23.0017 23.4020 0.1245 0.1719 } '''MTIME'''
{{out}}
<pre>
1: 23.4743
</pre>
 
=={{header|Ruby}}==
Using the methods at [[http://rosettacode.org/wiki/Averages/Mean_angle#Ruby|Averages/Mean angle]]
 
<langsyntaxhighlight lang="ruby">def time2deg(t)
raise "invalid time" unless m = t.match(/^(\d\d):(\d\d):(\d\d)$/)
hh,mm,ss = m[1..3].map {|e| e.to_i}
Line 2,214 ⟶ 2,335:
end
 
puts mean_time ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]</langsyntaxhighlight>
{{out}}
23:47:43
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">global pi
pi = acs(-1)
Line 2,262 ⟶ 2,383:
atan2 = (y=0)*(x<0)*pi
end if
End Function</langsyntaxhighlight>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::f64::consts::PI;
 
Line 2,327 ⟶ 2,448:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,334 ⟶ 2,455:
 
=={{header|Scala}}==
{{libheader|java.time.LocalTime}}<langsyntaxhighlight Scalalang="scala">import java.time.LocalTime
import scala.compat.Platform
 
Line 2,363 ⟶ 2,484:
assert(LocalTime.MIN.plusSeconds(meanAngle(times, dayInSeconds).round).toString == "23:47:40")
println(s"Successfully completed without errors. [total ${Platform.currentTime - executionStart} ms]")
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
Line 2,370 ⟶ 2,491:
To be self-contained, this starts with the functions from [[Averages/Mean angle]]
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme inexact)
Line 2,423 ⟶ 2,544:
(write (mean-time-of-day '("23:00:17" "23:40:20" "00:12:45" "00:17:19")))
(newline)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,433 ⟶ 2,554:
{{trans|Ruby}}
Using the '''mean_angle()''' function from: [http://rosettacode.org/wiki/Averages/Mean_angle#Sidef "Averages/Mean angle"]
<langsyntaxhighlight lang="ruby">func time2deg(t) {
(var m = t.match(/^(\d\d):(\d\d):(\d\d)$/)) || die "invalid time"
var (hh,mm,ss) = m.cap.map{.to_i}...
Line 2,449 ⟶ 2,570:
}
 
say mean_time(["23:00:17", "23:40:20", "00:12:45", "00:17:19"])</langsyntaxhighlight>
{{out}}
<pre>23:47:43</pre>
Line 2,455 ⟶ 2,576:
=={{header|SQL}}/{{header|PostgreSQL}}==
{{trans|Python}}
<syntaxhighlight lang="sql">
<lang SQL>
--Setup table for testing
CREATE TABLE time_table(times time);
Line 2,468 ⟶ 2,589:
(SELECT EXTRACT(epoch from times) t
FROM time_table) T1
)T2</langsyntaxhighlight>
 
Output:
Line 2,477 ⟶ 2,598:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
@inlinable public func d2r<T: FloatingPoint>(_ f: T) -> T { f * .pi / 180 }
Line 2,533 ⟶ 2,654:
let meanTime = DigitTime(fromDegrees: 360 + meanOfAngles(times.map({ $0.toDegrees() })))
 
print("Given times \(times), the mean time is \(meanTime)")</langsyntaxhighlight>
 
{{out}}
Line 2,540 ⟶ 2,661:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc meanTime {times} {
set secsPerRad [expr {60 * 60 * 12 / atan2(0,-1)}]
set sumSin [set sumCos 0.0]
Line 2,557 ⟶ 2,678:
}
 
puts [meanTime {23:00:17 23:40:20 00:12:45 00:17:19}]</langsyntaxhighlight>
{{out}}
23:47:43
Line 2,563 ⟶ 2,684:
=={{header|VBA}}==
Uses Excel and [[Averages/Mean_angle#VBA|mean angle]].
<langsyntaxhighlight lang="vb">Public Sub mean_time()
Dim angles() As Double
s = [{"23:00:17","23:40:20","00:12:45","00:17:19"}]
Line 2,570 ⟶ 2,691:
Next i
Debug.Print Format(mean_angle(s) / 360 + 1, "hh:mm:ss")
End Sub</langsyntaxhighlight>{{out}}
<pre>23:47:43</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function TimeToDegrees(time As TimeSpan) As Double
Line 2,620 ⟶ 2,741:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Enter times, end with no input:
Line 2,630 ⟶ 2,751:
The mean time is : 23:47:43</pre>
 
=={{header|V (Vlang)}}==
{{trans|Wren}}
<syntaxhighlight lang="v (vlang)">import math
 
const inputs = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
Line 2,670 ⟶ 2,791:
s := t[2].f64()
return (h + m + s) / 240
}</langsyntaxhighlight>
 
{{out}}
Line 2,679 ⟶ 2,800:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var timeToDegs = Fn.new { |time|
Line 2,696 ⟶ 2,817:
s = m % 60
m = (m / 60).floor
return "%(Fmt.dswrite(2"$2d:$2d:$2d", h)):%(Fmt.d(2, m)):%(Fmt.d(2, s))"
}
 
Line 2,712 ⟶ 2,833:
var times = ["23:00:17", "23:40:20", "00:12:45", "00:17:19"]
var angles = times.map { |t| timeToDegs.call(t) }.toList
System.print("Mean time of day is : %(degsToTime.call(meanAngle.call(angles)))")</langsyntaxhighlight>
 
{{out}}
Line 2,720 ⟶ 2,841:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
 
proc NumOut(N); \Display 2-digit N with leading zero
Line 2,756 ⟶ 2,877:
];
 
TimeOut(MeanTime([4, [23,00,17], [23,40,20], [00,12,45], [00,17,19]]))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,764 ⟶ 2,885:
=={{header|Yabasic}}==
{{trans|Phix}}
<langsyntaxhighlight Yabasiclang="yabasic">sub atan2(y, x)
return 2 * atan((sqrt(x **2 + y ** 2) - x) / y)
end sub
Line 2,809 ⟶ 2,930:
 
// Output: Mean Time is 23:47:43
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
Line 2,815 ⟶ 2,936:
to task "Averages/Mean angle" and some on-the-fly
time-to-angle and back conversions.
<langsyntaxhighlight lang="zkl">var D=Time.Date;
fcn meanT(t1,t2,etc){
ts:=vm.arglist.apply(fcn(hms){
Line 2,826 ⟶ 2,947:
if(mt<0) mt+=24; //-0.204622-->23.7954
D.toHour(mt).concat(":")
}</langsyntaxhighlight>
Time.Date.toFloat/toHour convert 24hr HMS to fractional time and back. Multiplying fractional time by 360/24=15 yields angle.
{{out}}
3,032

edits