I feel very stupid as I don't seem to get a plain Natural number representing the seconds since the unix epoch (01/01/1970 00:00:00) in Ada. I've read the Ada.Calendar
and it's subpackages up and down but don't seem to find a sensible way of achieving that, even though Ada.Calendar.Clock
itself is supposed to be exactly what I want...
I am at my wits end. Any nudge in the right direction?
Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.
1665273605 seconds elapsed since jan 1 1970. Unix time (also known as POSIX time or UNIX Epoch time) is a system for describing a point in time. It is the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, minus leap seconds.
The epoch time, commonly referred to as UNIX time or POSIX time, describes instants in time as the number of seconds that have elapsed since 00:00:00 on 1 January 1970. In other words, the system represents different dates as elapsed seconds.
Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.
Using Ada.Calendar.Formatting
, construct a Time
representing the epoch.
Epoch : constant Time := Formatting.Time_Of(1970, 1, 1, 0.0);
Examine the difference between Ada.Calendar.Clock
and Epoch
.
Put(Natural(Clock - Epoch)'Img);
Check the result against this epoch display or the Unix command date +%s
.
See Rationale for Ada 2005: §7.3 Times and dates and Rationale for Ada 2012: §6.6 General miscellanea for additional details.
According to POSIX standard UNIX time does not account leap seconds, while Ada.Calendar."-" handles them:
For the returned values, if Days = 0, then Seconds + Duration(Leap_Seconds) = Calendar."–" (Left, Right).
One option is to split Ada.Calendar.Time into pieces using Ada.Calendar.Formatting.Split and gather back using POSIX algorithm.
The best option option seems to be to use Ada.Calendar.Arithmetic.Difference. It returns Days, Seconds and Leap_Seconds. You can then combine Days * 86_400 + Seconds to get UNIX time, and Leap_Seconds will be explicitly thrown away as required by POSIX.
I have recently been solving this problem and posted a library into public domain.
In the GNAT implementation of Ada, there is a private package Ada.Calendar.Conversions
which contains Ada <-> Unix conversions used by the children of Calendar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With