In Ada, how can I get the current time in seconds as a value with which I can print and perform operations?

advertisements

I am doing a project for an operating systems class. I need to write a program that prints out the current time every ten seconds but also accounts for the delay of the overhead so that it does not drift when it has been running for a long time. I need it to be up to at least 1 decimal place as well.

I am stuck on step 1 as I can't figure out how to get the current time in seconds as a value. I have searched but could only find out how to get the current time in the HH:MM:SS format.

Thanks


Here's what I came up with:

writing_test.ads

package Writing_Test is

    protected Writer is
        entry write( Text : String; New_Line : Boolean:= True );
    end Writer;

    task Timer is
        entry Start;
        entry Pause;
        entry Stop;
    end Timer;

private

    Timer_Frequency : constant Duration:= 10.0;

end Writing_Test;

writing_test.adb

with
Ada.Calendar,
Ada.Text_IO;

package body Writing_Test is

    protected body Writer is
        entry write( Text : String; New_Line : Boolean:= True ) when True is
        begin
            Ada.Text_IO.Put( Text );

            if New_Line then
                Ada.Text_IO.New_Line;
            end if;
        end;
    end Writer;

    task body Timer is
        Active,
        Stop_Task : Boolean:= False;

        Next_Time : Ada.Calendar.Time;
        use type Ada.Calendar.Time;
    begin
        MAIN:
        loop
            if not Active then
                select
                    accept Start do
                        Active:= True;
                        Next_Time:= Ada.Calendar.Clock + Timer_Frequency;
                    end Start;
                or
                    terminate;
                end select;
            else
                select
                    accept Pause do
                        Active:= False;
                    end Pause;
                or
                    accept Stop do
                        Stop_Task:= True;
                    end Stop;
                or
                    delay until Next_Time;
                end select;

                exit MAIN when Stop_Task;

                if Active then
                    declare
                        Use Ada.Calendar;
                        Now : Time renames Clock;
                        Str : String renames
                          Day_Duration'Image( Ada.Calendar.Seconds(Now) );
    --' Formatter-correction trick
                    begin
                        Writer.write(Text => Str);
                        Next_Time:= Next_Time + Timer_Frequency;
                    end;
                end if;
            end if;
        end loop MAIN;

    end Timer;

end Writing_Test;