How to print variable Ada.Real_Time.Time
How to print variable Ada.Real_Time.Time?
procedure Main is
test : TValue :=
(value => 0.7,
timeStamp => Clock,
status => (Valid => False, Unknown => True)
);
begin
-- print of Test.value
Put(Item => Test.value ,Fore => 5, Aft => 3, Exp => 0);
-- here I want a print of timestamp
end Main;
I want to print timeStamp to console, how do I do that? I tried to convert it to String or Integer but with no success
An important section to read is LRM D.8 , which defines package Ada.Real_Time
. In particular, it is worth noting paragraph 19 , which says that the era is not indicated by language. This means that Ada.Real_Time.Time
you need to define the epoch yourself to print the variables . One such epoch could be the launch time of the application under test:
with Ada.Real_Time; package Real_Time_IO is ... private Epoch : constant Ada.Real_Time.Time := Ada.Real_Time.Clock; end Real_Time_IO;
Now you can calculate the time intervals since the application was launched:
package body Real_Time_IO is function Since_Start return Ada.Real_Time.Time_Span is begin return Ada.Real_Time.Clock - Epoch; end Since_Start; ...
If we only need to use this package 24 hours after starting the application, we can be lazy and just convert the result Since_Start
to a type Duration
and then to a string:
function Since_Start return Duration is begin return Ada.Real_Time.To_Duration (Since_Start); end Since_Start; function Since_Start return String is begin return Duration'Image (Since_Start); end Since_Start;
... and I hope you know how to print lines. :-)
source to share
After some research, here's a quick example of how to print Real_Time.Time
with Ada.Real_Time; use Ada.Real_Time;
with Ada.Calendar;
with Ada.Calendar.Formatting;
with Ada.Text_IO; use Ada.Text_IO;
procedure PrintRT is
The_Clock : Ada.Real_Time.Time := Ada.Real_Time.Clock;
-- Convert to Time_Span
As_Time_Span : Ada.Real_Time.Time_Span := The_Clock - Time_Of(0, Time_Span_Zero);
-- Epoch ?
Epoch : constant Ada.Calendar.Time := Ada.Calendar.Time_Of(1970, 01, 01);
Dur : Duration := Ada.Real_Time.To_Duration(As_Time_Span);
begin
Put_Line(Ada.Calendar.Formatting.Image(Ada.Calendar."+"(Epoch, Dur)));
end PrintRT;
The only way to get Time_Span is to use the "-" operator between the two.
Since Seconds_Count represents the number of seconds from Epoch, you can plot Time from the time using Time_Of.
Then converting it to duration allows you to find the correct date.
source to share
There is no conversion between Real_Time.Time
and Calendar.Time
, because it Real_Time
increases monotonically (as long as it does not overflow, of course you can count for at least 50 years, ARM D.8 (30) ), and Calendar
may affect daylight saving time, NTP updates, etc. etc.
Did it help Ada.Real_Time.Delays.To_Duration
? (this is a non-standard GNAT package).
I tried
function To_Duration (T : Ada.Real_Time.Time) return Duration is use type Ada.Real_Time.Time; begin return Ada.Real_Time.To_Duration (T - Ada.Real_Time.Time_First); end To_Duration;
but it failed (Mac OS X, desktop, might work better on RTOS) with
raised CONSTRAINT_ERROR : a-reatim.adb:94 overflow check failed
Otherwise, you can use Unchecked_Conversion
for some unsigned type the same size as Real_Time.Time
.
source to share
use the ada.calendar.formatting package like:
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Calendar.Time_Zones; use Ada.Calendar.Time_Zones;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
Put_Line(Image(Date => Clock, Time_Zone => Ada.Calendar.Time_Zones.UTC_Time_Offset) & ": Timeout");
end Main;
but if you want a raw number, you can use unchecked coverage for signed integer 64. (note ada epoch is not unix epoch)
source to share