Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in Ada

How to convert Unix timestamp to Datetime in Ada

🎈 🎈 🎈
+1

    with Ada.Calendar;
    with Ada.Text_IO;
    
    procedure Unix_Timestamp_To_Date is
        -- The Unix epoch starts on January 1, 1970
        Epoch : constant Ada.Calendar.Time := Ada.Calendar.Time_Of(1970, 1, 1);
    
        -- Example Unix timestamp (1692902400 corresponds to August 24, 2023)
        Unix_Timestamp : constant Integer := 1692902400;
    
        -- Convert Unix timestamp to Ada.Calendar.Time
        Specific_Time : Ada.Calendar.Time := Epoch + Ada.Calendar.Duration(Unix_Timestamp);
    
    begin
        -- Output the converted date and time
        Ada.Text_IO.Put_Line(Ada.Calendar.Image(Specific_Time));
    end Unix_Timestamp_To_Date;        
       

Output:
1692835200
Example only. There may be multiple ways to perform this operation.

Code Explanation

This Ada snippet converts a Unix timestamp into a human-readable date using the Ada.Calendar and Ada.Text_IO packages.
The procedure Unix_Timestamp declares a constant Timestamp of type Ada.Calendar.Time, which represents a point in time. It initializes this constant by adding the Unix timestamp 1609459200.0 (representing the number of seconds since January 1, 1970) to the epoch, effectively calculating the date of January 1, 2021. Inside the procedure body, Ada.Text_IO.Put_Line is used to print the result, where Ada.Calendar.Image(Timestamp) converts the timestamp into a string format.
When executed, the procedure outputs the corresponding date and time, such as "2023-08-24 18:40:00".


Other useful Ada date functions

Function Description Example
Clock Returns the current date and time as a value of type Ada.Calendar.Time. Clock returns 2023-08-28T12:00:00Z
Year (Time) Returns the year of a given Time value from Ada.Calendar.Time. Year(Clock) returns 2023
Month (Time) Returns the month (1-12) of a given Time value. Month(Clock) returns 8 for August.
Day (Time) Returns the day of the month (1-31) of a given Time value. Day(Clock) returns 28 for the 28th of August.
Second (Time) Returns the second part of a Time value (0-59). Second(Clock) returns 12 for 12 seconds past the minute.
Split (Time, Year, Month, Day, Seconds) Splits a Time value into its component parts: year, month, day, and seconds. Split(Clock, Y, M, D, S) sets Y=2023, M=8, D=28, S=12
Time_Of (Year, Month, Day, Seconds) Returns a Time value constructed from the provided year, month, day, and seconds. Time_Of(2023, 8, 28, 12) returns 2023-08-28T12:00:00Z
Time_Zones Provides timezone information for adjusting local and UTC times. Ada.Calendar.Time_Zones can be used to handle time zone conversions.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder