module TimeJawn::InstanceMethods

Defines methods that will be added to instances of classes that have previously called has_time_zone.

Public Class Methods

included(base) click to toggle source

This method generates a series of methods on instances by calling the enerate_to_local and generate_to_local_with_assignment that are private on the parent class. The methods that are created are called local_#{attribute} and local_#{attribute}= the attribute portion their names are completed by enumerating the datetime_attributes of the class. Twice as many methods as there are DateTime attributes will be created.

:created_at, and :updated_at

 local_created_at
 local_updated_at
 local_created_at=
 local_updated_at=

The local_#{attribute} methods will take the value stored in the attribute indicated by the methods name and apply a time zone conversion to it. This

is useful for displaying the local time (according to the object).

local_#{attribute}= methods are assignment shortcuts. They behave a little differently than you would expect. They do not take a local time and convert it into utc (or whatever, ActiveSupport will handle that for us), what these assigment methods do is take any sort of string that looks like a time, or any sort of time or datetime object lop off whatever timezone is being fed in and glue the instances local timezone on the end before applying it to the appropriate attribute. This is convenient for some one in one time zone setting a value for an instance that represents a different time zone. For example:

I am in Philadelphia (EST), my application is set to UTC, and I want
to set the time on an Alarm instance that  goes off in San Francisco
(PST). I want that time to be 6PM. In Philadlephia I choose 6PM
(local), the applications assumes I meant 6PM UTC (2PM EST and 11AM
PST). That is not what I intended, I intended on 6PM PST, and now my
Alarm is all wrong. The assignment methods turn 6PM (set in EST, and
processed in UTC) into 6PM PST (or 9PM EST, 1AM UTC) the expected
time. The Alarm goes off as expected!*

*Times in this example may be wrong since I put them in myself.

You can see examples of how these methods work in the specs folder.

# File lib/time_jawn/time_jawn.rb, line 80
def self.included(base)
  date_time_attributes = base.send(:class_date_attributes_or_arguments)
  date_time_attributes.each do |attribute|
    base.send(:generate_to_local, attribute)
    base.send(:generate_to_local_with_assignment, attribute)
  end
end

Public Instance Methods

current_time() click to toggle source

Returns the current time according to the instance.

# File lib/time_jawn/time_jawn.rb, line 89
def current_time
  to_local(DateTime.current)
end

Private Instance Methods

add_zone(time_string) click to toggle source

Given a string that looks like a time. It will convert that string into a time object that matches the time but with the instances time zone appended.

# File lib/time_jawn/time_jawn.rb, line 104
def add_zone(time_string)
  Time.zone = send(self.class.time_zone_attribute_name)
  Time.zone.parse(Time.parse(time_string).strftime(DATE_FORMAT))
end
change_zone(time) click to toggle source

Returns a string representation of a time object suitable for consumption by add_zone.

# File lib/time_jawn/time_jawn.rb, line 111
def change_zone(time)
  add_zone(time.strftime(DATE_FORMAT))
end
to_local(time) click to toggle source

converts a time object into it's local counter part (they will have the same value but differnt presentation.)

# File lib/time_jawn/time_jawn.rb, line 97
def to_local(time)
  time.in_time_zone(send(self.class.time_zone_attribute_name))
end