Project

time_jawn

0.0
No release in over 3 years
Low commit activity in last 3 years
TimeJawn makes class instances time zone aware. It doesn't care one iota about system, application or database time as far as I can tell. It has some expectations and adds some useful methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.9
~> 0.82
~> 0.9

Runtime

 Project Readme

TimeJawn v3.0.0

Code Climate

TimeJawn makes class instances time zone aware. It doesn't care one iota about system, application or database time.

Usage

gem install time_jawn

TimeJawn expects there to be an attribute on the model called time_zone, and that the attribute be present, and valid. TimeJawn works fine with delegations. If you do not have an attribute called time_zone you can add one:

rails g migration AddTimeZoneToModel time_zone:string

Next, add the following line to your model:

has_time_zone

If you already have a time zone attribute on your class, but it is not named time_zone you can specify it's name.

has_time_zone named: :my_whacky_time_zone

If you would like to specify specific date_time attributes that you would like affected you can do so like this.

has_time_zone   time_attributes: [:created_at, :start_time]

Finally, you can also specify a time zone attribute and control the affected attributes.

has_time_zone     named: :my_whacky_time_zone,
                  time_attributes: [:created_at, :start_time]

At this point when you create a new instance of that model you will get two methods for each datetime attribute. One that will convert the time stored in the database into the local time of the instance, and one that will convert any time presented to it into a localized version of that time (which is to say, any time zone info will be stripped off, and the objects time zone will be appended). That can be confusing so here are some examples (that we are going to assume have a time zone attribute already):

class GenericClass < ActiveRecord::Base
  has_time_zone
end

DateTime.now
# Mon, 30 Sep 2013 13:02:19 -0400

instance_of_class = GenericClass.new(time_zone: 'Arizona', created_at: DateTime.now, updated_at: DateTime.now)

instance_of_class.local_created_at
# Mon, 30 Sep 2013 10:02:41 MST -07:00

instance_of_class.local_updated_at
# Mon, 30 Sep 2013 10:02:41 MST -07:00

instance_of_class.current_time
# Mon, 30 Sep 2013 10:04:01 MST -07:00

instance_of_class.local_updated_at = DateTime.now
# Mon, 30 Sep 2013 13:07:00 -0400

instance_of_class.local_updated_at
# Mon, 30 Sep 2013 13:07:00 MST -07:00 - Time zone info completely replaced

At some point you may want to add a field to a form so you can easily set the time zone. ActiveSupport makes that pretty easy:

= f.label :time_zone, "Time Zone: "
= f.time_zone_select(:time_zone, ActiveSupport::TimeZone.us_zones)

Resources

This is not everything I used to figure out how to write this gem, but it's a solid sample.

Time:

Gem:

Other:

License

TimeJawn is Copyright © 2020 Mark Platt and Tamman Technologies, Inc. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.