Sha256: 7a6218aa6b6e1666fbcc769d9657afd489d098486589098e7ffdc4f3174ec608

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

module Greenwich
  module Utilities
    def self.get_time_zone_field(name, columns)
      target_columns = ["#{name}_time_zone", "time_zone"]

      get_target_column(target_columns, columns)
    end

    def self.get_time_zone(object, time_zone_field_name)
      begin
        time_zone_name = object.send(time_zone_field_name.to_sym)
      rescue
        time_zone_name = ''
      end

      Greenwich::Utilities.coerce_to_time_zone(time_zone_name)
    end

    def self.coerce_to_time_zone(value)
      return nil   if value.nil?
      return value if value.is_a? ActiveSupport::TimeZone

      ActiveSupport::TimeZone.new(value)
    end

    def self.coerce_to_time_zone_name(value)
      coerce_to_time_zone(value).try(:name)
    end

    def self.coerce_to_time_without_zone(value)
      return value if value.is_a?(Time)

      value.gsub! /\s[-+]\d{4}$/, '' if value.respond_to? :gsub!
      value.to_time                  if value.respond_to? :to_time
    rescue ArgumentError
      nil
    end

  private
    def self.get_target_column(target_columns, all_columns)
      target_columns.each {|col| return col if all_columns.include?(col) }

      nil
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
greenwich-2.0.4 lib/greenwich/utilities.rb
greenwich-2.0.3 lib/greenwich/utilities.rb
greenwich-2.0.2 lib/greenwich/utilities.rb
greenwich-2.0.1 lib/greenwich/utilities.rb
greenwich-2.0.0 lib/greenwich/utilities.rb
greenwich-1.0.0 lib/greenwich/utilities.rb