Adam
Adam

Reputation: 518

Ruby - Is there a way to get the timestamp for a specific future time but have that be dependent on timezone

I have an email system. I want to send emails to people at 8AM local time. If I have their timezone in this format: "America/New_York" how can I get a Time object with the next instance of 8AM for that timezone?

Upvotes: 0

Views: 380

Answers (1)

spickermann
spickermann

Reputation: 106952

I assume that you have a User model and each user has its timezone stored in an attribute timezone and timezone #=> 'America/New_York'.

Then you can add a method like the following to your User model:

def next_time_it_is_8am_in_this_users_timezone_in_utc
  time = ActiveSupport::TimeZone[timezone].now.change(hour: 8)
  time = time + 1.day if time.past?
  
  time.utc
end

Upvotes: 1

Related Questions