how would you calculate the number of seconds since epoch in eiffel for a DATE_TIME object

53 Views Asked by At

how would you calculate the number of seconds since epoch in eiffel for a DATE_TIME object?

2

There are 2 best solutions below

0
On BEST ANSWER

Assuming variable t is of type DATE_TIME (in UTC), the code to get the corresponding number of seconds since the epoch is

t.definite_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count

Note. If the calculation is performed rather often, it might be more efficient to create an object epoch of type DATE_TIME with create epoch.make_from_epoch (0) in advance and to use it in the calculation instead:

t.definite_duration (epoch).seconds_count
0
On

This seems to work:

make
        -- Execute the example
    local
        l_date:DATE_TIME
        l_seconds:INTEGER_64
    do
        create l_date.make_now
        l_seconds := l_date.relative_duration (create {DATE_TIME}.make_from_epoch (0)).seconds_count
        print("The number of seconds: " + l_seconds.out + "%N")
    end