DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Ada.Real_Time

Monotonic nanosecond Time and Time_Span values.

Read the family guide for representation, selection and resource rules. The complete declaration page retains exact signatures/contracts; the source specification is the implementation baseline.

Availability and ownership

All of RM D.8, on 64-bit nanosecond counts. A time plus or minus a span stops at Time_First and Time_Last, so it has no precondition and the periodic-task idiom proves; the operations on spans have preconditions that keep them in range. Clock reads the monotonic clock.

A client proof uses these contracts without proving the body again (see library assurance). Global => null excludes package-global effects, not changes made through an in out argument.

Import and instantiate

with Ada.Real_Time;
import Ada.Real_Time;

A Vibe module imports this unit's names with from Ada.Real_Time import Name; see library use across syntaxes. In Varc, keep each name's declared spelling and write & on arguments the call changes.

Contract and lifecycle reading

A Pre condition is the caller's obligation; a Post is what a normal return guarantees. What a postcondition does not state, a client cannot assume. Ghost functions are for contracts and ghost code. The family guide describes copying, lifetime, order and cost.

API 001

Time (declaration) — A point on the monotonic clock: a private count of nanoseconds in 64 bits, distinct from an elapsed Time_Span.

Declared interface:

type Time is private;

API 002

Time_Span (declaration) — An interval: a private count of nanoseconds in 64 bits, about 292 years either way.

Declared interface:

type Time_Span is private;

API 003

Time_First (declaration) — The earliest Time, Long_Long_Integer'First nanoseconds; a time minus a span stops here.

Declared interface:

Time_First : constant Time;

API 004

Time_Last (declaration) — The latest Time, Long_Long_Integer'Last nanoseconds; a time plus a span stops here.

Declared interface:

Time_Last : constant Time;

API 005

Time_Span_First (declaration) — The most negative span.

Declared interface:

Time_Span_First : constant Time_Span;

API 006

Time_Span_Last (declaration) — The longest span.

Declared interface:

Time_Span_Last : constant Time_Span;

API 007

Time_Span_Zero (declaration) — The span of no time.

Declared interface:

Time_Span_Zero : constant Time_Span;

API 008

Time_Span_Unit (declaration) — The smallest span, a nanosecond.

Declared interface:

Time_Span_Unit : constant Time_Span;

API 009

Tick (declaration) — The clock's step: a nanosecond, the unit of Time.

Declared interface:

Tick : constant Time_Span;

API 010

Time_Unit (declaration) — A nanosecond, as a number of seconds.

Declared interface:

Time_Unit : constant := 0.000_000_001;

API 011

Nanoseconds_Of (declaration) — (Verada) A time's count of nanoseconds, which the contracts are written in.

Declared interface:

function Nanoseconds_Of (T : Time) return Long_Long_Integer
  with Global => null;

API 012

Span_Nanoseconds (declaration) — (Verada) A span's count of nanoseconds.

Declared interface:

function Span_Nanoseconds (TS : Time_Span) return Long_Long_Integer
  with Global => null;

API 013

Clock (declaration) — The monotonic clock, read through the run time; the prover knows only that it counts from about when the machine started, far below Time_Last.

Declared interface:

