在Ada中,如何以秒为单位获取当前时间作为我可以打印并执行操作的值?

时间:2013-12-05 20:10:50

标签: time ada seconds

我正在为一个操作系统类做一个项目。我需要编写一个程序,每10秒打印一次当前时间,但也考虑到开销的延迟,以便在它运行很长时间后不会漂移。我需要它至少达到小数点后1位。

我被困在第1步,因为我无法弄清楚如何以秒为单位获取当前时间作为值。我搜索过但只能找到如何以HH:MM:SS格式获取当前时间。

由于

2 个答案:

答案 0 :(得分:0)

这是我想出的:

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;

答案 1 :(得分:0)

这是一个简单的程序,可以完成“每10秒打印”一部分工作。您可以在单独的包中轻松使用此代码。

with Ada.Text_IO;
with Ada.Calendar;

procedure Periodic_Printer is

   task type My_Printer_Task is
   end My_Printer_Task;

   task body My_Printer_Task is
      use Ada.Calendar; -- for the "-" and "+" operations on Time
      Start_Time : Ada.Calendar.Time;
      Next_Cycle : Ada.Calendar.Time;
      Period     : constant Duration  := 10.0;
   begin
      Start_Time := Ada.Calendar.Clock;
      Next_Cycle := Start_Time;
      loop
         Ada.Text_IO.Put_Line(Duration'Image(Ada.Calendar.Clock - Start_Time)); --'
         -- You could use Next_Cycle instead of Ada.Calendar.Clock - Start_Time
         -- so the printing does not depend of the time needed to do the elapsed
         -- time calculation

         delay 3.0; -- like a long operation, takes time......
                    -- This pattern assumes the each cycle last less than Period
                    -- If you cannot ensure that, you should consider improving
                    -- the pattern or reduce the computation load of each cycle

         Next_Cycle := Next_Cycle + Period;
         delay until Next_Cycle;
      end loop;
   end My_Printer_Task;

   Printer : My_Printer_Task;
begin
   delay 90.0; -- You can do your 'real work' here.

   -- Unclean way to terminate a task, you should consider improve it for a
   -- real world scenario
   abort Printer;

   Ada.Text_IO.Put_Line("End of program");
end Periodic_Printer;