0.0
No commit activity in last 3 years
No release in over 3 years
DateTimezone is an ActiveRecord concern that makes date-type attributes to convert String inputs to Date taking account of the application's timezone.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 4.2
 Project Readme

DateTimezone

Build Status Gem Version

DateTimezone is an ActiveRecord concern that makes date-type attributes to convert String inputs to Date taking account of the application's timezone.

Motivation

In JavaScript, sometimes we want to handle date as Date and send it to Rails application directly. JavaScript's JSON.parse() converts Date to an string in UTC ISO 8601 format.

JSON.stringify({ date: new Date(2015, 2, 14) })
// "{"date":"2015-03-13T15:00:00.000Z"}"

So, that's what Rails application's controller receives and it is usually passed to ActiveRecord model directly. However, ActiveRecord's date-type attribute doesn't take timezone into account when it converts the given String to Date.

class Application < Rails::Application
  config.time_zone = 'Tokyo'
end

class Person < ActiveRecord::Base
  # birth_date :date
end

expect(Person.new(birth_date: '2015-02-13T15:00:00.000Z').birth_date)
  .to eq(Date.new(2015, 2, 13))

DateTimezone concern overrides date-type attribute mutators in your ActiveRecord model to take timezone into account.

class Person < ActiveRecord::Base
  include DateTimezone
end

expect(Person.new(birth_date: '2015-02-13T15:00:00.000Z').birth_date)
  .to eq(Date.new(2015, 2, 14))