0

Is there a way to convert postgres UTC time to the user's timezone or at the very least Eastern Standard Time (I'd be grateful with either answer)?

I thought that there was a way to change postgres UTC time, but I don't think there is without causing a lot of issues. From what I read it would be better to use code on the frontend that would convert it to the correct time zone?

This barely makes sense to me.

What's the point?

So that when a user checks off he completed a good habit, the habit disappears, and is suppose to reshow tomorrow at 12am, but the habits end up reshowing later in the day because of UTC.

habit.rb

scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Date.today)} # aka those habits not completed today will be shown

def completed=(boolean)
  self.completed_at = boolean ? Time.current : nil
end

def completed
  completed_at && completed_at >= Time.current.beginning_of_day
end
8
  • so if you add this config.time_zone = 'Eastern Time (US & Canada)' in your application.rb and in your console if you do Habit.last.completed_at does it returns UTC time or Eastern time.? Commented Jul 26, 2015 at 19:13
  • config.time_zone let you access your time in specfic time zone using Time.zone.now or Time.zone.beginning_of_day. if you change your scope abit and add config.time_zone = 'Eastern Time (US & Canada)' in your application.rb you will acheive what you want Commented Jul 26, 2015 at 19:15
  • I already have that line @Athar in my application.rb: stackoverflow.com/questions/31639288/… Commented Jul 26, 2015 at 19:18
  • okay did you try this Habit.last.completed_at what does it return. also change your scope like this scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.zone.now.beginning_of_day)} Commented Jul 26, 2015 at 19:19
  • That returns: Sun, 26 Jul 2015 12:20:13 EDT -04:00 It works in development @Athar. I'll give that scope a try. Commented Jul 26, 2015 at 19:21

2 Answers 2

1

please change your scope to this, it will search time zone specifically.

scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.zone.now.beginning_of_day)}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey this didn't work for me, but the answer in your comments did: scope :incomplete, -> {where("completed_at is null OR completed_at < ?", Time.zone.now.beginning_of_day)} Thanks again :]
0

Place this code snippet into your application.rb

  config.time_zone = 'Eastern Time (US & Canada)'

and run this so it will be set on heroku heroku config:add TZ=America/Los_Angeles.

Generally, you can also use #in_time_zone. Always use the time like Time.zone.now.

Time.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
DateTime.utc(2000).in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Date.new(2000).in_time_zone('Alaska')  # => Sat, 01 Jan 2000 00:00:00 AKST -09:00

date and time zones

api docs

1 Comment

Thanks Adam! I already have that in my application.rb & ran that command: stackoverflow.com/questions/31639288/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.