Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhjguxin/9111528 to your computer and use it in GitHub Desktop.
Save jhjguxin/9111528 to your computer and use it in GitHub Desktop.
rails time zone outside of Rails eg db time

some tips for rails time zone

resources:

# Set `Time.zone` default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is `UTC`.
config.time_zone = 'Beijing'
# config.active_record.default_timezone determines whether to use Time.local (if set to :local) or Time.utc (if set to :utc) when pulling dates and times from the database. The default is :utc for Rails, although Active Record defaults to :local when used outside of Rails.

So if you config an incorrent config.time_zone for rails application, must beware that your have alreally store an incorrent time date to db (utc to store db, and auto convert your app time zone when call 'object_instance.time_field').

if you have work in corrent yeat, when compare time from db 'object_instance.time_field' and DateTime or Time (from PC time) must covert pc time to app time zone, eg:

# whether current gx_grade_score_daily need_reset
# return [:boolean]
def need_reset?
  # updated_at.midnight < DateTime.now.midnight
  # will inconsistent with really machine time
  # updated_at.midnight < Time.now.in_time_zone.midnight
  updated_at.localtime.midnight < Time.now.midnight
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment