spec/lazier/timezone_spec.rb in lazier-3.5.7 vs spec/lazier/timezone_spec.rb in lazier-4.0.0
- old
+ new
@@ -1,26 +1,24 @@
-# encoding: utf-8
#
# This file is part of the lazier gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
#
require "spec_helper"
describe Lazier::TimeZone do
let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
+ let(:subject_zone) { ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"] }
let(:zone_without_dst) { ::ActiveSupport::TimeZone["International Date Line West"] }
before(:all) do
::Lazier.load!
- ::Lazier::Settings.instance(true)
- ::Lazier::Settings.instance.i18n = :en
+ ::Time.zone = ::ActiveSupport::TimeZone["Mountain Time (US & Canada)"]
end
describe ".rationalize_offset" do
it "should return the correct rational value" do
- expect(::ActiveSupport::TimeZone.rationalize_offset(::ActiveSupport::TimeZone[4])).to eq(Rational(1, 6))
expect(::ActiveSupport::TimeZone.rationalize_offset(-25200)).to eq(Rational(-7, 24))
end
end
describe ".format_offset" do
@@ -28,100 +26,123 @@
expect(::ActiveSupport::TimeZone.format_offset(-25200)).to eq("-07:00")
expect(::ActiveSupport::TimeZone.format_offset(Rational(-4, 24), false)).to eq("-0400")
end
end
- describe ".parameterize_zone" do
- it "should return the parameterized version of the zone" do
- expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
- expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str)).to eq(subject_zone.to_str_parameterized)
- expect(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false)).to eq(subject_zone.to_str_parameterized(false))
- expect(::ActiveSupport::TimeZone.parameterize_zone("INVALID")).to eq("invalid")
+ describe ".find" do
+ it "should find timezones" do
+ expect(::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)")).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (DST)")).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) Daylight Saving Time", " Daylight Saving Time")).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.find("INVALID", "INVALID")).to be_nil
end
end
- describe ".unparameterize_zone" do
+ describe ".list" do
+ it "should list all timezones as list" do
+ expect(::ActiveSupport::TimeZone.list(false)).to include("(GMT-11:00) Pacific/American Samoa", "(GMT-11:00) International Date Line West")
+ expect(::ActiveSupport::TimeZone.list(false).first).to eq("(GMT+01:00) Africa/Algiers")
+ expect(::ActiveSupport::TimeZone.list(false, sort_by_name: false).first).to eq("(GMT-11:00) Pacific/American Samoa")
+ expect(::ActiveSupport::TimeZone.list(false)).to include("(GMT-11:00) Pacific/American Samoa", "(GMT-11:00) International Date Line West")
+ expect(::ActiveSupport::TimeZone.list(true)).to include("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
+ expect(::ActiveSupport::TimeZone.list(true, dst_label: " Daylight Saving Time")).to include("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
+ expect(::ActiveSupport::TimeZone.list(true, parameterized: true)).to include("-0600@mountain-time-us-canada-dst")
+ end
+
+ it "should list all timezones as hash" do
+ expect(::ActiveSupport::TimeZone.list(true, sort_by_name: true, as_hash: true)["(GMT-06:00) #{subject_zone.aliases.first} (DST)"]).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.list(true, sort_by_name: true, as_hash: true).keys.first).to eq("(GMT+01:00) Africa/Algiers")
+ expect(::ActiveSupport::TimeZone.list(true, sort_by_name: false, as_hash: true).keys.first).to eq("(GMT-11:00) Pacific/American Samoa")
+ expect(::ActiveSupport::TimeZone.list(true, as_hash: true, parameterized: true)).to include("-0700@mountain-time-us-canada")
+
+ end
+ end
+
+ describe ".parameterize" do
it "should return the parameterized version of the zone" do
- expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized)).to eq(subject_zone)
- expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_parameterized, true)).to eq(subject_zone.to_str)
- expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized)).to eq(subject_zone)
- expect(::ActiveSupport::TimeZone.unparameterize_zone(subject_zone.to_str_with_dst_parameterized, true)).to eq(subject_zone.to_str_with_dst)
- expect(::ActiveSupport::TimeZone.unparameterize_zone("INVALID")).to eq(nil)
+ expect(::ActiveSupport::TimeZone.parameterize(subject_zone)).to eq("-0700@mountain-time-us-canada")
+ expect(::ActiveSupport::TimeZone.parameterize(subject_zone.to_str)).to eq("-0700@mountain-time-us-canada")
+ expect(::ActiveSupport::TimeZone.parameterize(subject_zone, false)).to eq("mountain-time-us-canada")
+ expect(::ActiveSupport::TimeZone.parameterize("INVALID")).to eq("invalid")
+ expect(::ActiveSupport::TimeZone.parameterize("-0700@mountain-time-us-canada")).to eq("-0700@mountain-time-us-canada")
end
end
- describe ".find" do
- it "should find timezones" do
- expect(::ActiveSupport::TimeZone.find("(GMT-07:00) Mountain Time (US & Canada)")).to eq(subject_zone)
- expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) (DST)")).to eq(subject_zone)
- expect(::ActiveSupport::TimeZone.find("(GMT-06:00) Mountain Time (US & Canada) Daylight Saving Time", "Daylight Saving Time")).to eq(subject_zone)
- expect(::ActiveSupport::TimeZone.find("INVALID", "INVALID")).to be_nil
+ describe ".unparameterize" do
+ it "should return the parameterized version of the zone" do
+ expect(::ActiveSupport::TimeZone.unparameterize("-0700@mountain-time-us-canada")).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.unparameterize("mountain-time-us-canada")).to be_nil
+ expect(::ActiveSupport::TimeZone.unparameterize("-0600@mountain-time-us-canada-day", " DAY")).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.unparameterize(subject_zone.to_str)).to eq(subject_zone)
+ expect(::ActiveSupport::TimeZone.unparameterize("INVALID")).to eq(nil)
end
end
- describe ".list_all" do
- it "should list all timezones" do
- expect(::ActiveSupport::TimeZone.list_all(false)).to eq(::ActiveSupport::TimeZone.all.map(&:to_s))
- expect(::ActiveSupport::TimeZone.list_all(true)).to include("(GMT-06:00) #{subject_zone.aliases.first} (DST)")
- expect(::ActiveSupport::TimeZone.list_all(true, "Daylight Saving Time")).to include("(GMT-06:00) #{subject_zone.aliases.first} Daylight Saving Time")
+ describe "#compare" do
+ it "should correctly compare timezones" do
+ expect(::ActiveSupport::TimeZone.compare(::ActiveSupport::TimeZone["Africa/Algiers"], ::ActiveSupport::TimeZone["International Date Line West"])).to eq(-1)
+ expect(::ActiveSupport::TimeZone.compare(::ActiveSupport::TimeZone["Africa/Algiers"], ::ActiveSupport::TimeZone["Africa/Algiers"])).to eq(0)
+ expect(::ActiveSupport::TimeZone.compare(::ActiveSupport::TimeZone["Europe/Madrid"], ::ActiveSupport::TimeZone["Europe/Copenhagen"])).to eq(1)
+ expect(::ActiveSupport::TimeZone.compare("(GMT+01:00) Europe/Rome", "(GMT+02:00) Europe/Kiev")).to eq(1)
end
end
- describe "#offset" do
- it "should correctly return zone offset" do
- expect(subject_zone.offset).to eq(subject_zone.utc_offset)
+ describe "#aliases" do
+ it "should return the right list of aliases" do
+ expect(ActiveSupport::TimeZone["America/Los_Angeles"].aliases).to eq(["America/Los Angeles", "Pacific Time (US & Canada)"])
end
end
describe "#current_offset" do
it "should correctly return current zone offset" do
- expect(subject_zone.current_offset(false, ::DateTime.civil(2012, 1, 15))).to eq(subject_zone.offset)
- expect(subject_zone.current_offset(true, ::DateTime.civil(2012, 7, 15))).to eq(subject_zone.dst_offset(true))
+ expect(subject_zone.current_offset(false, ::DateTime.civil(2012, 1, 15))).to eq(-25200)
+ expect(subject_zone.current_offset(true, ::DateTime.civil(2012, 7, 15))).to eq(Rational(-1, 4))
end
end
describe "#current_alias" do
it "should correctly return current zone alias or the first one" do
zone = ActiveSupport::TimeZone["America/Halifax"]
expect(zone.current_alias).to eq("America/Halifax")
- allow(zone.tzinfo).to receive(:identifier).and_return("INVALID")
+ allow(zone).to receive(:name).and_return("INVALID")
expect(zone.current_alias).to eq("America/Atlantic Time (Canada)")
end
end
- describe "#current_alias=" do
- it "should set the current alias alias" do
- zone = ActiveSupport::TimeZone["America/Halifax"]
- zone.current_alias = "ALIAS"
- expect(zone.current_alias).to eq("ALIAS")
+ describe "#current_name" do
+ it "should correctly get zone name with Daylight Saving Time" do
+ expect(subject_zone.current_name).to eq("Mountain Time (US & Canada)")
+ expect(subject_zone.current_name(true)).to eq("Mountain Time (US & Canada) (DST)")
+ expect(subject_zone.current_name(true, dst_label: "-dst")).to eq("Mountain Time (US & Canada)-dst")
+ expect(subject_zone.current_name(true, year: 1000)).to eq("Mountain Time (US & Canada)")
end
end
- describe "#dst_period" do
+ describe "#offset" do
it "should correctly return zone offset" do
- expect(subject_zone.dst_period).to be_a(::TZInfo::TimezonePeriod)
- expect(subject_zone.dst_period(1000)).to be_nil
- expect(zone_without_dst.dst_period).to be_nil
+ expect(subject_zone.offset).to eq(subject_zone.utc_offset)
end
end
describe "#uses_dst?" do
it "should correctly detect offset usage" do
- expect(subject_zone.uses_dst?).to be_true
- expect(subject_zone.uses_dst?(::DateTime.civil(2012, 7, 15))).to be_true
- expect(subject_zone.uses_dst?(::DateTime.civil(2012, 1, 15))).to be_false
- expect(subject_zone.uses_dst?(1000)).to be_false
- expect(zone_without_dst.uses_dst?).to be_false
+ expect(subject_zone.uses_dst?).to be_truthy
+ expect(subject_zone.uses_dst?(::DateTime.civil(2012, 7, 15))).to be_truthy
+ expect(subject_zone.uses_dst?(::DateTime.civil(2012, 1, 15))).to be_falsey
+ expect(subject_zone.uses_dst?(1000)).to be_falsey
+ expect(zone_without_dst.uses_dst?).to be_falsey
end
end
- describe "#dst_name" do
- it "should correctly get zone name with Daylight Saving Time" do
- expect(subject_zone.dst_name).to eq("Mountain Time (US & Canada) (DST)")
- expect(subject_zone.dst_name("Daylight Saving Time")).to eq("Mountain Time (US & Canada) Daylight Saving Time")
- expect(subject_zone.dst_name(nil, 1000)).to be_nil
- expect(zone_without_dst.to_str_with_dst).to be_nil
+ describe "#dst_period" do
+ it "should correctly return zone offset" do
+ expect(subject_zone.dst_period).to be_a(::TZInfo::TimezonePeriod)
+ expect(subject_zone.dst_period(1000)).to be_nil
+ expect(zone_without_dst.dst_period).to be_nil
+
+ expect(zone_without_dst).to receive(:period_for_utc).and_raise(RuntimeError)
+ expect(zone_without_dst.dst_period).to be_nil
end
end
describe "#dst_correction" do
it "should correctly detect offset usage" do
@@ -130,42 +151,35 @@
expect(subject_zone.dst_correction(false, 1000)).to eq(0)
expect(zone_without_dst.dst_correction).to eq(0)
end
end
- describe "#dst_offset" do
- it "should correctly return zone offset" do
- expect(subject_zone.dst_offset).to eq(subject_zone.dst_correction + subject_zone.utc_offset)
- expect(subject_zone.dst_offset(true)).to eq(::ActiveSupport::TimeZone.rationalize_offset(subject_zone.dst_correction + subject_zone.utc_offset))
- expect(zone_without_dst.dst_offset(false, 1000)).to eq(0)
- expect(zone_without_dst.dst_offset).to eq(0)
+ describe "#to_str" do
+ describe "parameterized" do
+ it "should correctly parameterize the zone" do
+ expect(subject_zone.to_str(parameterized: true, label: "FOO")).to eq("-0700@foo")
+ expect(subject_zone.to_str(parameterized: true)).to eq("-0700@mountain-time-us-canada")
+ expect(subject_zone.to_str(true, parameterized: true)).to eq("-0600@mountain-time-us-canada-dst")
+ expect(subject_zone.to_str(true, parameterized: true, dst_label: "-DAY")).to eq("-0600@mountain-time-us-canada-day")
+ expect(subject_zone.to_str(true, parameterized: true, utc_label: "UTC")).to eq("-0600@mountain-time-us-canada-dst")
+ expect(subject_zone.to_str(parameterized: true, year: 1000)).to eq("-0700@mountain-time-us-canada")
+ expect(subject_zone.to_str(parameterized: true, with_offset: false)).to eq("mountain-time-us-canada")
+ end
end
- end
- describe "#to_str_with_dst" do
- it "should correctly format zone with Daylight Saving Time" do
- expect(subject_zone.to_str_with_dst).to eq("(GMT-06:00) #{subject_zone.current_alias} (DST)")
- expect(subject_zone.to_str_with_dst("Daylight Saving Time")).to eq("(GMT-06:00) #{subject_zone.current_alias} Daylight Saving Time")
- expect(subject_zone.to_str_with_dst("Daylight Saving Time", nil, "NAME")).to eq("(GMT-06:00) NAME Daylight Saving Time")
- expect(subject_zone.to_str_with_dst(nil, 1000)).to be_nil
- expect(zone_without_dst.to_str_with_dst).to be_nil
- end
- end
-
- describe "#to_str_parameterized" do
- it "should correctly format (parameterized) zone" do
- expect(subject_zone.to_str_parameterized).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str))
- expect(subject_zone.to_str_parameterized(false)).to eq(::ActiveSupport::TimeZone.parameterize_zone(subject_zone.to_str, false))
- expect(subject_zone.to_str_parameterized(false, "NAME SPACE")).to eq(::ActiveSupport::TimeZone.parameterize_zone("NAME SPACE", false))
- end
- end
-
- describe "#to_str_with_dst_parameterized" do
- it "should correctly format (parameterized) zone with Daylight Saving Time" do
- expect(subject_zone.to_str_with_dst_parameterized).to eq("-0600@mountain-time-us-canada-dst")
- expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time")).to eq("-0600@mountain-time-us-canada-daylight-saving-time")
- expect(subject_zone.to_str_with_dst_parameterized(nil, 1000)).to be_nil
- expect(subject_zone.to_str_with_dst_parameterized("Daylight Saving Time", nil, "NAME SPACE")).to eq("-0600@name-space-daylight-saving-time")
- expect(zone_without_dst.to_str_with_dst_parameterized).to be_nil
+ describe "not parameterized" do
+ it "should correctly parameterize the zone" do
+ expect(subject_zone.to_str(label: "FOO")).to eq("(GMT-07:00) FOO")
+ expect(subject_zone.to_str).to eq("(GMT-07:00) Mountain Time (US & Canada)")
+ expect(subject_zone.to_str(true)).to eq("(GMT-06:00) Mountain Time (US & Canada) (DST)")
+ expect(subject_zone.to_str(true, dst_label: "-DAY")).to eq("(GMT-06:00) Mountain Time (US & Canada)-DAY")
+ expect(subject_zone.to_str(true, utc_label: "UTC")).to eq("(UTC-06:00) Mountain Time (US & Canada) (DST)")
+ expect(subject_zone.to_str(year: 1000)).to eq("(GMT-07:00) Mountain Time (US & Canada)")
+ expect(subject_zone.to_str(with_offset: false)).to eq("Mountain Time (US & Canada)")
+ expect(subject_zone.to_str(offset_position: :end)).to eq("Mountain Time (US & Canada) (GMT-07:00)")
+ expect(subject_zone.to_str(offset_position: :other)).to eq("(GMT-07:00) Mountain Time (US & Canada)")
+ expect(subject_zone.to_str(colon: false)).to eq("(GMT-0700) Mountain Time (US & Canada)")
+ expect(subject_zone.to_str).to eq("(GMT-07:00) Mountain Time (US & Canada)")
+ end
end
end
end
\ No newline at end of file