timezone_local 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +31 -0
- data/VERSION +1 -0
- data/lib/timezone_local.rb +35 -0
- data/lib/unix.rb +138 -0
- data/timezone_local.gemspec +40 -0
- metadata +79 -0
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
begin
|
3
|
+
require 'rubygems'
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gemspec|
|
6
|
+
gemspec.name = "timezone_local"
|
7
|
+
gemspec.summary = "Determine the local system's time zone"
|
8
|
+
gemspec.description = ""
|
9
|
+
gemspec.email = "chetan@evidon.com"
|
10
|
+
gemspec.homepage = "http://github.com/chetan/timezone_local"
|
11
|
+
gemspec.authors = ["Chetan Sarva"]
|
12
|
+
gemspec.add_dependency('tzinfo', '>= 0.3.24')
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require "rake/testtask"
|
20
|
+
desc "Run unit tests"
|
21
|
+
Rake::TestTask.new("test") { |t|
|
22
|
+
#t.libs << "test"
|
23
|
+
t.ruby_opts << "-rubygems"
|
24
|
+
t.pattern = "test/**/*_test.rb"
|
25
|
+
t.verbose = false
|
26
|
+
t.warning = false
|
27
|
+
}
|
28
|
+
|
29
|
+
require "yard"
|
30
|
+
YARD::Rake::YardocTask.new("docs") do |t|
|
31
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
require 'tzinfo'
|
3
|
+
|
4
|
+
module TimeZone
|
5
|
+
module Local
|
6
|
+
|
7
|
+
# load the correct module depending on OS
|
8
|
+
if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM then
|
9
|
+
require File.join(File.dirname(__FILE__), 'windows')
|
10
|
+
include TimeZone::Local::Windows
|
11
|
+
else
|
12
|
+
require File.join(File.dirname(__FILE__), 'unix')
|
13
|
+
extend TimeZone::Local::Unix
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get
|
17
|
+
|
18
|
+
self.METHODS.each do |m|
|
19
|
+
begin
|
20
|
+
tz = self.send(m)
|
21
|
+
return tz if not tz.nil?
|
22
|
+
rescue Exception => ex
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class TZInfo::Timezone
|
32
|
+
def self.get_local_timezone
|
33
|
+
TimeZone::Local.get
|
34
|
+
end
|
35
|
+
end
|
data/lib/unix.rb
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
|
2
|
+
require 'find'
|
3
|
+
require 'ftools'
|
4
|
+
|
5
|
+
module TimeZone::Local
|
6
|
+
module Unix
|
7
|
+
|
8
|
+
def METHODS
|
9
|
+
[ :from_env, :from_etc_localtime, :from_etc_timezone, :from_etc_TIMEZONE,
|
10
|
+
:from_etc_sysconfig_clock, :from_etc_default_init ]
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_env
|
14
|
+
return ENV["TZ"] if ENV.include? "TZ"
|
15
|
+
end
|
16
|
+
|
17
|
+
def from_etc_localtime
|
18
|
+
|
19
|
+
lt_file = "/etc/localtime"
|
20
|
+
|
21
|
+
# check if it is symlinked
|
22
|
+
real_name = expand_symlink(lt_file)
|
23
|
+
|
24
|
+
# search for it not found
|
25
|
+
real_name ||= find_matching_zoneinfo_file(lt_file)
|
26
|
+
|
27
|
+
return nil if real_name.nil?
|
28
|
+
|
29
|
+
dirs, file = File.split(real_name)
|
30
|
+
parts = dirs.split(File::Separator).reject{ |f| f.empty? } << file
|
31
|
+
parts.reverse.each_with_index do |part, x|
|
32
|
+
name = x < parts.size ? parts[x..parts.size].join("/") : part
|
33
|
+
tz = load_tz(name)
|
34
|
+
return tz if not tz.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def expand_symlink(f)
|
40
|
+
return nil if File.symlink?(f)
|
41
|
+
File.expand_path(File.readlink(f), File.dirname(File.expand_path(f)))
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_matching_zoneinfo_file(file_to_match)
|
45
|
+
|
46
|
+
zoneinfo = '/usr/share/zoneinfo'
|
47
|
+
return if not (File.exists? file_to_match and File.directory? zoneinfo)
|
48
|
+
|
49
|
+
size = File.size(file_to_match) # look for this
|
50
|
+
|
51
|
+
match = nil
|
52
|
+
Find.find(zoneinfo) do |path|
|
53
|
+
if File.file? path and
|
54
|
+
File.size(path) == size and
|
55
|
+
File.basename(path) != 'posixrules' and
|
56
|
+
File.compare(file_to_match, path) then
|
57
|
+
|
58
|
+
match = path
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
return match
|
63
|
+
end
|
64
|
+
|
65
|
+
def from_etc_timezone
|
66
|
+
|
67
|
+
tz_file = '/etc/timezone'
|
68
|
+
|
69
|
+
return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)
|
70
|
+
|
71
|
+
name = IO.read(tz_file).strip
|
72
|
+
|
73
|
+
return nil if not is_valid_name? name
|
74
|
+
return load_tz(name)
|
75
|
+
end
|
76
|
+
|
77
|
+
def from_etc_TIMEZONE
|
78
|
+
tz_file = '/etc/timezone'
|
79
|
+
|
80
|
+
return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)
|
81
|
+
|
82
|
+
IO.readlines(tz_file).each do |line|
|
83
|
+
if line =~ /\A\s*TZ\s*=\s*(\S+)/ then
|
84
|
+
name = $1
|
85
|
+
break
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
return nil if not is_valid_name? name
|
90
|
+
return load_tz(name)
|
91
|
+
end
|
92
|
+
|
93
|
+
def from_etc_sysconfig_clock
|
94
|
+
tz_file = '/etc/sysconfig/clock'
|
95
|
+
return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)
|
96
|
+
IO.readlines(tz_file).each do |line|
|
97
|
+
if line =~ /^(?:TIME)?ZONE="([^"]+)"/ then
|
98
|
+
name = $1
|
99
|
+
break
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
return nil if not is_valid_name? name
|
104
|
+
return load_tz(name)
|
105
|
+
end
|
106
|
+
|
107
|
+
def from_etc_default_init
|
108
|
+
tz_file = '/etc/default/init'
|
109
|
+
return nil if not (File.exists? tz_file and File.file? tz_file and File.readable? tz_file)
|
110
|
+
|
111
|
+
IO.readlines(tz_file).each do |line|
|
112
|
+
if line =~ /^TZ=(.+)/ then
|
113
|
+
name = $1
|
114
|
+
break
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return nil if not is_valid_name? name
|
119
|
+
return load_tz(name)
|
120
|
+
end
|
121
|
+
|
122
|
+
def load_tz(name)
|
123
|
+
begin
|
124
|
+
tz = TZInfo::Timezone.get(name)
|
125
|
+
return tz
|
126
|
+
rescue Exception => ex
|
127
|
+
end
|
128
|
+
return nil
|
129
|
+
end
|
130
|
+
|
131
|
+
def is_valid_name?(name)
|
132
|
+
return false if name.nil? or name.empty? or name == 'local'
|
133
|
+
return name =~ %r{^[\w/\-\+]+$}
|
134
|
+
end
|
135
|
+
|
136
|
+
end # Unix
|
137
|
+
end # DateTime
|
138
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{timezone_local}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Chetan Sarva"]
|
12
|
+
s.date = %q{2011-03-28}
|
13
|
+
s.description = %q{}
|
14
|
+
s.email = %q{chetan@evidon.com}
|
15
|
+
s.files = [
|
16
|
+
"Rakefile",
|
17
|
+
"VERSION",
|
18
|
+
"lib/timezone_local.rb",
|
19
|
+
"lib/unix.rb",
|
20
|
+
"timezone_local.gemspec"
|
21
|
+
]
|
22
|
+
s.homepage = %q{http://github.com/chetan/timezone_local}
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
s.rubygems_version = %q{1.3.6}
|
25
|
+
s.summary = %q{Determine the local system's time zone}
|
26
|
+
|
27
|
+
if s.respond_to? :specification_version then
|
28
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
29
|
+
s.specification_version = 3
|
30
|
+
|
31
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
32
|
+
s.add_runtime_dependency(%q<tzinfo>, [">= 0.3.24"])
|
33
|
+
else
|
34
|
+
s.add_dependency(%q<tzinfo>, [">= 0.3.24"])
|
35
|
+
end
|
36
|
+
else
|
37
|
+
s.add_dependency(%q<tzinfo>, [">= 0.3.24"])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timezone_local
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Chetan Sarva
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-28 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: tzinfo
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 3
|
30
|
+
- 24
|
31
|
+
version: 0.3.24
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: ""
|
35
|
+
email: chetan@evidon.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- Rakefile
|
44
|
+
- VERSION
|
45
|
+
- lib/timezone_local.rb
|
46
|
+
- lib/unix.rb
|
47
|
+
- timezone_local.gemspec
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/chetan/timezone_local
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.6
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Determine the local system's time zone
|
78
|
+
test_files: []
|
79
|
+
|