0

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

Pipo
  • 4,653
  • 38
  • 47

2 Answers2

1

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
Louis M
  • 576
  • 2
  • 4
1

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
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35