Pergunta

I'm using an API that requires a start_time and an end_time in epoch that will give me data between those times. The question is I want all the data from the start of UTC today to the end of UTC today.

What's the most effective way to do this in ruby?

Foi útil?

Solução

You can use the ActiveSupport Date functions #beginning_of_day and #end_of_day. And use to_i to convert the time to seconds since Epoch.

require 'active_support/core_ext'

Date.today.beginning_of_day.to_i
# => 1395532800 
Date.today.end_of_day.to_i
# => 1395619199 

Outras dicas

Date.today.to_time.to_i

should get you the start, and

(Date.today + 1).to_time.to_i

should get you the end.

If you are not using rails or do not wish to import require 'active_support/core_ext' for some reason you can do it using ruby as follows

Date.today.to_time.to_i # beginning_of_day
=> 1481221800

Date.today.to_time.change(hour: 23, min: 59, sec: 59).to_i # end_of_day
=> 1481308199
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top