Sha256: 55568edc43d56754381dc73c572f87d1958bdba89fffca3d022358952638f21f

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

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 auto_set
        ::Time.zone = auto
      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

6 entries across 6 versions & 2 rubygems

Version Path
avm-tools-0.94.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.93.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.92.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.91.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
avm-tools-0.90.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/local_time_zone.rb
eac_ruby_utils-0.58.1 lib/eac_ruby_utils/local_time_zone.rb