Task: Averages_Mean time of day





Java: public class MeanTimeOfDay {
    
    static double meanAngle(double[] angles) {
        int len = angles.length;
        double sinSum = 0.0;
        for (int i = 0; i < len; i++) {
            sinSum += Math.sin(angles[i] * Math.PI / 180.0);
        }
 
        double cosSum = 0.0;
        for (int i = 0; i < len; i++) {
            cosSum += Math.cos(angles[i] * Math.PI / 180.0);
        }

        return Math.atan2(sinSum / len, cosSum / len) * 180.0 / Math.PI;
    }

    
    static int timeToSecs(String t) {
        int hours = Integer.parseInt(t.substring(0, 2));
        int mins  = Integer.parseInt(t.substring(3, 5));
        int secs  = Integer.parseInt(t.substring(6, 8));
        return 3600 * hours + 60 * mins + secs;
    }

    
    static double timeToDegrees(String t) {
        return timeToSecs(t) / 240.0;
    }

    static String degreesToTime(double d) {
        if (d < 0.0) d += 360.0;
        int secs  = (int)(d * 240.0);
        int hours = secs / 3600;
        int mins  = secs % 3600;
        secs = mins % 60;
        mins /= 60;
        return String.format("%2d:%2d:%2d", hours, mins, secs);
    }

    public static void main(String[] args) {
        String[] tm = {"23:00:17", "23:40:20", "00:12:45", "00:17:19"};
        double[] angles = new double[4];
        for (int i = 0; i < 4; i++) angles[i] = timeToDegrees(tm[i]);        
        double mean = meanAngle(angles);
        System.out.println("Average time is : " + degreesToTime(mean));
    }
}







Ada: with Ada.Calendar.Formatting;
with Ada.Command_Line;
with Ada.Numerics.Elementary_Functions;
with Ada.Strings.Fixed;
with Ada.Text_IO;

procedure Mean_Time_Of_Day is

   subtype Time        is Ada.Calendar.Time;
   subtype Time_Of_Day is Ada.Calendar.Day_Duration;
   subtype Time_String is String (1 .. 8);  

   type Time_List is array (Positive range <>) of Time_String;

   function Average_Time (List : Time_List) return Time_String is

      function To_Time (Time_Image : Time_String) return Time
      is (Ada.Calendar.Formatting.Value ("2000-01-01 " & Time_Image));

      function To_Time_Of_Day (TS : Time) return Time_Of_Day is
         use Ada.Calendar.Formatting;
         Hour_Part : constant Time_Of_Day := 60.0 * 60.0 * Hour (TS);
         Min_Part  : constant Time_Of_Day := 60.0 * Minute (TS);
         Sec_Part  : constant Time_Of_Day := Time_Of_Day (Second (TS));
      begin
         return Hour_Part + Min_Part + Sec_Part;
      end To_Time_Of_Day;

      function To_Time_Image (Angle : Time_Of_Day) return Time_String
      is
         use Ada.Calendar.Formatting;
         TOD : constant Time := Time_Of
           (Year => 2000, Month => 1, Day => 1,  
            Seconds => Angle);
      begin
         return Ada.Strings.Fixed.Tail (Image (TOD), Time_String'Length);
      end To_Time_Image;

      function Average_Time_Of_Day (List : Time_List) return Time_Of_Day is
         use Ada.Numerics.Elementary_Functions;
         Cycle : constant Float := Float (Time_Of_Day'Last);
         X_Sum, Y_Sum : Float := 0.0;
         Angle        : Float;
      begin
         for Time_Stamp of List loop
            Angle := Float (To_Time_Of_Day (To_Time (Time_Stamp)));
            X_Sum := X_Sum + Cos (Angle, Cycle => Cycle);
            Y_Sum := Y_Sum + Sin (Angle, Cycle => Cycle);
         end loop;
         Angle := Arctan (Y_Sum, X_Sum, Cycle => Cycle);
         if Angle < 0.0 then
            Angle := Angle + Cycle;
         elsif Angle > Cycle then
            Angle := Angle - Cycle;
         end if;
         return Time_Of_Day (Angle);
      end Average_Time_Of_Day;

   begin
      return To_Time_Image (Average_Time_Of_Day (List));
   end Average_Time;

   use Ada.Command_Line;
   List : Time_List (1 .. Argument_Count);
begin
   if Argument_Count = 0 then
      raise Constraint_Error;
   end if;

   for A in 1 .. Argument_Count loop
      List (A) := Argument (A);
   end loop;
   Ada.Text_IO.Put_Line (Average_Time (List));

exception
   when others =>
      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;