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

.autoActiveSupport::TimeZone

Returns:

  • (ActiveSupport::TimeZone)


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

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

.auto_setObject



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

def auto_set
  ::Time.zone = auto
end

.by_debian_configActiveSupport::TimeZone

Returns:

  • (ActiveSupport::TimeZone)


23
24
25
26
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 23

def by_debian_config
  path = ::Pathname.new(DEBIAN_CONFIG_PATH)
  path.exist? ? path.read.strip.if_present { |v| ::ActiveSupport::TimeZone[v] } : nil
end

.by_offsetActiveSupport::TimeZone

Returns:

  • (ActiveSupport::TimeZone)


29
30
31
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 29

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

.by_timedatectlActiveSupport::TimeZone

Returns:

  • (ActiveSupport::TimeZone)


34
35
36
37
38
39
40
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 34

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] }
    .if_present { |v| ::ActiveSupport::TimeZone[v] }
end

.by_tz_envActiveSupport::TimeZone

Returns:

  • (ActiveSupport::TimeZone)


43
44
45
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 43

def by_tz_env
  ENV['TZ'].presence
end