Module: EacRubyUtils::LocalTimeZone

Defined in:
lib/eac_ruby_utils/local_time_zone.rb

Constant Summary collapse

DEBIAN_CONFIG_PATH =
'/etc/timezone'
TIMEDATECTL_TIMEZONE_LINE_PATTERN =
%r{\s*Time zone:\s*(\S+/\S+)\s}.freeze

Class Method Summary collapse

Class Method Details

.autoObject



13
14
15
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 13

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

.auto_setObject



17
18
19
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 17

def auto_set
  ::Time.zone = auto
end

.by_debian_configObject



21
22
23
24
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 21

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

.by_offsetObject



26
27
28
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 26

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

.by_timedatectlObject



30
31
32
33
34
35
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 30

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

.by_tz_envObject



37
38
39
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 37

def by_tz_env
  ENV['TZ'].presence
end