cowtech-extensions 2.1.3 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.yardopts +1 -0
- data/README.md +10 -0
- data/Rakefile +6 -3
- data/cowtech-extensions.gemspec +9 -5
- data/doc/Cowtech.html +128 -0
- data/doc/Cowtech/Extensions.html +546 -0
- data/doc/Cowtech/Extensions/Boolean.html +297 -0
- data/doc/Cowtech/Extensions/DateTime.html +787 -0
- data/doc/Cowtech/Extensions/DateTime/ClassMethods.html +1592 -0
- data/doc/Cowtech/Extensions/Exceptions.html +125 -0
- data/doc/Cowtech/Extensions/Exceptions/Dump.html +133 -0
- data/doc/Cowtech/Extensions/Hash.html +393 -0
- data/doc/Cowtech/Extensions/Math.html +130 -0
- data/doc/Cowtech/Extensions/Math/ClassMethods.html +362 -0
- data/doc/Cowtech/Extensions/Object.html +1565 -0
- data/doc/Cowtech/Extensions/Pathname.html +225 -0
- data/doc/Cowtech/Extensions/Settings.html +1249 -0
- data/doc/Cowtech/Extensions/String.html +471 -0
- data/doc/Cowtech/Extensions/TimeZone.html +1210 -0
- data/doc/Cowtech/Extensions/TimeZone/ClassMethods.html +925 -0
- data/doc/Cowtech/Extensions/Version.html +189 -0
- data/doc/_index.html +305 -0
- data/doc/class_list.html +53 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +328 -0
- data/doc/file.README.html +103 -0
- data/doc/file_list.html +55 -0
- data/doc/frames.html +28 -0
- data/doc/index.html +103 -0
- data/doc/js/app.js +214 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +620 -0
- data/doc/top-level-namespace.html +112 -0
- data/lib/cowtech-extensions.rb +47 -16
- data/lib/cowtech-extensions/boolean.rb +8 -1
- data/lib/cowtech-extensions/datetime.rb +377 -71
- data/lib/cowtech-extensions/exceptions.rb +16 -0
- data/lib/cowtech-extensions/hash.rb +20 -9
- data/lib/cowtech-extensions/math.rb +15 -8
- data/lib/cowtech-extensions/object.rb +84 -27
- data/lib/cowtech-extensions/pathname.rb +10 -1
- data/lib/cowtech-extensions/settings.rb +120 -0
- data/lib/cowtech-extensions/string.rb +30 -3
- data/lib/cowtech-extensions/version.rb +11 -2
- data/spec/coverage_helper.rb +19 -0
- data/spec/cowtech-extensions/boolean_spec.rb +4 -0
- data/spec/cowtech-extensions/datetime_spec.rb +238 -79
- data/spec/cowtech-extensions/hash_spec.rb +5 -2
- data/spec/cowtech-extensions/math_spec.rb +14 -4
- data/spec/cowtech-extensions/object_spec.rb +19 -1
- data/spec/cowtech-extensions/pathname_spec.rb +5 -1
- data/spec/cowtech-extensions/settings_spec.rb +101 -0
- data/spec/cowtech-extensions/string_spec.rb +13 -0
- data/spec/cowtech-extensions_spec.rb +33 -13
- data/spec/spec_helper.rb +2 -5
- metadata +182 -97
- data/lib/cowtech-extensions/utils.rb +0 -74
@@ -6,11 +6,20 @@
|
|
6
6
|
|
7
7
|
module Cowtech
|
8
8
|
module Extensions
|
9
|
+
# The current version of cowtech-extensions, according semantic versioning.
|
10
|
+
#
|
11
|
+
# @see http://semver.org
|
9
12
|
module Version
|
13
|
+
# The major version.
|
10
14
|
MAJOR = 2
|
11
|
-
MINOR = 1
|
12
|
-
PATCH = 3
|
13
15
|
|
16
|
+
# The minor version.
|
17
|
+
MINOR = 5
|
18
|
+
|
19
|
+
# The patch version.
|
20
|
+
PATCH = 0
|
21
|
+
|
22
|
+
# The current version of cowtech-extensions.
|
14
23
|
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
|
15
24
|
end
|
16
25
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "simplecov"
|
8
|
+
require "pathname"
|
9
|
+
|
10
|
+
if ENV["COWTECH_EXTENSIONS_COVERAGE"] == "TRUE" then
|
11
|
+
root = Pathname.new(File.dirname(__FILE__)) + ".."
|
12
|
+
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter do |src_file|
|
15
|
+
path = Pathname.new(src_file.filename).relative_path_from(root).to_s
|
16
|
+
path !~ /^lib/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -7,6 +7,10 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Cowtech::Extensions::Boolean do
|
10
|
+
before(:all) do
|
11
|
+
::Cowtech::Extensions.load!
|
12
|
+
end
|
13
|
+
|
10
14
|
describe "#to_i" do
|
11
15
|
it "should return 1 for true" do true.to_i.should == 1 end
|
12
16
|
it "should return 0 for false" do false.to_i.should == 0 end
|
@@ -7,116 +7,136 @@
|
|
7
7
|
require "spec_helper"
|
8
8
|
|
9
9
|
describe Cowtech::Extensions::DateTime do
|
10
|
-
let(:random_reference) { DateTime.civil(1990 + rand(30), 1 + rand(10), 1 + rand(25), 1 + rand(20), 1 + rand(58), 1 + rand(58)).in_time_zone }
|
11
|
-
let(:fixed_reference){
|
12
|
-
|
13
|
-
|
14
|
-
}
|
10
|
+
let(:random_reference) { ::DateTime.civil(1990 + rand(30), 1 + rand(10), 1 + rand(25), 1 + rand(20), 1 + rand(58), 1 + rand(58)).in_time_zone }
|
11
|
+
let(:fixed_reference){ ::DateTime.civil(2005, 6, 7, 8, 9, 10, ::DateTime.rationalize_offset(25200)) }
|
12
|
+
let(:reference_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
13
|
+
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
15
14
|
|
16
|
-
|
15
|
+
before(:all) do
|
16
|
+
::Cowtech::Extensions.load!
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".days" do
|
17
20
|
it "should return the list of the days of the week" do
|
18
|
-
DateTime.days.should be_kind_of(Array)
|
19
|
-
DateTime.days[3].should == {:value => "4", :label => "Wed"}
|
20
|
-
DateTime.days(false).should be_kind_of(Array)
|
21
|
-
DateTime.days(false)[3].should == {:value => "4", :label => "Wednesday"}
|
21
|
+
::DateTime.days.should be_kind_of(::Array)
|
22
|
+
::DateTime.days[3].should == {:value => "4", :label => "Wed"}
|
23
|
+
::DateTime.days(false).should be_kind_of(::Array)
|
24
|
+
::DateTime.days(false)[3].should == {:value => "4", :label => "Wednesday"}
|
22
25
|
|
23
|
-
Cowtech::Extensions.settings.setup_date_names(nil, nil, 7.times.collect {|i| (i + 1).to_s * 2}, 7.times.collect {|i| (i + 1).to_s})
|
24
|
-
DateTime.days.should be_kind_of(Array)
|
25
|
-
DateTime.days[3].should == {:value => "4", :label => "4"}
|
26
|
-
DateTime.days(false).should be_kind_of(Array)
|
27
|
-
DateTime.days(false)[3].should == {:value => "4", :label => "44"}
|
26
|
+
::Cowtech::Extensions.settings.setup_date_names(nil, nil, 7.times.collect {|i| (i + 1).to_s * 2}, 7.times.collect {|i| (i + 1).to_s})
|
27
|
+
::DateTime.days.should be_kind_of(::Array)
|
28
|
+
::DateTime.days[3].should == {:value => "4", :label => "4"}
|
29
|
+
::DateTime.days(false).should be_kind_of(::Array)
|
30
|
+
::DateTime.days(false)[3].should == {:value => "4", :label => "44"}
|
28
31
|
end
|
29
32
|
end
|
30
33
|
|
31
|
-
describe "
|
34
|
+
describe ".months" do
|
32
35
|
it "should return the list of the months of the year" do
|
33
|
-
DateTime.months.should be_kind_of(Array)
|
34
|
-
DateTime.months[6].should == {:value => "07", :label => "Jul"}
|
35
|
-
DateTime.months(false).should be_kind_of(Array)
|
36
|
-
DateTime.months(false)[6].should == {:value => "07", :label => "July"}
|
36
|
+
::DateTime.months.should be_kind_of(::Array)
|
37
|
+
::DateTime.months[6].should == {:value => "07", :label => "Jul"}
|
38
|
+
::DateTime.months(false).should be_kind_of(::Array)
|
39
|
+
::DateTime.months(false)[6].should == {:value => "07", :label => "July"}
|
37
40
|
|
38
|
-
Cowtech::Extensions.settings.setup_date_names(12.times.collect {|i| (i + 1).to_s * 2}, 12.times.collect {|i| (i + 1).to_s}, nil, nil)
|
39
|
-
DateTime.months.should be_kind_of(Array)
|
40
|
-
DateTime.months[6].should == {:value => "07", :label => "7"}
|
41
|
-
DateTime.months(false).should be_kind_of(Array)
|
42
|
-
DateTime.months(false)[6].should == {:value => "07", :label => "77"}
|
41
|
+
::Cowtech::Extensions.settings.setup_date_names(12.times.collect {|i| (i + 1).to_s * 2}, 12.times.collect {|i| (i + 1).to_s}, nil, nil)
|
42
|
+
::DateTime.months.should be_kind_of(::Array)
|
43
|
+
::DateTime.months[6].should == {:value => "07", :label => "7"}
|
44
|
+
::DateTime.months(false).should be_kind_of(::Array)
|
45
|
+
::DateTime.months(false)[6].should == {:value => "07", :label => "77"}
|
43
46
|
end
|
44
47
|
|
45
48
|
end
|
46
49
|
|
47
|
-
describe "
|
50
|
+
describe ".years" do
|
48
51
|
it "should return a range of years" do
|
49
|
-
DateTime.years.
|
50
|
-
DateTime.years(5).
|
51
|
-
DateTime.years(5,
|
52
|
-
DateTime.years(5, false
|
52
|
+
::DateTime.years.should == (::Date.today.year - 10..::Date.today.year + 10).to_a
|
53
|
+
::DateTime.years(5).should == (::Date.today.year - 5..::Date.today.year + 5).to_a
|
54
|
+
::DateTime.years(5, true, nil, true).collect(&:value).should == (::Date.today.year - 5..::Date.today.year + 5).to_a
|
55
|
+
::DateTime.years(5, false).should == (::Date.today.year - 5..::Date.today.year).to_a
|
56
|
+
::DateTime.years(5, false, 1900).should == (1895..1900).to_a
|
53
57
|
end
|
54
58
|
end
|
55
59
|
|
56
|
-
describe "
|
57
|
-
it "should
|
58
|
-
{1984 => "0422", 1995 => "0416", 2006 => "0416", 2017 => "0416"}.each do |year, date|
|
59
|
-
DateTime.easter(year).strftime("%Y%m%d").should == "#{year}#{date}"
|
60
|
-
end
|
61
|
-
end
|
60
|
+
describe ".timezones" do
|
61
|
+
it "should list all timezones" do ::DateTime.timezones.should == ::ActiveSupport::TimeZone.all end
|
62
62
|
end
|
63
63
|
|
64
|
-
describe "
|
65
|
-
it "should
|
66
|
-
|
67
|
-
DateTime.
|
68
|
-
|
69
|
-
|
64
|
+
describe ".list_timezones" do
|
65
|
+
it "should forward to ActiveSupport::TimeZone" do
|
66
|
+
::ActiveSupport::TimeZone.should_receive(:list_all)
|
67
|
+
::DateTime.list_timezones
|
68
|
+
end
|
69
|
+
end
|
70
70
|
|
71
|
-
|
72
|
-
|
71
|
+
describe ".find_timezone" do
|
72
|
+
it "should forward to ActiveSupport::TimeZone" do
|
73
|
+
::ActiveSupport::TimeZone.should_receive(:find)
|
74
|
+
::DateTime.find_timezone(reference_zone.name)
|
73
75
|
end
|
76
|
+
end
|
74
77
|
|
75
|
-
|
78
|
+
describe ".rationalize_offset" do
|
79
|
+
it "should return the correct rational value" do
|
80
|
+
::ActiveSupport::TimeZone.should_receive(:rationalize_offset)
|
81
|
+
::DateTime.rationalize_offset(0)
|
82
|
+
end
|
76
83
|
end
|
77
84
|
|
78
|
-
describe "
|
79
|
-
it "should
|
80
|
-
|
81
|
-
DateTime.
|
85
|
+
describe ".parameterize_zone" do
|
86
|
+
it "should forward to ActiveSupport::TimeZone" do
|
87
|
+
::ActiveSupport::TimeZone.should_receive(:parameterize_zone)
|
88
|
+
::DateTime.parameterize_zone(reference_zone)
|
82
89
|
end
|
90
|
+
end
|
83
91
|
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
describe ".unparameterize_zone" do
|
93
|
+
it "should forward to ActiveSupport::TimeZone" do
|
94
|
+
::ActiveSupport::TimeZone.should_receive(:unparameterize_zone)
|
95
|
+
::DateTime.unparameterize_zone(reference_zone)
|
87
96
|
end
|
88
97
|
end
|
89
98
|
|
90
|
-
describe "
|
91
|
-
it "should
|
92
|
-
|
93
|
-
|
99
|
+
describe ".easter" do
|
100
|
+
it "should compute the valid Easter day" do
|
101
|
+
{1984 => "0422", 1995 => "0416", 2006 => "0416", 2017 => "0416"}.each do |year, date|
|
102
|
+
::DateTime.easter(year).strftime("%Y%m%d").should == "#{year}#{date}"
|
103
|
+
end
|
94
104
|
end
|
95
105
|
end
|
96
106
|
|
97
|
-
describe "
|
98
|
-
it "should
|
99
|
-
DateTime.
|
100
|
-
DateTime.
|
101
|
-
|
107
|
+
describe ".custom_format" do
|
108
|
+
it "should find the format" do
|
109
|
+
::DateTime.custom_format(:ct_date).should == "%Y-%m-%d"
|
110
|
+
::DateTime.custom_format("ct_date").should == "%Y-%m-%d"
|
111
|
+
|
112
|
+
::Cowtech::Extensions.settings.setup_date_formats({:ct_foo => "%ABC"})
|
113
|
+
|
114
|
+
::DateTime.custom_format(:ct_foo).should == "%ABC"
|
115
|
+
::DateTime.custom_format("ct_foo").should == "%ABC"
|
102
116
|
end
|
117
|
+
|
118
|
+
it "should return the key if format is not found" do ::DateTime.custom_format(:ct_unused) == "ct_unused" end
|
103
119
|
end
|
104
120
|
|
105
|
-
describe "
|
106
|
-
it "should
|
107
|
-
DateTime.
|
108
|
-
DateTime.
|
109
|
-
|
121
|
+
describe ".is_valid?" do
|
122
|
+
it "should recognize a valid date" do
|
123
|
+
::DateTime.is_valid?("2012-04-05", "%F").should be_true
|
124
|
+
::DateTime.is_valid?("2012-04-05", :ct_date).should be_true
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should fail if the argument or the format is not valid" do
|
128
|
+
::DateTime.is_valid?("ABC", "%F").should be_false
|
129
|
+
::DateTime.is_valid?("2012-04-05", "%X").should be_false
|
110
130
|
end
|
111
131
|
end
|
112
132
|
|
113
133
|
describe "#utc_time" do
|
114
|
-
it "should convert to UTC Time" do random_reference.utc_time.should
|
134
|
+
it "should convert to UTC Time" do random_reference.utc_time.should be_a(::Time) end
|
115
135
|
end
|
116
136
|
|
117
137
|
describe "#in_months" do
|
118
138
|
it "should return the amount of months passed since the start of the reference year" do
|
119
|
-
Date.today.in_months.should == Date.today.month
|
139
|
+
::Date.today.in_months.should == ::Date.today.month
|
120
140
|
fixed_reference.in_months(2000).should == 66
|
121
141
|
end
|
122
142
|
end
|
@@ -124,7 +144,7 @@ describe Cowtech::Extensions::DateTime do
|
|
124
144
|
describe "#padded_month" do
|
125
145
|
it "should pad the month number" do
|
126
146
|
random_reference.padded_month.should == random_reference.month.to_s.rjust(2, "0")
|
127
|
-
Date.civil(2000, 8, 8).padded_month.should == "08"
|
147
|
+
::Date.civil(2000, 8, 8).padded_month.should == "08"
|
128
148
|
end
|
129
149
|
end
|
130
150
|
|
@@ -134,35 +154,43 @@ describe Cowtech::Extensions::DateTime do
|
|
134
154
|
fixed_reference.lstrftime("%F").should == "2005-06-07"
|
135
155
|
fixed_reference.lstrftime(:ct_iso_8601).should == "2005-06-07T08:09:10+0700"
|
136
156
|
|
137
|
-
Cowtech::Extensions.settings.setup_date_names
|
138
|
-
Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
157
|
+
::Cowtech::Extensions.settings.setup_date_names
|
158
|
+
::Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
139
159
|
fixed_reference.lstrftime(:ct_local_test).should == "Tue Tuesday Jun June 07 2005 08"
|
140
160
|
|
141
|
-
Cowtech::Extensions.settings.setup_date_names(
|
161
|
+
::Cowtech::Extensions.settings.setup_date_names(
|
142
162
|
12.times.collect {|i| (i + 1).to_s * 2}, 12.times.collect {|i| (i + 1).to_s},
|
143
163
|
7.times.collect {|i| (i + 1).to_s * 2}, 7.times.collect {|i| (i + 1).to_s}
|
144
164
|
)
|
145
165
|
|
146
166
|
fixed_reference.lstrftime(:ct_local_test).should == "3 33 6 66 07 2005 08"
|
147
|
-
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should fix Ruby 1.8 %z and %Z bug" do
|
170
|
+
original_ruby_version = RUBY_VERSION
|
171
|
+
::Kernel::silence_warnings { Object.const_set("RUBY_VERSION", "1.9.3") }
|
172
|
+
fixed_reference.lstrftime("%z").should == "+0700"
|
173
|
+
fixed_reference.lstrftime("%:z").should == "+07:00"
|
174
|
+
::Kernel::silence_warnings { Object.const_set("RUBY_VERSION", original_ruby_version) }
|
175
|
+
end
|
148
176
|
end
|
149
177
|
|
150
178
|
describe "#local_strftime" do
|
151
179
|
it "should retrieve the date in the current timezone" do
|
152
|
-
::Time.zone = ActiveSupport::TimeZone[0]
|
153
|
-
Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
180
|
+
::Time.zone = ::ActiveSupport::TimeZone[0]
|
181
|
+
::Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
154
182
|
fixed_reference.local_strftime(:ct_local_test).should == "Tue Tuesday Jun June 07 2005 01"
|
155
183
|
end
|
156
184
|
end
|
157
185
|
|
158
186
|
describe "#local_lstrftime" do
|
159
187
|
it "should retrieve the date in the current timezone" do
|
160
|
-
::Time.zone = ActiveSupport::TimeZone[0]
|
188
|
+
::Time.zone = ::ActiveSupport::TimeZone[0]
|
161
189
|
|
162
|
-
Cowtech::Extensions.settings.setup_date_names
|
163
|
-
Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
190
|
+
::Cowtech::Extensions.settings.setup_date_names
|
191
|
+
::Cowtech::Extensions.settings.setup_date_formats({:ct_local_test => "%a %A %b %B %d %Y %H"})
|
164
192
|
|
165
|
-
Cowtech::Extensions.settings.setup_date_names(
|
193
|
+
::Cowtech::Extensions.settings.setup_date_names(
|
166
194
|
12.times.collect {|i| (i + 1).to_s * 2}, 12.times.collect {|i| (i + 1).to_s},
|
167
195
|
7.times.collect {|i| (i + 1).to_s * 2}, 7.times.collect {|i| (i + 1).to_s}
|
168
196
|
)
|
@@ -170,4 +198,135 @@ describe Cowtech::Extensions::DateTime do
|
|
170
198
|
fixed_reference.local_lstrftime(:ct_local_test).should == "3 33 6 66 07 2005 01"
|
171
199
|
end
|
172
200
|
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe Cowtech::Extensions::TimeZone do
|
204
|
+
let(:reference_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
205
|
+
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
206
|
+
|
207
|
+
before(:all) do
|
208
|
+
::Cowtech::Extensions.load!
|
209
|
+
end
|
210
|
+
|
211
|
+
describe ".rationalize_offset" do
|
212
|
+
it "should return the correct rational value" do
|
213
|
+
::ActiveSupport::TimeZone.rationalize_offset(::ActiveSupport::TimeZone[4]).should == Rational(1, 6)
|
214
|
+
::ActiveSupport::TimeZone.rationalize_offset(-25200).should == Rational(-7, 24)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
describe ".format_offset" do
|
219
|
+
it "should correctly format an offset" do
|
220
|
+
::ActiveSupport::TimeZone.format_offset(-25200).should == "-07:00"
|
221
|
+
::ActiveSupport::TimeZone.format_offset(Rational(-4, 24), false).should == "-0400"
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
describe ".parameterize_zone" do
|
226
|
+
it "should return the parameterized version of the zone" do
|
227
|
+
::ActiveSupport::TimeZone.parameterize_zone(reference_zone).should == reference_zone.to_s_parameterized
|
228
|
+
::ActiveSupport::TimeZone.parameterize_zone(reference_zone).should == reference_zone.to_s_parameterized
|
229
|
+
::ActiveSupport::TimeZone.parameterize_zone(reference_zone, false).should == reference_zone.to_s_parameterized(false)
|
230
|
+
::ActiveSupport::TimeZone.parameterize_zone("INVALID").should == "invalid"
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe ".unparameterize_zone" do
|
235
|
+
it "should return the parameterized version of the zone" do
|
236
|
+
::ActiveSupport::TimeZone.unparameterize_zone(reference_zone.to_s_parameterized).should == reference_zone
|
237
|
+
::ActiveSupport::TimeZone.unparameterize_zone(reference_zone.to_s_parameterized, true).should == reference_zone.to_s
|
238
|
+
::ActiveSupport::TimeZone.unparameterize_zone(reference_zone.to_s_with_dst_parameterized).should == reference_zone
|
239
|
+
::ActiveSupport::TimeZone.unparameterize_zone(reference_zone.to_s_with_dst_parameterized, true).should == reference_zone.to_s_with_dst
|
240
|
+
::ActiveSupport::TimeZone.unparameterize_zone("INVALID").should == nil
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe ".find" do
|
245
|
+
it "should find timezones" do
|
246
|
+
::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)").should == reference_zone
|
247
|
+
::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (Daylight Saving Time)").should == reference_zone
|
248
|
+
::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) DST", "DST").should == reference_zone
|
249
|
+
::ActiveSupport::TimeZone.find("INVALID", "INVALID").should be_nil
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe ".list_all" do
|
254
|
+
it "should list all timezones" do
|
255
|
+
::ActiveSupport::TimeZone.list_all(false).should == ::ActiveSupport::TimeZone.all.collect(&:to_s)
|
256
|
+
::ActiveSupport::TimeZone.list_all(true).should include("(GMT-06:00) Mountain Time (US & Canada) (Daylight Saving Time)")
|
257
|
+
::ActiveSupport::TimeZone.list_all(true, "DST").should include("(GMT-06:00) Mountain Time (US & Canada) DST")
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe "#offset" do
|
262
|
+
it "should correctly return zone offset" do
|
263
|
+
reference_zone.offset.should == reference_zone.utc_offset
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe "#current_offset" do
|
268
|
+
it "should correctly return current zone offset" do
|
269
|
+
reference_zone.current_offset(false, ::DateTime.civil(2012, 1, 15)).should == reference_zone.offset
|
270
|
+
reference_zone.current_offset(true, ::DateTime.civil(2012, 7, 15)).should == reference_zone.dst_offset(true)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
describe "#dst_period" do
|
275
|
+
it "should correctly return zone offset" do
|
276
|
+
reference_zone.dst_period.should be_a(::TZInfo::TimezonePeriod)
|
277
|
+
reference_zone.dst_period(1000).should be_nil
|
278
|
+
zone_without_dst.dst_period.should be_nil
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
describe "#uses_dst?" do
|
283
|
+
it "should correctly detect offset usage" do
|
284
|
+
reference_zone.uses_dst?.should be_true
|
285
|
+
reference_zone.uses_dst?(1000).should be_false
|
286
|
+
zone_without_dst.uses_dst?.should be_false
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
describe "#dst_correction" do
|
291
|
+
it "should correctly detect offset usage" do
|
292
|
+
reference_zone.dst_correction.should == 3600
|
293
|
+
reference_zone.dst_correction(true).should == Rational(1, 24)
|
294
|
+
reference_zone.dst_correction(false, 1000).should == 0
|
295
|
+
zone_without_dst.dst_correction.should == 0
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
describe "#dst_offset" do
|
300
|
+
it "should correctly return zone offset" do
|
301
|
+
reference_zone.dst_offset.should == reference_zone.dst_correction + reference_zone.utc_offset
|
302
|
+
reference_zone.dst_offset(true).should == ::ActiveSupport::TimeZone.rationalize_offset(reference_zone.dst_correction + reference_zone.utc_offset)
|
303
|
+
zone_without_dst.dst_offset(false, 1000).should == 0
|
304
|
+
zone_without_dst.dst_offset.should == 0
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
describe "#to_s_with_dst" do
|
309
|
+
it "should correctly format zone name" do
|
310
|
+
reference_zone.to_s_with_dst.should == "(GMT-06:00) Mountain Time (US & Canada) (Daylight Saving Time)"
|
311
|
+
reference_zone.to_s_with_dst("DST").should == "(GMT-06:00) Mountain Time (US & Canada) DST"
|
312
|
+
reference_zone.to_s_with_dst(nil, 1000).should be_nil
|
313
|
+
zone_without_dst.to_s_with_dst.should be_nil
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
describe "#to_s_parameterized" do
|
318
|
+
it "should correctly parameterize zone name" do
|
319
|
+
reference_zone.to_s_parameterized.should == ::ActiveSupport::TimeZone.parameterize_zone(reference_zone)
|
320
|
+
reference_zone.to_s_parameterized(false).should == ::ActiveSupport::TimeZone.parameterize_zone(reference_zone, false)
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
describe "#to_s_with_dst_parameterized" do
|
325
|
+
it "should correctly parameterize zone name" do
|
326
|
+
reference_zone.to_s_with_dst_parameterized.should == "-0600@mountain-time-us-canada-daylight-saving-time"
|
327
|
+
reference_zone.to_s_with_dst_parameterized("DST").should == "-0600@mountain-time-us-canada-dst"
|
328
|
+
reference_zone.to_s_with_dst_parameterized(nil, false, 1000).should be_nil
|
329
|
+
zone_without_dst.to_s_with_dst_parameterized.should be_nil
|
330
|
+
end
|
331
|
+
end
|
173
332
|
end
|