Skip to content

Instantly share code, notes, and snippets.

@cjolly
Created October 5, 2012 22:19
Show Gist options
  • Save cjolly/3842778 to your computer and use it in GitHub Desktop.
Save cjolly/3842778 to your computer and use it in GitHub Desktop.
DateTime vs Time and ActiveRecord
>> ps.cancel_at
=> Fri, 05 Oct 2012 00:00:00 UTC +00:00
>> ps.cancel_at.class
=> ActiveSupport::TimeWithZone
>> ps.cancel_at = DateTime.new 2012, 11, 05
=> Mon, 05 Nov 2012 00:00:00 +0000
>> ps.save
   (0.2ms)  BEGIN
  Entity Load (0.5ms)  SELECT "entities".* FROM "entities" WHERE "entities"."id" = 2485 LIMIT 1
  Product Load (0.7ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 32445 LIMIT 1
   (0.8ms)  UPDATE "product_subscriptions" SET "cancel_at" = '2012-11-05 00:00:00.000000', "updated_at" = '2012-10-05 22:16:42.029113' WHERE "product_subscriptions"."id" = 6762
   (1245.9ms)  COMMIT
=> true
>> ps.cancel_at = Time.local 2012, 11, 05
=> 2012-11-05 00:00:00 -0700
>> ps.save
   (0.2ms)  BEGIN
   (0.5ms)  UPDATE "product_subscriptions" SET "cancel_at" = '2012-11-05 07:00:00.000000', "updated_at" = '2012-10-05 22:17:32.097390' WHERE "product_subscriptions"."id" = 6762
   (0.4ms)  COMMIT
=> true
>> 
@cjolly
Copy link
Author

cjolly commented Oct 11, 2012

Something we could consider is our nice_date helper, perhaps adding a precise_date which normalizes to a set TZ regardless of logged_in user's TZ.

>> Time.utc(2012, 8, 13, 5, 0).in_time_zone(-6.hours).to_date
=> Sun, 12 Aug 2012
>> Time.utc(2012, 8, 13, 5, 0).to_date
=> Mon, 13 Aug 2012
>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment