Module: TimeZone::Local::Unix

Included in:
TimeZone::Local
Defined in:
lib/unix.rb

Instance Method Summary collapse

Instance Method Details



39
40
41
42
# File 'lib/unix.rb', line 39

def expand_symlink(f)
    return nil if File.symlink?(f)
    File.expand_path(File.readlink(f), File.dirname(File.expand_path(f)))
end

#find_matching_zoneinfo_file(file_to_match) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/unix.rb', line 44

def find_matching_zoneinfo_file(file_to_match)

    zoneinfo = '/usr/share/zoneinfo'
    return if not (File.exists? file_to_match and File.directory? zoneinfo)

    size = File.size(file_to_match) # look for this

    match = nil
    Find.find(zoneinfo) do |path|
        if File.file? path and
           File.size(path) == size and
           File.basename(path) != 'posixrules' and
           FileUtils.compare_file(file_to_match, path) then

           match = path
       end
    end

    return match
end

#from_envObject



13
14
15
# File 'lib/unix.rb', line 13

def from_env
    return ENV["TZ"] if ENV.include? "TZ"
end

#from_etc_default_initObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/unix.rb', line 107

def from_etc_default_init
    tz_file = '/etc/default/init'
    return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)

    IO.readlines(tz_file).each do |line|
        if line =~ /^TZ=(.+)/ then
            name = $1
            break
        end
    end

    return nil if not is_valid_name? name
    return load_tz(name)
end

#from_etc_localtimeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/unix.rb', line 17

def from_etc_localtime

   lt_file = "/etc/localtime"

   # check if it is symlinked
   real_name = expand_symlink(lt_file)

   # search for it not found
   real_name ||= find_matching_zoneinfo_file(lt_file)

   return nil if real_name.nil?

   dirs, file = File.split(real_name)
   parts = dirs.split(File::Separator).reject{ |f| f.empty? } << file
   parts.reverse.each_with_index do |part, x|
       name = x < parts.size ? parts[x..parts.size].join("/") : part
       tz = load_tz(name)
       return tz if not tz.nil?
   end

end

#from_etc_sysconfig_clockObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/unix.rb', line 93

def from_etc_sysconfig_clock
    tz_file = '/etc/sysconfig/clock'
    return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)
    IO.readlines(tz_file).each do |line|
        if line =~ /^(?:TIME)?ZONE="([^"]+)"/ then
            name = $1
            break
        end
    end

    return nil if not is_valid_name? name
    return load_tz(name)
end

#from_etc_TIMEZONEObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/unix.rb', line 77

def from_etc_TIMEZONE
    tz_file = '/etc/timezone'

    return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)

    IO.readlines(tz_file).each do |line|
        if line =~ /\A\s*TZ\s*=\s*(\S+)/ then
            name = $1
            break
        end
    end

    return nil if not is_valid_name? name
    return load_tz(name)
end

#from_etc_timezoneObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/unix.rb', line 65

def from_etc_timezone

    tz_file = '/etc/timezone'

    return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)

    name = IO.read(tz_file).strip

    return nil if not is_valid_name? name
    return load_tz(name)
end

#is_valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
# File 'lib/unix.rb', line 131

def is_valid_name?(name)
    return false if name.nil? or name.empty? or name == 'local'
    return name =~ %r{^[\w/\-\+]+$}
end

#load_tz(name) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/unix.rb', line 122

def load_tz(name)
    begin
        tz = TZInfo::Timezone.get(name)
        return tz
    rescue Exception => ex
    end
    return nil
end

#METHODSObject



8
9
10
11
# File 'lib/unix.rb', line 8

def METHODS
    [ :from_env, :from_etc_localtime, :from_etc_timezone, :from_etc_TIMEZONE,
      :from_etc_sysconfig_clock, :from_etc_default_init ]
end