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
- .auto ⇒ ActiveSupport::TimeZone
- .auto_set ⇒ Object
- .by_debian_config ⇒ ActiveSupport::TimeZone
- .by_offset ⇒ ActiveSupport::TimeZone
- .by_timedatectl ⇒ ActiveSupport::TimeZone
- .by_tz_env ⇒ ActiveSupport::TimeZone
Class Method Details
.auto ⇒ 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_set ⇒ Object
18 19 20 |
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 18 def auto_set ::Time.zone = auto end |
.by_debian_config ⇒ 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_offset ⇒ 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_timedatectl ⇒ 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_env ⇒ ActiveSupport::TimeZone
43 44 45 |
# File 'lib/eac_ruby_utils/local_time_zone.rb', line 43 def by_tz_env ENV['TZ'].presence end |