Sha256: cad4727faa84caae759c64a7ab9c897031b5a4a8c99b64ab98a4a58102b6a34e

Contents?: true

Size: 1001 Bytes

Versions: 7

Compression:

Stored size: 1001 Bytes

Contents

# frozen_string_literal: true

require 'active_support/core_ext/object/blank'
require 'active_support/values/time_zone'

module EacRubyUtils
  module LocalTimeZone
    DEBIAN_CONFIG_PATH = '/etc/timezone'

    class << self
      TIMEDATECTL_TIMEZONE_LINE_PATTERN = %r{\s*Time zone:\s*(\S+/\S+)\s}.freeze

      def auto
        %w[tz_env debian_config offset].lazy.map { |s| send("by_#{s}") }.find(&:present?)
      end

      def by_debian_config
        path = ::Pathname.new(DEBIAN_CONFIG_PATH)
        path.exist? ? path.read.strip.presence : nil
      end

      def by_offset
        ::ActiveSupport::TimeZone[::Time.now.getlocal.gmt_offset].name
      end

      def by_timedatectl
        executable = ::EacRubyUtils::Envs.local.executable('timedatectl', '--version')
        return nil unless executable.exist?

        TIMEDATECTL_TIMEZONE_LINE_PATTERN.if_match(executable.command.execute!) { |m| m[1] }
      end

      def by_tz_env
        ENV['TZ'].presence
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
avm-tools-0.89.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.88.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.87.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
ehbrs-tools-0.19.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
eac_ruby_utils-0.58.0 lib/eac_ruby_utils/local_time_zone.rb
ehbrs-tools-0.18.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
eac_ruby_utils-0.57.2 lib/eac_ruby_utils/local_time_zone.rb