Skip to content

Instantly share code, notes, and snippets.

@deepakmahakale
Created July 5, 2018 05:01
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 deepakmahakale/33789b5240f56efe4f63b719322460b8 to your computer and use it in GitHub Desktop.
Save deepakmahakale/33789b5240f56efe4f63b719322460b8 to your computer and use it in GitHub Desktop.
  • By default rails will set the current utc time for created_at and rails will display them by converting into the application timeone

  • You can set application time zone with config.time_zone = 'Eastern Time (US & Canada)'

  • You can set application time zone config.active_record.default_timezone = :utc Supports :utc and :local

  • Use Time.current wherever possible

  • This doesn't work as expected

    Message.where(created_at: '19/01/2018'.to_time.beginning_of_day..'19/01/2018'.to_time.end_of_day)
    Message Exists (0.2ms)  SELECT  1 AS one FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?) LIMIT ?  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59.999999"], ["LIMIT", 1]]
    Message Load (0.1ms)  SELECT "messages".* FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?)  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59.999999"]]
  • Whereas this does

    Message.where(created_at: '19/01/2018'.to_time.in_time_zone.beginning_of_day..'19/01/2018'.to_time.in_time_zone.end_of_day)
    Message Exists (0.3ms)  SELECT  1 AS one FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?) LIMIT ?  [["created_at", "2018-01-18 05:00:00"], ["created_at", "2018-01-19 04:59:59.999999"], ["LIMIT", 1]]
    Message Load (0.1ms)  SELECT "messages".* FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?)  [["created_at", "2018-01-18 05:00:00"], ["created_at", "2018-01-19 04:59:59.999999"]]
  • Query differences

    2.4.3 :008 > Message.where(created_at: '2018-01-19 00:00:00'.to_time..'2018-01-19 23:59:59'.to_time)
    Message Exists (0.2ms)  SELECT  1 AS one FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?) LIMIT ?  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59"], ["LIMIT", 1]]
    Message Load (0.1ms)  SELECT "messages".* FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?)  [["created_at", "2018-01-18 18:30:00"], ["created_at", "2018-01-19 18:29:59"]]
    
    2.4.3 :007 > Message.where(created_at: '2018-01-19 00:00:00'..'2018-01-19 23:59:59')
    Message Exists (0.4ms)  SELECT  1 AS one FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?) LIMIT ?  [["created_at", "2018-01-19 00:00:00"], ["created_at", "2018-01-19 23:59:59"], ["LIMIT", 1]]
    Message Load (0.2ms)  SELECT "messages".* FROM "messages" WHERE ("messages"."created_at" BETWEEN ? AND ?)  [["created_at", "2018-01-19 00:00:00"], ["created_at", "2018-01-19 23:59:59"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment