Module: Time::Zone

Defined in:
lib/time/zone.rb,
lib/time/zone/locking.rb,
lib/time/zone/version.rb

Constant Summary collapse

LOCK =
Mutex.new
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.convert(time, zone) ⇒ Object



34
35
36
37
38
# File 'lib/time/zone.rb', line 34

def self.convert(time, zone)
  with(zone) do
    return time.localtime
  end
end

.now(zone) ⇒ Object



27
28
29
30
31
32
# File 'lib/time/zone.rb', line 27

def self.now(zone)
  with(zone) do
    # Time instances are lazy initialized, so we need to force it to pick up the current TZ by invoking #localtime
    return Time.new.localtime
  end
end

.with(zone) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/time/zone/locking.rb', line 27

def self.with(zone)
  LOCK.synchronize do
    original_zone = ENV['TZ']
    
    begin
      ENV['TZ'] = zone
      
      yield
    ensure
      if original_zone
        ENV['TZ'] = original_zone
      else
        ENV.delete('TZ')
      end
    end
  end
end