Sha256: 4494f37b23962b2fda5654d087da24ef2f9ec9f29ff5bf3145b938032e95337a

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

module SoapyCake
  class TimeConverter
    def initialize(time_zone, time_offset = nil)
      if time_offset
        self.class.print_deprecation_warning

        # Etc/GMT time zones have their sign reversed
        time_zone = format('Etc/GMT%+d', -time_offset.to_i)
      end

      fail Error, 'Cake time zone missing' if time_zone.blank?
      @zone = ActiveSupport::TimeZone[time_zone]
    end

    def to_cake(date)
      date = date.to_datetime if date.is_a?(Date)
      date.in_time_zone(zone).strftime('%Y-%m-%dT%H:%M:%S')
    end

    def from_cake(value)
      zone.parse(value).utc
    end

    private

    attr_reader :zone

    def self.print_deprecation_warning
      return if @deprecation_warning_printed
      @deprecation_warning_printed = true

      STDERR.puts 'SoapyCake - DEPRECATED: Please use time_zone instead of time_offset.'
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
soapy_cake-1.16.1 lib/soapy_cake/time_converter.rb
soapy_cake-1.16.0 lib/soapy_cake/time_converter.rb