Sha256: 8ec826f4f7b30f108fb0f648e14c7ae0663ded3c6879a1ab90101e267474a557

Contents?: true

Size: 738 Bytes

Versions: 124

Compression:

Stored size: 738 Bytes

Contents

# frozen_string_literal: true

class ReeDatetime::OffsetToString
  include Ree::FnDSL

  fn :offset_to_string

  MAX_OFFSET = 86_400
  OFFSET_CACHE = {}

  doc("Converts an offset in seconds to a formatted string (ex. 3600 => '+01:00")
  contract(Integer => String).throws(ArgumentError)
  def call(offset)
    return OFFSET_CACHE[offset] if OFFSET_CACHE.has_key?(offset)

    if !(-MAX_OFFSET..MAX_OFFSET).include?(offset)
      raise ArgumentError, "offset should be in (-#{MAX_OFFSET}..#{MAX_OFFSET})"
    end

    val = offset.abs
    hours = val / 3600
    minutes = (val % 3600) / 60
    str = "#{offset < 0 ? '-' : '+'}#{hours < 10 ? '0' : ''}#{hours}:#{minutes < 10 ? '0' : ''}#{minutes}"

    OFFSET_CACHE[offset] = str
  end
end

Version data entries

124 entries across 124 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_datetime/package/ree_datetime/functions/offset_to_string.rb