Sha256: cb392113368f93e705b22d3851282a605d2025058d2c4e228a90927af381e11c

Contents?: true

Size: 906 Bytes

Versions: 9

Compression:

Stored size: 906 Bytes

Contents

# frozen_string_literal: true

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

      raise 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

    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

    private

    attr_reader :zone
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
soapy_cake-2.2.7 lib/soapy_cake/time_converter.rb
soapy_cake-2.2.6 lib/soapy_cake/time_converter.rb
soapy_cake-2.2.5 lib/soapy_cake/time_converter.rb
soapy_cake-2.2.4 lib/soapy_cake/time_converter.rb
soapy_cake-2.2.3 lib/soapy_cake/time_converter.rb
soapy_cake-2.2.1 lib/soapy_cake/time_converter.rb
soapy_cake-2.1.6 lib/soapy_cake/time_converter.rb
soapy_cake-2.1.5 lib/soapy_cake/time_converter.rb
soapy_cake-2.1.4 lib/soapy_cake/time_converter.rb