Get current time in microseconds

How can I get the current time in microseconds (formatted as integer64) in ada ?

I want to get the equivalent of C function clock_gettime (frome time.h).


  • It depends when you want the count to start. If you want uptime, you’ll need to handle clock_gettime(). If you want time since the Unix epoch, this might help:

    with Ada.Calendar;
    with Ada.Text_IO;
    procedure Get_Current_Time is
       type Integer_64 is range -2**63 .. 2**63 - 1;
       The_Unix_Epoch : constant Ada.Calendar.Time :=
         Ada.Calendar.Time_Of (Year  => 1970,
                               Month => 1,
                               Day   => 1);
       use type Ada.Calendar.Time;
       Time_Now : constant Ada.Calendar.Time := Ada.Calendar.Clock;
       Time_Since_Epoch : constant Duration := Time_Now - The_Unix_Epoch;
       In_Microseconds : constant Integer_64
         := Integer_64 (Time_Since_Epoch * 1.0e6);
       Ada.Text_IO.Put_Line ("result:" & In_Microseconds'Image);
    end Get_Current_Time;

    Or, instead of The_Unix_Epoch, you could store the current clock at the start of your program, if you just want elapsed time.