Module: SidekiqVigil::Timezone

Defined in:
lib/sidekiq_vigil/timezone.rb

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.local_time(time, zone) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sidekiq_vigil/timezone.rb', line 11

def local_time(time, zone)
  return time.getutc if zone == "UTC"
  return time.getlocal(zone) if zone.match?(/\A[+-]\d{2}:\d{2}\z/)

  MUTEX.synchronize do
    previous = ENV.fetch("TZ", nil)
    ENV["TZ"] = zone
    Time.at(time.to_f).localtime
  ensure
    ENV["TZ"] = previous
  end
end

.same_local_days_ago(time, days, zone) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sidekiq_vigil/timezone.rb', line 24

def same_local_days_ago(time, days, zone)
  local = local_time(time, zone)
  date = local.to_date - days
  return Time.utc(date.year, date.month, date.day, local.hour, local.min, local.sec) if zone == "UTC"
  return Time.new(date.year, date.month, date.day, local.hour, local.min, local.sec, zone) if zone.match?(/\A[+-]\d{2}:\d{2}\z/)

  MUTEX.synchronize do
    previous = ENV.fetch("TZ", nil)
    ENV["TZ"] = zone
    Time.local(date.year, date.month, date.day, local.hour, local.min, local.sec)
  ensure
    ENV["TZ"] = previous
  end
end