Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “time

Changing the Date from UTC to Another Time Zone

I had some calculations that I wanted to do according to my app’s time zone and not UTC.  Date.today was changing earlier than I wanted it to since the default is to use UTC.  However, I found a simple way to fix this:

in application.rb:

config.time_zone = ‘Eastern Time (US & Canada)’

then you can use this to get the date in the time zone you set:

Time.current.to_date

If do not want to set the time_zone in the configuration, you can just do this:

Time.now.in_time_zone( ‘Eastern Time (US & Canada)’).to_date


Time Zones in Rails 3

I was looking for what options I could use to set the time zone config in application.rb.  The best I could find were these posts in Stack Overflow:

http://stackoverflow.com/questions/5073917/why-doesnt-config-time-zone-seem-to-do-anything

http://stackoverflow.com/questions/4362663/timezone-with-rails-3

essentially they are (for the biggest US zones):

Pacific Time (US & Canada), Central Time (US & Canada), Eastern Time (US & Canada)

The posts are good to reference nonetheless, as there seems to be a lot of confusion on what the config does (and time helpers).


Validations for Dates

I searched many times trying to figure out how to validate date of birth in my user model.  Finally, I stumbled on the validates_timeliness gem and it worked great for validating if a user was old enough to join my site.

after installing the gem, i just added the line from the example in my user model:

validates_date :date_of_birth, :before => lambda { 18.years.ago },
                              :before_message => "must be at least 18 years old"

https://github.com/adzap/validates_timeliness

Thank you for making this gem available adzap!