Skip to content

Instantly share code, notes, and snippets.

@tamouse
Created November 12, 2014 11:50
Show Gist options
  • Save tamouse/68bffc38e3fc362ea71a to your computer and use it in GitHub Desktop.
Save tamouse/68bffc38e3fc362ea71a to your computer and use it in GitHub Desktop.
showing time in different time zones
module Blahapp
class Application < Rails::Application
# 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 = 'Central Time (US & Canada)'
end
end
$ bin/rails c
Loading development environment (Rails 4.1.6)
[1] pry(main)> show-models
TimeTest
id: integer
whats_at: datetime
created_at: datetime
updated_at: datetime
[2] pry(main)> TimeTest.find(1).attributes
TimeTest Load (0.2ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> {"id"=>1,
"whats_at"=>Wed, 12 Nov 2014 05:41:11 CST -06:00,
"created_at"=>Wed, 12 Nov 2014 05:41:11 CST -06:00,
"updated_at"=>Wed, 12 Nov 2014 05:41:11 CST -06:00}
[3] pry(main)> TimeTest.find(1).whats_at
TimeTest Load (0.3ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> Wed, 12 Nov 2014 05:41:11 CST -06:00
[4] pry(main)> TimeTest.find(1).whats_at.utc
TimeTest Load (0.1ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> 2014-11-12 11:41:11 UTC
[6] pry(main)> TimeTest.find(1).whats_at.in_time_zone
TimeTest Load (0.2ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> Wed, 12 Nov 2014 05:41:11 CST -06:00
[7] pry(main)> TimeTest.find(1).whats_at.in_time_zone('EST')
TimeTest Load (0.2ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> Wed, 12 Nov 2014 06:41:11 EST -05:00
[8] pry(main)> TimeTest.find(1).whats_at.in_time_zone('Pretoria')
TimeTest Load (0.2ms) SELECT "time_tests".* FROM "time_tests" WHERE "time_tests"."id" = ? LIMIT 1 [["id", 1]]
=> Wed, 12 Nov 2014 13:41:11 SAST +02:00
[9] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment