ZetCode

Ada date and time

last modified November 23, 2026

Ada keeps wall-clock time and elapsed time in two different packages. Ada.Calendar represents a date and time of day and answers questions about years, months, and weekdays, while Ada.Real_Time provides a monotonic clock for measuring intervals. Both use private types with their own arithmetic, so a program cannot accidentally add a date to a date or subtract a duration from a year. This article reads the parts of a time, formats and parses it, crosses a year boundary with date arithmetic, measures a computation, and finds the weekday of several dates.

Splitting a time into parts

A Time value is a point on the calendar, not a record with visible fields. Time_Of builds one from a year, a month, a day, and the seconds since midnight, and the functions Year, Month, Day, and Seconds take it apart again. The type of Seconds is Day_Duration, a fixed-point type that allows a fractional second.

date_parts.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting;

procedure Date_Parts is
   Date : constant Time :=
     Ada.Calendar.Formatting.Time_Of (2026, 11, 23, 9, 30, 0);
begin
   Put_Line ("year  =" & Year_Number'Image (Year (Date)));
   Put_Line ("month =" & Month_Number'Image (Month (Date)));
   Put_Line ("day   =" & Day_Number'Image (Day (Date)));
   Put_Line ("seconds =" & Day_Duration'Image (Seconds (Date)));
end Date_Parts;

The date is nine thirty in the morning, so Seconds is 37800.0, or 9 × 3600 + 30 × 60. The overloaded Time_Of from Ada.Calendar.Formatting is used because the plain Ada.Calendar version only accepts seconds since midnight, not an hour and a minute. The attributes Year_Number, Month_Number, and Day_Number name the integer subtypes of the results and feed their 'Image.

$ gnatmake -gnat2022 -gnatwa date_parts.adb
$ ./date_parts
year  = 2026
month = 11
day   = 23
seconds = 37800.000000000

Formatting and parsing

Ada.Calendar.Formatting.Image produces the ISO 8601 form YYYY-MM-DD HH:MM:SS, and Value reads that form back into a Time. The same package provides Hour, Minute, Second, Sub_Second, and Day_Of_Week. Include_Time_Fraction adds hundredths of a second to the image.

calendar_demo.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;

procedure Calendar_Demo is
   Date  : constant Time := Time_Of (2026, 12, 24, 18, 30, 15);
   Again : constant Time := Value ("2026-12-24 18:30:15");
begin
   Put_Line ("image    = " & Image (Date));
   Put_Line ("fraction = " & Image (Date, Include_Time_Fraction => True));
   Put_Line ("weekday  = " & Day_Name'Image (Day_Of_Week (Date)));
   Put_Line ("hour     =" & Integer'Image (Hour (Date)));
   Put_Line ("minute   =" & Integer'Image (Minute (Date)));
   Put_Line ("second   =" & Integer'Image (Second (Date)));
   Put_Line ("round trip = " & Image (Again));
end Calendar_Demo;

Date and Again end up equal, which the last line shows by printing the parsed value. The weekday is an enumeration value of type Day_Name, and its 'Image is uppercase in this GNAT version. A string that does not match the expected format makes Value raise Constraint_Error, so a program that reads dates from a file should wrap the call in a handler or validate the text first.

$ gnatmake -gnat2022 -gnatwa calendar_demo.adb
$ ./calendar_demo
image    = 2026-12-24 18:30:15
fraction = 2026-12-24 18:30:15.00
weekday  = THURSDAY
hour     = 18
minute   = 30
second   = 15
round trip = 2026-12-24 18:30:15

Date arithmetic

A Time can be shifted by a Duration measured in seconds, and subtracting two times gives the duration between them. For calendar arithmetic the package Ada.Calendar.Arithmetic offers Difference, which splits the gap into whole days and the remaining seconds and also reports any leap seconds. The next program adds ninety days to a November morning and asks for the result in both forms.

arithmetic.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Arithmetic; use Ada.Calendar.Arithmetic;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;

procedure Arithmetic is
   Start : constant Time := Time_Of (2026, 11, 23, 8, 0, 0);
   Later : constant Time := Start + 90.0 * 86400.0;
   Days  : Day_Count;
   Secs  : Duration;
   Leap  : Leap_Seconds_Count;
begin
   Put_Line ("start = " & Image (Start));
   Put_Line ("later = " & Image (Later));

   Difference (Later, Start, Days, Secs, Leap);
   Put_Line ("days  =" & Day_Count'Image (Days));
   Put_Line ("secs  =" & Duration'Image (Secs));
   Put_Line ("leap  =" & Leap_Seconds_Count'Image (Leap));

   Put_Line ("plus 1 hour = " &
             Image (Start + 3600.0, Include_Time_Fraction => True));
end Arithmetic;

Ninety days after November 23 is February 21 of the next year, and the difference reports exactly ninety days with no remainder. Multiplying 90.0 * 86400.0 converts days to seconds, because the "+" operator on Time expects a duration in seconds. The final line shows a one-hour shift with the time fraction enabled, which prints the .00 that the plain image omits.

$ gnatmake -gnat2022 -gnatwa arithmetic.adb
$ ./arithmetic
start = 2026-11-23 08:00:00
later = 2027-02-21 08:00:00
days  = 90
secs  = 0.000000000
leap  = 0
plus 1 hour = 2026-11-23 09:00:00.00

Measuring elapsed time

For intervals, Ada.Real_Time is a better choice than Ada.Calendar, because its clock cannot jump when the system time is adjusted. Clock returns a Time, subtracting two of them gives a Time_Span, and To_Duration converts the span to seconds. The program measures a loop that adds five million remainders.

elapsed.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;

procedure Elapsed is
   Start : constant Time := Clock;
   Sum   : Natural := 0;
begin
   for I in 1 .. 5_000_000 loop
      Sum := Sum + I mod 7;
   end loop;

   declare
      Span : constant Time_Span := Clock - Start;
      Ms   : constant Integer := Integer (To_Duration (Span) * 1000.0);
   begin
      Put_Line ("sum =" & Natural'Image (Sum));
      Put_Line ("elapsed =" & Integer'Image (Ms) & " ms");
   end;
end Elapsed;

The sum is 15,000,000 and the elapsed time was twelve milliseconds on the machine that produced this output; the exact number depends on the machine and on what else is running. The Time_Span type supports addition, subtraction, and comparison, and Milliseconds and Seconds build spans from integers. For a delay rather than a measurement, delay takes a Duration and delay until takes an absolute Real_Time.Time.

$ gnatmake -gnat2022 -gnatwa elapsed.adb
$ ./elapsed
sum = 15000000
elapsed = 12 ms

Finding the weekday

Day_Of_Week returns a value of the enumeration type Day_Name, whose literals run from Monday to Sunday. The next program builds four dates and prints the image of each together with its weekday. The last line lowercases one of the literals with Ada.Characters.Handling.To_Lower, because 'Image of an enumeration is uppercase here.

weekdays.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
with Ada.Characters.Handling;

procedure Weekdays is
   Dates : constant array (1 .. 4) of Time :=
     [Time_Of (2026, 1, 1, 0, 0, 0),
      Time_Of (2026, 2, 14, 0, 0, 0),
      Time_Of (2026, 7, 4, 0, 0, 0),
      Time_Of (2026, 12, 25, 0, 0, 0)];
begin
   for D of Dates loop
      Put_Line (Image (D) & " is " &
                Day_Name'Image (Day_Of_Week (D)));
   end loop;
   Put_Line (Ada.Characters.Handling.To_Lower (Day_Name'Image (Sunday)));
end Weekdays;

January 1, 2026 is a Thursday, February 14 and July 4 are Saturdays, and December 25 is a Friday. The loop walks the array of dates and lets Day_Of_Week do the calendar work, including leap years, so no arithmetic on month lengths is needed. Sunday is an enumeration literal in the same type, and its lowercased image is the last line of the output.

$ gnatmake -gnat2022 -gnatwa weekdays.adb
$ ./weekdays
2026-01-01 00:00:00 is THURSDAY
2026-02-14 00:00:00 is SATURDAY
2026-07-04 00:00:00 is SATURDAY
2026-12-25 00:00:00 is FRIDAY
sunday

Next steps

The next article introduces random numbers: seeding a generator, producing floats in a range, choosing from a discrete type, and shuffling an array. The fixed-point article explains the exact decimal type used for the fractional seconds, and text output covers the integer formatting that the programs use for their numeric fields.

Source

Ada Reference Manual: Delay Statements, Duration, and Time

Ada Reference Manual: Formatting and Time Zones

GNAT User's Guide

In this article we have split a time into year, month, day, and seconds, formatted and parsed dates, added ninety days across a year boundary, measured a loop with the monotonic real-time clock, and printed the weekday of four dates. The types article explains the private types and the arithmetic that the calendar packages define for them.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.