Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in Ada

🎈 🎈 🎈
+1

    with Ada.Calendar;
    with Ada.Text_IO;
    
    procedure Simple_Date_To_Unix_Timestamp is
        -- The Unix epoch starts on January 1, 1970
        Epoch : constant Ada.Calendar.Time := Ada.Calendar.Time_Of(1970, 1, 1);
    
        -- Example date: August 24, 2023, 00:00:00
        Specific_Time : Ada.Calendar.Time := Ada.Calendar.Time_Of(2023, 8, 24, 0, 0, 0.0);
    
        -- Calculate the difference in seconds
        Unix_Timestamp : constant Integer := Integer(Ada.Calendar.Seconds(Specific_Time - Epoch));
    
    begin
        -- Output the Unix timestamp
        Ada.Text_IO.Put_Line("Unix Timestamp: " & Integer'Image(Unix_Timestamp));
    end Simple_Date_To_Unix_Timestamp;
       

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

Code Explanation

This Ada code calculates the Unix timestamp for a specific date (eg. August 24, 2023, 00:00:00).
It first imports the Ada.Calendar and Ada.Text_IO packages. The code defines the Unix epoch (January 1, 1970) using Ada.Calendar.Time_Of. Then, it assigns the target date (2023-08-24) to the variable Specific_Time and calculates the difference in seconds between this date and the epoch using the Seconds() function. This difference is converted into an integer to represent the Unix timestamp.
Finally, the program prints the resulting Unix timestamp to the console using Ada.Text_IO.Put_Line.


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