Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get seconds since unix epoch in Ada?

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?

like image 932
alex Avatar asked Dec 12 '15 12:12

alex


People also ask

How do I convert epoch time to seconds?

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.

How many seconds have passed since the Unix epoch?

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.

Is Unix epoch in 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.

How do I get epoch time in Unix?

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.


3 Answers

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.

like image 62
trashgod Avatar answered Oct 12 '22 00:10

trashgod


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.

like image 32
OCTAGRAM Avatar answered Oct 12 '22 01:10

OCTAGRAM


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.

like image 1
flotto Avatar answered Oct 12 '22 02:10

flotto