function Clock return Time
  with Global => null,
       Post => Nanoseconds_Of (Clock'Result) in 0 .. 4_000_000_000_000_000_000;

API 014

"+" (declaration) — A time a span later: exact, or Time_Last (Time_First for a negative span) when it would be beyond it; no precondition, so a periodic task that adds its period forever proves.

Declared interface:

function "+" (Left : Time; Right : Time_Span) return Time
  with Global => null,
       Post => (if Span_Nanoseconds (Right) >= 0
                then (if Nanoseconds_Of (Left) <= Long_Long_Integer'Last - Span_Nanoseconds (Right)
                      then Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Left) + Span_Nanoseconds (Right)
                      else Nanoseconds_Of ("+"'Result) = Long_Long_Integer'Last)
                else (if Nanoseconds_Of (Left) >= Long_Long_Integer'First - Span_Nanoseconds (Right)
                      then Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Left) + Span_Nanoseconds (Right)
                      else Nanoseconds_Of ("+"'Result) = Long_Long_Integer'First));

API 015

"+" (declaration) — The same sum, span first.

Declared interface:

function "+" (Left : Time_Span; Right : Time) return Time
  with Global => null,
       Post => Nanoseconds_Of ("+"'Result) = Nanoseconds_Of (Right + Left);

API 016

"-" (declaration) — A time a span earlier, stopping at Time_First and Time_Last likewise.

Declared interface:

function "-" (Left : Time; Right : Time_Span) return Time
  with Global => null,
       Post => (if Span_Nanoseconds (Right) <= 0
                then (if Nanoseconds_Of (Left) <= Long_Long_Integer'Last + Span_Nanoseconds (Right)
                      then Nanoseconds_Of ("-"'Result) = Nanoseconds_Of (Left) - Span_Nanoseconds (Right)
                      else Nanoseconds_Of ("-"'Result) = Long_Long_Integer'Last)
                else (if Nanoseconds_Of (Left) >= Long_Long_Integer'First + Span_Nanoseconds (Right)
                      then Nanoseconds_Of ("-"'Result) = Nanoseconds_Of (Left) - Span_Nanoseconds (Right)
                      else Nanoseconds_Of ("-"'Result) = Long_Long_Integer'First));

API 017

"-" (declaration) — The span between two times; the precondition keeps it in range.

Declared interface:

function "-" (Left : Time; Right : Time) return Time_Span
  with Global => null,
       Pre => (if Nanoseconds_Of (Right) >= 0
               then Nanoseconds_Of (Left) >= Long_Long_Integer'First + Nanoseconds_Of (Right)
               else Nanoseconds_Of (Left) <= Long_Long_Integer'Last + Nanoseconds_Of (Right)),
       Post => Span_Nanoseconds ("-"'Result) = Nanoseconds_Of (Left) - Nanoseconds_Of (Right);

API 018

"<" (declaration) — Whether a time is earlier.

Declared interface:

function "<" (Left, Right : Time) return Boolean
  with Global => null,
       Post => "<"'Result = (Nanoseconds_Of (Left) < Nanoseconds_Of (Right));

API 019

"<=" (declaration) — Whether a time is earlier or the same.

Declared interface:

function "<=" (Left, Right : Time) return Boolean
  with Global => null,
       Post => "<="'Result = (Nanoseconds_Of (Left) <= Nanoseconds_Of (Right));

API 020

">" (declaration) — Whether a time is later.

Declared interface:

function ">" (Left, Right : Time) return Boolean
  with Global => null,
       Post => ">"'Result = (Nanoseconds_Of (Left) > Nanoseconds_Of (Right));

API 021

">=" (declaration) — Whether a time is later or the same.

Declared interface:

function ">=" (Left, Right : Time) return Boolean
  with Global => null,
       Post => ">="'Result = (Nanoseconds_Of (Left) >= Nanoseconds_Of (Right));

API 022

"+" (declaration) — The sum of two spans, kept in range by the precondition.

Declared interface:

function "+" (Left, Right : Time_Span) return Time_Span
  with Global => null,
       Pre => (if Span_Nanoseconds (Right) >= 0
               then Span_Nanoseconds (Left) <= Long_Long_Integer'Last - Span_Nanoseconds (Right)
               else Span_Nanoseconds (Left) >= Long_Long_Integer'First - Span_Nanoseconds (Right)),
       Post => Span_Nanoseconds ("+"'Result) = Span_Nanoseconds (Left) + Span_Nanoseconds (Right);

API 023

"-" (declaration) — The difference of two spans, kept in range by the precondition.

Declared interface:

function "-" (Left, Right : Time_Span) return Time_Span
  with Global => null,
       Pre => (if Span_Nanoseconds (Right) >= 0
               then Span_Nanoseconds (Left) >= Long_Long_Integer'First + Span_Nanoseconds (Right)
               else Span_Nanoseconds (Left) <= Long_Long_Integer'Last + Span_Nanoseconds (Right)),
       Post => Span_Nanoseconds ("-"'Result) = Span_Nanoseconds (Left) - Span_Nanoseconds (Right);

API 024

"-" (declaration) — A span negated; not Time_Span_First, whose negation is out of range.

Declared interface:

function "-" (Right : Time_Span) return Time_Span
  with Global => null,
       Pre => Span_Nanoseconds (Right) /= Long_Long_Integer'First,
       Post => Span_Nanoseconds ("-"'Result) = -Span_Nanoseconds (Right);

API 025

"abs" (declaration) — A span's length; not Time_Span_First.

Declared interface:

function "abs" (Right : Time_Span) return Time_Span
  with Global => null,
       Pre => Span_Nanoseconds (Right) /= Long_Long_Integer'First,
       Post => Span_Nanoseconds ("abs"'Result) = abs Span_Nanoseconds (Right);

API 026

"*" (declaration) — A span times an integer, whose product the precondition keeps in range.

Declared interface:

function "*" (Left : Time_Span; Right : Integer) return Time_Span
  with Global => null,
       Pre => Long_Long_Long_Integer (Span_Nanoseconds (Left)) * Long_Long_Long_Integer (Right)
              in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
       Post => Long_Long_Long_Integer (Span_Nanoseconds ("*"'Result))
               = Long_Long_Long_Integer (Span_Nanoseconds (Left)) * Long_Long_Long_Integer (Right);

API 027

"*" (declaration) — The same product, integer first.

Declared interface:

function "*" (Left : Integer; Right : Time_Span) return Time_Span
  with Global => null,
       Pre => Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Span_Nanoseconds (Right))
              in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
       Post => Long_Long_Long_Integer (Span_Nanoseconds ("*"'Result))
               = Long_Long_Long_Integer (Left) * Long_Long_Long_Integer (Span_Nanoseconds (Right));

API 028

"/" (declaration) — How many times a span goes into another, truncated toward zero.

Declared interface:

function "/" (Left, Right : Time_Span) return Integer
  with Global => null,
       Pre => Span_Nanoseconds (Right) /= 0
              and then not (Span_Nanoseconds (Left) = Long_Long_Integer'First and then Span_Nanoseconds (Right) = -1),
       Post => Long_Long_Integer ("/"'Result) = Span_Nanoseconds (Left) / Span_Nanoseconds (Right);

API 029

"/" (declaration) — A span divided by an integer, truncated toward zero.

Declared interface:

function "/" (Left : Time_Span; Right : Integer) return Time_Span
  with Global => null,
       Pre => Right /= 0 and then not (Span_Nanoseconds (Left) = Long_Long_Integer'First and then Right = -1),
       Post => Span_Nanoseconds ("/"'Result) = Span_Nanoseconds (Left) / Long_Long_Integer (Right);

API 030

"<" (declaration) — Whether a span is shorter.

Declared interface:

function "<" (Left, Right : Time_Span) return Boolean
  with Global => null,
       Post => "<"'Result = (Span_Nanoseconds (Left) < Span_Nanoseconds (Right));

API 031

"<=" (declaration) — Whether a span is shorter or the same.

Declared interface:

function "<=" (Left, Right : Time_Span) return Boolean
  with Global => null,
       Post => "<="'Result = (Span_Nanoseconds (Left) <= Span_Nanoseconds (Right));

API 032

">" (declaration) — Whether a span is longer.

Declared interface:

function ">" (Left, Right : Time_Span) return Boolean
  with Global => null,
       Post => ">"'Result = (Span_Nanoseconds (Left) > Span_Nanoseconds (Right));

API 033

">=" (declaration) — Whether a span is longer or the same.

Declared interface:

function ">=" (Left, Right : Time_Span) return Boolean
  with Global => null,
       Post => ">="'Result = (Span_Nanoseconds (Left) >= Span_Nanoseconds (Right));

API 034

To_Duration (declaration) — A span as a Duration (which holds about 136 years either way), to the nanosecond as near as Duration's small allows.

Declared interface:

function To_Duration (TS : Time_Span) return Duration
  with Global => null,
       Pre => Span_Nanoseconds (TS) / 1_000_000_000 in -4_294_967_295 .. 4_294_967_295;

API 035

To_Time_Span (declaration) — A Duration as a span.

Declared interface:

function To_Time_Span (D : Duration) return Time_Span
  with Global => null,
       Post => abs (Span_Nanoseconds (To_Time_Span'Result) - Long_Long_Integer (D) * 1_000_000_000) <= 500_000_000;

API 036

Nanoseconds (declaration) — A span of so many nanoseconds.

Declared interface:

function Nanoseconds (NS : Integer) return Time_Span
  with Global => null,
       Post => Span_Nanoseconds (Nanoseconds'Result) = Long_Long_Integer (NS);

API 037

Microseconds (declaration) — A span of so many microseconds, for counts whose span is in range.

Declared interface:

function Microseconds (US : Integer) return Time_Span
  with Global => null,
       Pre => US in -9_223_372_036_854_775 .. 9_223_372_036_854_775,
       Post => Span_Nanoseconds (Microseconds'Result) = Long_Long_Integer (US) * 1_000;

API 038

Milliseconds (declaration) — A span of so many milliseconds, for counts whose span is in range.

Declared interface:

function Milliseconds (MS : Integer) return Time_Span
  with Global => null,
       Pre => MS in -9_223_372_036_854 .. 9_223_372_036_854,
       Post => Span_Nanoseconds (Milliseconds'Result) = Long_Long_Integer (MS) * 1_000_000;

API 039

Seconds (declaration) — A span of so many seconds, for counts whose span is in range.

Declared interface:

function Seconds (S : Integer) return Time_Span
  with Global => null,
       Pre => S in -9_223_372_036 .. 9_223_372_036,
       Post => Span_Nanoseconds (Seconds'Result) = Long_Long_Integer (S) * 1_000_000_000;

API 040

Minutes (declaration) — A span of so many minutes, for counts whose span is in range.

Declared interface:

function Minutes (M : Integer) return Time_Span
  with Global => null,
       Pre => M in -153_722_867 .. 153_722_867,
       Post => Span_Nanoseconds (Minutes'Result) = Long_Long_Integer (M) * 60_000_000_000;

API 041

Seconds_Count (declaration) — Whole seconds (RM D.8(29)), enough for those of any Time.

Declared interface:

type Seconds_Count is range -9_223_372_037 .. 9_223_372_037;

API 042

Split (declaration) — A time as whole seconds and the nanoseconds after them, 0 .. 999_999_999.

Declared interface:

procedure Split (T : Time; SC : out Seconds_Count; TS : out Time_Span)
  with Global => null,
       Post => Span_Nanoseconds (TS) in 0 .. 999_999_999
               and then Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS))
                        = Long_Long_Long_Integer (Nanoseconds_Of (T));

API 043

Time_Of (declaration) — The time Split gives these of.

Declared interface:

function Time_Of (SC : Seconds_Count; TS : Time_Span) return Time
  with Global => null,
       Pre => Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS))
              in Long_Long_Long_Integer (Long_Long_Integer'First) .. Long_Long_Long_Integer (Long_Long_Integer'Last),
       Post => Long_Long_Long_Integer (Nanoseconds_Of (Time_Of'Result))
               = Long_Long_Long_Integer (SC) * 1_000_000_000 + Long_Long_Long_Integer (Span_Nanoseconds (TS));