Module: Time::Zone

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

Defined Under Namespace

Classes: Timestamp

Constant Summary collapse

LOCK =
Mutex.new
UTC =
"UTC".freeze
VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.convert(time, zone) ⇒ Object



45
46
47
48
49
# File 'lib/time/zone.rb', line 45

def self.convert(time, zone)
  with(zone) do
    return Time.new(time.year, time.month, time.day, time.hour, time.minute, time.second, time.utc_offset).localtime
  end
end

.new(year, month, day, hour, minute, second, zone) ⇒ Object



32
33
34
35
36
# File 'lib/time/zone.rb', line 32

def self.new(year, month, day, hour, minute, second, zone)
  with(zone) do
    return Time.new(year, month, day, hour, minute, second).localtime
  end
end

.now(zone) ⇒ Object



38
39
40
41
42
43
# File 'lib/time/zone.rb', line 38

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

.parse(string, zone = "UTC", *args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/time/zone.rb', line 51

def self.parse(string, zone = "UTC", *args)
  string = string.dup
  
  if string.sub!(/\s([a-zA-Z][\w]*(\/[\w]+)?)$/, "")
    zone = $1
  end
  
  with(zone) do
    return Time.parse(string, *args).localtime, zone
  end
end

.utc?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/time/zone/locking.rb', line 41

def self.utc?
  zone == UTC
end

.with(zone) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/time/zone/locking.rb', line 45

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

.zoneObject



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

def self.zone
  LOCK.synchronize do
    ENV['TZ']
  end
end

.zone=(zone) ⇒ Object



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

def self.zone=(zone)
  LOCK.synchronize do
    ENV['TZ'] = zone
  end
end