lazier 3.5.2 → 3.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/doc/Lazier.html +38 -38
- data/doc/Lazier/Boolean.html +1 -1
- data/doc/Lazier/Configuration.html +1 -1
- data/doc/Lazier/DateTime.html +1 -1
- data/doc/Lazier/DateTime/ClassMethods.html +1 -1
- data/doc/Lazier/Exceptions.html +1 -1
- data/doc/Lazier/Exceptions/Debug.html +1 -1
- data/doc/Lazier/Exceptions/MissingTranslation.html +1 -1
- data/doc/Lazier/Hash.html +1 -1
- data/doc/Lazier/I18n.html +1 -1
- data/doc/Lazier/Localizer.html +1 -1
- data/doc/Lazier/Math.html +1 -1
- data/doc/Lazier/Math/ClassMethods.html +1 -1
- data/doc/Lazier/Object.html +1 -1
- data/doc/Lazier/Pathname.html +1 -1
- data/doc/Lazier/Settings.html +1 -1
- data/doc/Lazier/String.html +1 -1
- data/doc/Lazier/TimeZone.html +79 -79
- data/doc/Lazier/TimeZone/ClassMethods.html +65 -65
- data/doc/Lazier/Version.html +2 -2
- data/doc/_index.html +1 -1
- data/doc/file.README.html +1 -1
- data/doc/index.html +1 -1
- data/doc/method_list.html +24 -24
- data/doc/top-level-namespace.html +1 -1
- data/lib/lazier.rb +1 -0
- data/lib/lazier/datetime.rb +0 -292
- data/lib/lazier/timezone.rb +299 -0
- data/lib/lazier/version.rb +1 -1
- data/spec/lazier/datetime_spec.rb +0 -157
- data/spec/lazier/timezone_spec.rb +164 -0
- metadata +5 -2
@@ -0,0 +1,299 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Lazier
|
8
|
+
# Extensions for timezone objects.
|
9
|
+
module TimeZone
|
10
|
+
extend ::ActiveSupport::Concern
|
11
|
+
|
12
|
+
# General methods.
|
13
|
+
module ClassMethods
|
14
|
+
# Returns an offset in rational value.
|
15
|
+
#
|
16
|
+
# @param offset [Fixnum] The offset to convert.
|
17
|
+
# @return [Rational] The converted offset.
|
18
|
+
def rationalize_offset(offset)
|
19
|
+
::TZInfo::OffsetRationals.rational_for_offset(offset.is_a?(::Fixnum) ? offset : offset.offset)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns a +HH:MM formatted representation of the offset.
|
23
|
+
#
|
24
|
+
# @param offset [Rational|Fixnum] The offset to represent, in seconds or as a rational.
|
25
|
+
# @param colon [Boolean] If to put the colon in the output string.
|
26
|
+
# @return [String] The formatted offset.
|
27
|
+
def format_offset(offset, colon = true)
|
28
|
+
seconds_to_utc_offset(offset.is_a?(::Rational) ? (offset * 86_400).to_i : offset, colon)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Find a zone by its name.
|
32
|
+
#
|
33
|
+
# @param name [String] The zone name.
|
34
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
35
|
+
# @return [TimeZone] A timezone or `nil` if no zone was found.
|
36
|
+
def find(name, dst_label = nil)
|
37
|
+
catch(:zone) do
|
38
|
+
::ActiveSupport::TimeZone.all.each do |zone|
|
39
|
+
zone.aliases.each do |zone_alias|
|
40
|
+
throw(:zone, zone) if [zone.to_str(zone_alias), zone.to_str_with_dst(dst_label, nil, zone_alias)].include?(name)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns a list of names of all timezones.
|
49
|
+
#
|
50
|
+
# @param with_dst [Boolean] If include DST version of the zones.
|
51
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
52
|
+
# @return [Array] A list of names of timezones.
|
53
|
+
def list_all(with_dst = true, dst_label = nil)
|
54
|
+
dst_label ||= "(DST)"
|
55
|
+
|
56
|
+
@zones_names ||= { "STANDARD" => ::ActiveSupport::TimeZone.all.map(&:to_s) }
|
57
|
+
@zones_names["DST[#{dst_label}]-STANDARD"] ||= ::ActiveSupport::TimeZone.all
|
58
|
+
.map { |zone| fetch_aliases(zone, dst_label) }.flatten.compact.uniq
|
59
|
+
.sort { |a, b| ::ActiveSupport::TimeZone.compare(a, b) } # Sort by name
|
60
|
+
|
61
|
+
@zones_names["#{with_dst ? "DST[#{dst_label}]-" : ""}STANDARD"]
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns a string representation of a timezone.
|
65
|
+
#
|
66
|
+
# ```ruby
|
67
|
+
# DateTime.parameterize_zone(ActiveSupport::TimeZone["Pacific Time (US & Canada)"])
|
68
|
+
# # => "-0800@pacific-time-us-canada"
|
69
|
+
# ```
|
70
|
+
# @param tz [TimeZone] The zone to represent.
|
71
|
+
# @param with_offset [Boolean] If to include offset into the representation.
|
72
|
+
# @return [String] A string representation which can be used for searches.
|
73
|
+
def parameterize_zone(tz, with_offset = true)
|
74
|
+
tz = tz.to_s unless tz.is_a?(::String)
|
75
|
+
mo = /^(\([a-z]+([+-])(\d{2})(:?)(\d{2})\)\s(.+))$/i.match(tz)
|
76
|
+
|
77
|
+
if mo
|
78
|
+
with_offset ? "#{mo[2]}#{mo[3]}#{mo[5]}@#{mo[6].to_s.parameterize}" : mo[6].to_s.parameterize
|
79
|
+
elsif !with_offset then
|
80
|
+
tz.gsub(/^([+-]?(\d{2})(:?)(\d{2})@)/, "")
|
81
|
+
else
|
82
|
+
tz.parameterize
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Finds a parameterized timezone.
|
87
|
+
# @see DateTime#parameterize_zone
|
88
|
+
#
|
89
|
+
# @param tz [String] The zone to unparameterize.
|
90
|
+
# @param as_string [Boolean] If return just the zone name.
|
91
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
92
|
+
# @return [String|TimeZone] The found timezone or `nil` if the zone is not valid.
|
93
|
+
def unparameterize_zone(tz, as_string = false, dst_label = nil)
|
94
|
+
tz = parameterize_zone(tz, false)
|
95
|
+
rv = find_parameterized_zone(dst_label, /(#{Regexp.quote(tz)})$/)
|
96
|
+
|
97
|
+
if rv
|
98
|
+
as_string ? rv : find(rv, dst_label)
|
99
|
+
else
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Compares two timezones. They are sorted by the location name.
|
105
|
+
#
|
106
|
+
# @param left [String|TimeZone] The first zone name to compare.
|
107
|
+
# @param right [String|TimeZone] The second zone name to compare.
|
108
|
+
# @return [Fixnum] The result of comparison, like Ruby's operator `<=>`.
|
109
|
+
def compare(left, right)
|
110
|
+
left = left.to_str if left.is_a?(::ActiveSupport::TimeZone)
|
111
|
+
right = right.to_str if right.is_a?(::ActiveSupport::TimeZone)
|
112
|
+
left.ensure_string.split(" ", 2)[1] <=> right.ensure_string.split(" ", 2)[1]
|
113
|
+
end
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
# Returns a list of aliases for a given time zone.
|
118
|
+
#
|
119
|
+
# @param zone [ActiveSupport::TimeZone] The zone.
|
120
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
121
|
+
def fetch_aliases(zone, dst_label = "(DST)")
|
122
|
+
matcher = /(#{Regexp.quote(dst_label)})$/
|
123
|
+
|
124
|
+
zone.aliases.map { |zone_alias|
|
125
|
+
[zone.to_str(zone_alias), (zone.uses_dst? && zone_alias !~ matcher) ? zone.to_str_with_dst(dst_label, nil, zone_alias) : nil]
|
126
|
+
}
|
127
|
+
end
|
128
|
+
|
129
|
+
# Finds a parameterized timezone.
|
130
|
+
#
|
131
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
132
|
+
# @param matcher [Regexp] The expression to match.
|
133
|
+
# @return [TimeZone] The found timezone or `nil` if the zone is not valid.
|
134
|
+
def find_parameterized_zone(dst_label, matcher)
|
135
|
+
catch(:zone) do
|
136
|
+
list_all(true, dst_label).each do |zone|
|
137
|
+
throw(:zone, zone) if parameterize_zone(zone, false) =~ matcher
|
138
|
+
end
|
139
|
+
|
140
|
+
nil
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
# Returns a list of valid aliases (city names) for this timezone (basing on offset).
|
146
|
+
# @return [Array] A list of aliases for this timezone
|
147
|
+
def aliases
|
148
|
+
reference = self.class::MAPPING.fetch(name, name).gsub("_", " ")
|
149
|
+
@aliases ||= ([reference] + self.class::MAPPING.map { |name, zone| format_alias(name, zone, reference) }).uniq.compact.sort
|
150
|
+
end
|
151
|
+
|
152
|
+
# Returns the current offset for this timezone, taking care of Daylight Saving Time (DST).
|
153
|
+
#
|
154
|
+
# @param rational [Boolean] If to return the offset as a Rational.
|
155
|
+
# @param date [DateTime] The date to consider. Defaults to now.
|
156
|
+
# @return [Fixnum|Rational] The offset of this timezone.
|
157
|
+
def current_offset(rational = false, date = nil)
|
158
|
+
date ||= ::DateTime.now
|
159
|
+
rv = (period_for_utc(date.utc).dst? ? dst_offset : offset)
|
160
|
+
rational ? self.class.rationalize_offset(rv) : rv
|
161
|
+
end
|
162
|
+
|
163
|
+
# Returns the current alias for this timezone.
|
164
|
+
#
|
165
|
+
# @return [String] The current alias or the first alias of the current timezone.
|
166
|
+
def current_alias
|
167
|
+
identifier = tzinfo.identifier
|
168
|
+
|
169
|
+
catch(:alias) do
|
170
|
+
aliases.each do |a|
|
171
|
+
throw(:alias, a) if a == identifier
|
172
|
+
end
|
173
|
+
|
174
|
+
aliases.first
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns the standard offset for this timezone.
|
179
|
+
#
|
180
|
+
# @param rational [Boolean] If to return the offset as a Rational.
|
181
|
+
# @return [Fixnum|Rational] The offset of this timezone.
|
182
|
+
def offset(rational = false)
|
183
|
+
rational ? self.class.rationalize_offset(utc_offset) : utc_offset
|
184
|
+
end
|
185
|
+
|
186
|
+
# Gets a period for this timezone when the Daylight Saving Time (DST) is active (it takes care of different hemispheres).
|
187
|
+
#
|
188
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
189
|
+
# @return [TimezonePeriod] A period when the Daylight Saving Time (DST) is active or `nil` if the timezone doesn't use DST for that year.
|
190
|
+
def dst_period(year = nil)
|
191
|
+
year ||= ::Date.today.year
|
192
|
+
|
193
|
+
northern_summer = ::DateTime.civil(year, 7, 15).utc # This is a representation of a summer period in the Northern Hemisphere.
|
194
|
+
southern_summer = ::DateTime.civil(year, 1, 15).utc # This is a representation of a summer period in the Southern Hemisphere.
|
195
|
+
|
196
|
+
period = period_for_utc(northern_summer)
|
197
|
+
period = period_for_utc(southern_summer) unless period.dst?
|
198
|
+
period.dst? ? period : nil
|
199
|
+
end
|
200
|
+
|
201
|
+
# Checks if the timezone uses Daylight Saving Time (DST) for that date or year.
|
202
|
+
#
|
203
|
+
# @param reference [Object] The date or year to check. Defaults to the current year.
|
204
|
+
# @return [Boolean] `true` if the zone uses DST for that date or year, `false` otherwise.
|
205
|
+
def uses_dst?(reference = nil)
|
206
|
+
if reference.respond_to?(:year) && reference.respond_to?(:utc) # This is a date like object
|
207
|
+
dst_period(reference.year).present? && period_for_utc(reference.utc).dst?
|
208
|
+
else
|
209
|
+
dst_period(reference).present?
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Return the correction applied to the standard offset the timezone when the Daylight Saving Time (DST) is active.
|
214
|
+
#
|
215
|
+
# @param rational [Boolean] If to return the offset as a Rational.
|
216
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
217
|
+
# @return [Fixnum|Rational] The correction for dst.
|
218
|
+
def dst_correction(rational = false, year = nil)
|
219
|
+
dst_offset(rational, year, :std_offset)
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns the standard offset for this timezone timezone when the Daylight Saving Time (DST) is active.
|
223
|
+
#
|
224
|
+
# @param rational [Boolean] If to return the offset as a Rational.
|
225
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
226
|
+
# @param method [Symbol] The method to use for getting the offset. Default is total offset from UTC.
|
227
|
+
# @return [Fixnum|Rational] The DST offset for this timezone or `0`, if the timezone doesn't use DST for that year.
|
228
|
+
def dst_offset(rational = false, year = nil, method = :utc_total_offset)
|
229
|
+
period = dst_period(year)
|
230
|
+
rv = period ? period.send(method) : 0
|
231
|
+
rational ? self.class.rationalize_offset(rv) : rv
|
232
|
+
end
|
233
|
+
|
234
|
+
# Returns the name for this zone with Daylight Saving Time (DST) active.
|
235
|
+
#
|
236
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
237
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
238
|
+
# @param name [String] The name to use for this zone. Defaults to the zone name.
|
239
|
+
# @return [String] The name for the zone with DST or `nil`, if the timezone doesn't use DST for that year.
|
240
|
+
def dst_name(dst_label = nil, year = nil, name = nil)
|
241
|
+
uses_dst?(year) ? "#{name || self.name} #{dst_label || "(DST)"}" : nil
|
242
|
+
end
|
243
|
+
|
244
|
+
# Returns the name for this zone with Daylight Saving Time (DST) active.
|
245
|
+
#
|
246
|
+
# @param name [String] The name to use for this zone. Defaults to the zone name.
|
247
|
+
# @param colon [Boolean] If to put the colon in the output string.
|
248
|
+
# @return [String] The name for this zone.
|
249
|
+
def to_str(name = nil, colon = true)
|
250
|
+
"(GMT#{formatted_offset(colon)}) #{name || current_alias}"
|
251
|
+
end
|
252
|
+
|
253
|
+
# Returns a string representation for this zone with Daylight Saving Time (DST) active.
|
254
|
+
#
|
255
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
256
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
257
|
+
# @param name [String] The name to use for this zone. Defaults to the zone name.
|
258
|
+
# @return [String] The string representation for the zone with DST or `nil`, if the timezone doesn't use DST for that year.
|
259
|
+
def to_str_with_dst(dst_label = nil, year = nil, name = nil)
|
260
|
+
uses_dst?(year) ? "(GMT#{self.class.seconds_to_utc_offset(dst_period(year).utc_total_offset)}) #{name || current_alias} #{dst_label || "(DST)"}" : nil
|
261
|
+
end
|
262
|
+
|
263
|
+
# Returns a parameterized string representation for this zone.
|
264
|
+
#
|
265
|
+
# @param with_offset [Boolean] If to include offset into the representation.
|
266
|
+
# @param name [String] The name to use for this zone. Defaults to the zone name.
|
267
|
+
# @return [String] The parameterized string representation for this zone.
|
268
|
+
def to_str_parameterized(with_offset = true, name = nil)
|
269
|
+
::ActiveSupport::TimeZone.parameterize_zone(name || to_str, with_offset)
|
270
|
+
end
|
271
|
+
|
272
|
+
# Returns a parameterized string representation for this zone with Daylight Saving Time (DST) active.
|
273
|
+
#
|
274
|
+
# @param dst_label [String] Label for the DST indication. Defaults to `(DST)`.
|
275
|
+
# @param year [Fixnum] The year to which refer to. Defaults to the current year.
|
276
|
+
# @param name [String] The name to use for this zone. Defaults to the zone name.
|
277
|
+
# @return [String] The parameterized string representation for this zone with DST or `nil`, if the timezone doesn't use DST for that year.
|
278
|
+
def to_str_with_dst_parameterized(dst_label = nil, year = nil, name = nil)
|
279
|
+
rv = to_str_with_dst(dst_label, year, name)
|
280
|
+
rv ? ::ActiveSupport::TimeZone.parameterize_zone(rv) : nil
|
281
|
+
end
|
282
|
+
|
283
|
+
private
|
284
|
+
|
285
|
+
# Formats a time zone alias.
|
286
|
+
#
|
287
|
+
# @param name [String] The zone name.
|
288
|
+
# @param zone [String] The zone.
|
289
|
+
# @param reference [String] The main name for the zone.
|
290
|
+
# @return [String|nil] The formatted alias.
|
291
|
+
def format_alias(name, zone, reference)
|
292
|
+
if zone.gsub("_", " ") == reference
|
293
|
+
["International Date Line West", "UTC"].include?(name) || name.include?("(US & Canada)") ? name : reference.gsub(/\/.*/, "/#{name}")
|
294
|
+
else
|
295
|
+
nil
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
data/lib/lazier/version.rb
CHANGED
@@ -196,161 +196,4 @@ describe Lazier::DateTime do
|
|
196
196
|
expect(fixed_subject.local_lstrftime(:ct_local_test)).to eq("3 33 6 66 07 2005 01")
|
197
197
|
end
|
198
198
|
end
|
199
|
-
end
|
200
|
-
|
201
|
-
describe Lazier::TimeZone do
|
202
|
-
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
203
|
-
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
|
204
|
-
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
|
205
|
-
|
206
|
-
before(:all) do
|
207
|
-
::Lazier.load!
|
208
|
-
::Lazier::Settings.instance(true)
|
209
|
-
::Lazier::Settings.instance.i18n = :en
|
210
|
-
end
|
211
|
-
|
212
|
-
describe ".rationalize_offset" do
|
213
|
-
it "should return the correct rational value" do
|
214
|
-
expect(::ActiveSupport::TimeZone.rationalize_offset(::ActiveSupport::TimeZone[4])).to eq(Rational(1, 6))
|
215
|
-
expect(::ActiveSupport::TimeZone.rationalize_offset(-25200)).to eq(Rational(-7, 24))
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
describe ".format_offset" do
|
220
|
-
it "should correctly format an offset" do
|
221
|
-
expect(::ActiveSupport::TimeZone.format_offset(-25200)).to eq("-07:00")
|
222
|
-
expect(::ActiveSupport::TimeZone.format_offset(Rational(-4, 24), false)).to eq("-0400")
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
describe ".parameterize_zone" do
|
227
|
-
it "should return the parameterized version of the zone" do
|
228
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
|
229
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
|
230
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false)).to eq(subject_zone.to_str_parameterized(false))
|
231
|
-
expect(::ActiveSupport::TimeZone.parameterize_zone("INVALID")).to eq("invalid")
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
describe ".unparameterize_zone" do
|
236
|
-
it "should return the parameterized version of the zone" do
|
237
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized)).to eq(subject_zone)
|
238
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized, true)).to eq(subject_zone.to_str)
|
239
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized)).to eq(subject_zone)
|
240
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized, true)).to eq(subject_zone.to_str_with_dst)
|
241
|
-
expect(::ActiveSupport::TimeZone.unparameterize_zone("INVALID")).to eq(nil)
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
describe ".find" do
|
246
|
-
it "should find timezones" do
|
247
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)")).to eq(subject_zone)
|
248
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (DST)")).to eq(subject_zone)
|
249
|
-
expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) Daylight Saving Time", "Daylight Saving Time")).to eq(subject_zone)
|
250
|
-
expect(::ActiveSupport::TimeZone.find("INVALID", "INVALID")).to be_nil
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
|
-
describe ".list_all" do
|
255
|
-
it "should list all timezones" do
|
256
|
-
expect(::ActiveSupport::TimeZone.list_all(false)).to eq(::ActiveSupport::TimeZone.all.map(&:to_s))
|
257
|
-
expect(::ActiveSupport::TimeZone.list_all(true)).to include("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
|
258
|
-
expect(::ActiveSupport::TimeZone.list_all(true, "Daylight Saving Time")).to include("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
describe "#offset" do
|
263
|
-
it "should correctly return zone offset" do
|
264
|
-
expect(subject_zone.offset).to eq(subject_zone.utc_offset)
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "#current_offset" do
|
269
|
-
it "should correctly return current zone offset" do
|
270
|
-
expect(subject_zone.current_offset(false, ::DateTime.civil(2012, 1, 15))).to eq(subject_zone.offset)
|
271
|
-
expect(subject_zone.current_offset(true, ::DateTime.civil(2012, 7, 15))).to eq(subject_zone.dst_offset(true))
|
272
|
-
end
|
273
|
-
end
|
274
|
-
|
275
|
-
describe "#current_alias" do
|
276
|
-
it "should correctly return current zone alias or the first one" do
|
277
|
-
zone = ActiveSupport::TimeZone["America/Halifax"]
|
278
|
-
expect(zone.current_alias).to eq("America/Halifax")
|
279
|
-
allow(zone.tzinfo).to receive(:identifier).and_return("INVALID")
|
280
|
-
expect(zone.current_alias).to eq("America/Atlantic Time (Canada)")
|
281
|
-
end
|
282
|
-
end
|
283
|
-
|
284
|
-
describe "#dst_period" do
|
285
|
-
it "should correctly return zone offset" do
|
286
|
-
expect(subject_zone.dst_period).to be_a(::TZInfo::TimezonePeriod)
|
287
|
-
expect(subject_zone.dst_period(1000)).to be_nil
|
288
|
-
expect(zone_without_dst.dst_period).to be_nil
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
describe "#uses_dst?" do
|
293
|
-
it "should correctly detect offset usage" do
|
294
|
-
expect(subject_zone.uses_dst?).to be_true
|
295
|
-
expect(subject_zone.uses_dst?(::DateTime.civil(2012, 7, 15))).to be_true
|
296
|
-
expect(subject_zone.uses_dst?(::DateTime.civil(2012, 1, 15))).to be_false
|
297
|
-
expect(subject_zone.uses_dst?(1000)).to be_false
|
298
|
-
expect(zone_without_dst.uses_dst?).to be_false
|
299
|
-
end
|
300
|
-
end
|
301
|
-
|
302
|
-
describe "#dst_name" do
|
303
|
-
it "should correctly get zone name with Daylight Saving Time" do
|
304
|
-
expect(subject_zone.dst_name).to eq("Mountain Time (US & Canada) (DST)")
|
305
|
-
expect(subject_zone.dst_name("Daylight Saving Time")).to eq("Mountain Time (US & Canada) Daylight Saving Time")
|
306
|
-
expect(subject_zone.dst_name(nil, 1000)).to be_nil
|
307
|
-
expect(zone_without_dst.to_str_with_dst).to be_nil
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
describe "#dst_correction" do
|
312
|
-
it "should correctly detect offset usage" do
|
313
|
-
expect(subject_zone.dst_correction).to eq(3600)
|
314
|
-
expect(subject_zone.dst_correction(true)).to eq(Rational(1, 24))
|
315
|
-
expect(subject_zone.dst_correction(false, 1000)).to eq(0)
|
316
|
-
expect(zone_without_dst.dst_correction).to eq(0)
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
describe "#dst_offset" do
|
321
|
-
it "should correctly return zone offset" do
|
322
|
-
expect(subject_zone.dst_offset).to eq(subject_zone.dst_correction + subject_zone.utc_offset)
|
323
|
-
expect(subject_zone.dst_offset(true)).to eq(::ActiveSupport::TimeZone.rationalize_offset(subject_zone.dst_correction + subject_zone.utc_offset))
|
324
|
-
expect(zone_without_dst.dst_offset(false, 1000)).to eq(0)
|
325
|
-
expect(zone_without_dst.dst_offset).to eq(0)
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
describe "#to_str_with_dst" do
|
330
|
-
it "should correctly format zone with Daylight Saving Time" do
|
331
|
-
expect(subject_zone.to_str_with_dst).to eq("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
|
332
|
-
expect(subject_zone.to_str_with_dst("Daylight Saving Time")).to eq("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
|
333
|
-
expect(subject_zone.to_str_with_dst("Daylight Saving Time", nil, "NAME")).to eq("(GMT-06:00) NAME Daylight Saving Time")
|
334
|
-
expect(subject_zone.to_str_with_dst(nil, 1000)).to be_nil
|
335
|
-
expect(zone_without_dst.to_str_with_dst).to be_nil
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
describe "#to_str_parameterized" do
|
340
|
-
it "should correctly format (parameterized) zone" do
|
341
|
-
expect(subject_zone.to_str_parameterized).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str))
|
342
|
-
expect(subject_zone.to_str_parameterized(false)).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false))
|
343
|
-
expect(subject_zone.to_str_parameterized(false, "NAME SPACE")).to eq(::ActiveSupport::TimeZone.parameterize_zone("NAME SPACE", false))
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
describe "#to_str_with_dst_parameterized" do
|
348
|
-
it "should correctly format (parameterized) zone with Daylight Saving Time" do
|
349
|
-
expect(subject_zone.to_str_with_dst_parameterized).to eq("-0600@america-denver-dst")
|
350
|
-
expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time")).to eq("-0600@america-denver-daylight-saving-time")
|
351
|
-
expect(subject_zone.to_str_with_dst_parameterized(nil, 1000)).to be_nil
|
352
|
-
expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time", nil, "NAME SPACE")).to eq("-0600@name-space-daylight-saving-time")
|
353
|
-
expect(zone_without_dst.to_str_with_dst_parameterized).to be_nil
|
354
|
-
end
|
355
|
-
end
|
356
199
|
end
|