No release in over 3 years
Low commit activity in last 3 years
Introduces a custom ActiveRecord attribute type which will ignore the timezone set by Time.zone. This is useful in the event we are modifying the current thread timezone
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 13.0
>= 0

Runtime

 Project Readme

LocalDateTimeAttributes

Ruby Ruby Gem Gem Version

Leverages the active_record attributes api to define a custom active_record type local_date_time. Normally, active_record will convert a Time,DateTime,Date which has a timezone into the active_record's base timezone - performing conversion of the time in the process. This custom attribute will ignore the timezone component of the Time attribute.

Sometimes it is preferrable to ignore the timezone defined by Time.zone before persisting or loading from persistence. In otherwords, the time_zone component is ignored prior to saving to the database and after loading from database

Installation

Add this line to your application's Gemfile:

gem 'local_date_time_attributes'

And then execute:

$ bundle

Or install it yourself as:

$ gem install local_date_time_attributes

Usage

On an active_record model, just add an attribute with the type :local_date_time

class Interview < ApplicationRecord
  attribute :start_at, :local_date_time
end

We will have a date_time attribute on ProductUpdate.start_at that ignores timezone prior to persisting and after loading

$ Time.zone = 'Eastern Time (US & Canada)'
  => "Eastern Time (US & Canada)"

# active_record is setup to use a different timezone
$ ActiveRecord::Base.default_timezone
  => :utc

$ time = Time.zone.local(2007, 2, 10, 15, 30, 45)
  => Sat, 10 Feb 2007 15:30:45 EST -05:00

# Timezone is ignored when saving to a database
$ Interview.new(start_at: time).save
   (0.4ms)  BEGIN
  SQL (4.7ms)  UPDATE `interviews` SET `start_at` = '2007-02-10 15:30:45', `updated_at` = '2020-03-25 18:06:11' WHERE `product_updates`.`id` = 1
   (1.5ms)  COMMIT

$ interview = Interview.first

$ interview.start_at
  => Sat, 10 Feb 2007 15:30:45 EST -05:00

# timezone is ignored when loading
$ Time.use_zone('Asia/Singapore') { product_update.reload.start_at }
  => Sat, 10 Feb 2007 15:30:45 +08 +08:00

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Cisco-AMP/local_date_time_attributes.