Sha256: a20e8f705f90001f2d2b6ed7ab4d52215f6bbc0a146a1cd012e30aef21facdd4

Contents?: true

Size: 881 Bytes

Versions: 5

Compression:

Stored size: 881 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'.freeze)
    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

5 entries across 5 versions & 1 rubygems

Version Path
soapy_cake-1.20.0 lib/soapy_cake/time_converter.rb
soapy_cake-1.19.0 lib/soapy_cake/time_converter.rb
soapy_cake-1.18.1 lib/soapy_cake/time_converter.rb
soapy_cake-1.18.0 lib/soapy_cake/time_converter.rb
soapy_cake-1.16.2 lib/soapy_cake/time_converter.rb