module LocalTime def self.included(base) base.extend ClassMethods end module ClassMethods def local_time(*args) options = args.last.is_a?(Hash) ? args.shift : {} args.each do |field| class_eval do define_method "local_#{field}".to_sym do utc_time = read_attribute(field) if utc_time.is_a?(Time) && tz = TzinfoTimezone[send(options[:timezone] || :timezone) || ''] tz.utc_to_local(utc_time) else utc_time end end define_method "local_#{field}=".to_sym do |val| if val.is_a?(Time) && tz = TzinfoTimezone[send(options[:timezone] || :timezone) || ''] utc_time = tz.local_to_utc(val) else utc_time = val end write_attribute field, utc_time end end end end end end ActiveRecord::Base.class_eval do include LocalTime end