1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/now'
require_relative 'shared/local'
require_relative 'shared/time_params'
describe "Time.new" do
it_behaves_like :time_now, :new
end
describe "Time.new" do
it_behaves_like :time_local, :new
it_behaves_like :time_params, :new
end
describe "Time.new with a utc_offset argument" do
it "returns a non-UTC time" do
Time.new(2000, 1, 1, 0, 0, 0, 0).utc?.should == false
end
it "returns a Time with a UTC offset of the specified number of Integer seconds" do
Time.new(2000, 1, 1, 0, 0, 0, 123).utc_offset.should == 123
end
describe "with an argument that responds to #to_int" do
it "coerces using #to_int" do
o = mock('integer')
o.should_receive(:to_int).and_return(123)
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 123
end
end
it "returns a Time with a UTC offset of the specified number of Rational seconds" do
Time.new(2000, 1, 1, 0, 0, 0, Rational(5, 2)).utc_offset.should eql(Rational(5, 2))
end
describe "with an argument that responds to #to_r" do
it "coerces using #to_r" do
o = mock_numeric('rational')
o.should_receive(:to_r).and_return(Rational(5, 2))
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should eql(Rational(5, 2))
end
end
it "returns a Time with a UTC offset specified as +HH:MM" do
Time.new(2000, 1, 1, 0, 0, 0, "+05:30").utc_offset.should == 19800
end
it "returns a Time with a UTC offset specified as -HH:MM" do
Time.new(2000, 1, 1, 0, 0, 0, "-04:10").utc_offset.should == -15000
end
it "returns a Time with a UTC offset specified as +HH:MM:SS" do
Time.new(2000, 1, 1, 0, 0, 0, "+05:30:37").utc_offset.should == 19837
end
it "returns a Time with a UTC offset specified as -HH:MM" do
Time.new(2000, 1, 1, 0, 0, 0, "-04:10:43").utc_offset.should == -15043
end
describe "with an argument that responds to #to_str" do
it "coerces using #to_str" do
o = mock('string')
o.should_receive(:to_str).and_return("+05:30")
Time.new(2000, 1, 1, 0, 0, 0, o).utc_offset.should == 19800
end
end
it "returns a local Time if the argument is nil" do
with_timezone("PST", -8) do
t = Time.new(2000, 1, 1, 0, 0, 0, nil)
t.utc_offset.should == -28800
t.zone.should == "PST"
end
end
# [Bug #8679], r47676
it "disallows a value for minutes greater than 59" do
-> {
Time.new(2000, 1, 1, 0, 0, 0, "+01:60")
}.should raise_error(ArgumentError)
-> {
Time.new(2000, 1, 1, 0, 0, 0, "+01:99")
}.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not of the form (+|-)HH:MM" do
-> { Time.new(2000, 1, 1, 0, 0, 0, "3600") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the hour value is greater than 23" do
-> { Time.new(2000, 1, 1, 0, 0, 0, "+24:00") }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the String argument is not in an ASCII-compatible encoding" do
-> { Time.new(2000, 1, 1, 0, 0, 0, "-04:10".encode("UTF-16LE")) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value less than or equal to -86400 seconds" do
Time.new(2000, 1, 1, 0, 0, 0, -86400 + 1).utc_offset.should == (-86400 + 1)
-> { Time.new(2000, 1, 1, 0, 0, 0, -86400) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the argument represents a value greater than or equal to 86400 seconds" do
Time.new(2000, 1, 1, 0, 0, 0, 86400 - 1).utc_offset.should == (86400 - 1)
-> { Time.new(2000, 1, 1, 0, 0, 0, 86400) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the seconds argument is negative" do
-> { Time.new(2000, 1, 1, 0, 0, -1) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the utc_offset argument is greater than or equal to 10e9" do
-> { Time.new(2000, 1, 1, 0, 0, 0, 1000000000) }.should raise_error(ArgumentError)
end
it "raises ArgumentError if the month is greater than 12" do
# For some reason MRI uses a different message for month in 13-15 and month>=16
-> { Time.new(2000, 13, 1, 0, 0, 0, "+01:00") }.should raise_error(ArgumentError, /(mon|argument) out of range/)
-> { Time.new(2000, 16, 1, 0, 0, 0, "+01:00") }.should raise_error(ArgumentError, "argument out of range")
end
end
ruby_version_is "2.7" do
describe "Time.new with a timezone argument" do
it "returns a Time in the timezone" do
zone = TimeSpecs::Timezone.new(offset: (5*3600+30*60))
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
time.zone.should == zone
time.utc_offset.should == 5*3600+30*60
ruby_version_is "2.7" do
time.wday.should == 6
time.yday.should == 1
end
end
it "accepts timezone argument that must have #local_to_utc and #utc_to_local methods" do
zone = Object.new
def zone.utc_to_local(time)
time
end
def zone.local_to_utc(time)
time
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time)
}.should_not raise_error
end
it "raises TypeError if timezone does not implement #local_to_utc method" do
zone = Object.new
def zone.utc_to_local(time)
time
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone)
}.should raise_error(TypeError, /can't convert \w+ into an exact number/)
end
it "does not raise exception if timezone does not implement #utc_to_local method" do
zone = Object.new
def zone.local_to_utc(time)
time
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time)
}.should_not raise_error
end
# The result also should be a Time or Time-like object (not necessary to be the same class)
# The zone of the result is just ignored
describe "returned value by #utc_to_local and #local_to_utc methods" do
it "could be Time instance" do
zone = Object.new
def zone.local_to_utc(t)
Time.utc(t.year, t.mon, t.day, t.hour - 1, t.min, t.sec)
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time)
Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60
}.should_not raise_error
end
it "could be Time subclass instance" do
zone = Object.new
def zone.local_to_utc(t)
Class.new(Time).utc(t.year, t.mon, t.day, t.hour - 1, t.min, t.sec)
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time)
Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60
}.should_not raise_error
end
it "could be any object with #to_i method" do
zone = Object.new
def zone.local_to_utc(time)
Struct.new(:to_i).new(time.to_i - 60*60)
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone).should be_kind_of(Time)
Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 60*60
}.should_not raise_error
end
it "could have any #zone and #utc_offset because they are ignored" do
zone = Object.new
def zone.local_to_utc(time)
Struct.new(:to_i, :zone, :utc_offset).new(time.to_i, 'America/New_York', -5*60*60)
end
Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 0
zone = Object.new
def zone.local_to_utc(time)
Struct.new(:to_i, :zone, :utc_offset).new(time.to_i, 'Asia/Tokyo', 9*60*60)
end
Time.new(2000, 1, 1, 12, 0, 0, zone).utc_offset.should == 0
end
it "leads to raising Argument error if difference between argument and result is too large" do
zone = Object.new
def zone.local_to_utc(t)
Time.utc(t.year, t.mon, t.day + 1, t.hour, t.min, t.sec)
end
-> {
Time.new(2000, 1, 1, 12, 0, 0, zone)
}.should raise_error(ArgumentError, "utc_offset out of range")
end
end
# https://github.com/ruby/ruby/blob/v2_6_0/time.c#L5330
#
# Time-like argument to these methods is similar to a Time object in UTC without sub-second;
# it has attribute readers for the parts, e.g. year, month, and so on, and epoch time readers, to_i
#
# The sub-second attributes are fixed as 0, and utc_offset, zone, isdst, and their aliases are same as a Time object in UTC
describe "Time-like argument of #utc_to_local and #local_to_utc methods" do
before do
@obj = TimeSpecs::TimeLikeArgumentRecorder.result
@obj.should_not == nil
end
it "implements subset of Time methods" do
[
:year, :mon, :month, :mday, :hour, :min, :sec,
:tv_sec, :tv_usec, :usec, :tv_nsec, :nsec, :subsec,
:to_i, :to_f, :to_r, :+, :-,
:isdst, :dst?, :zone, :gmtoff, :gmt_offset, :utc_offset, :utc?, :gmt?,
:to_s, :inspect, :to_a, :to_time,
].each do |name|
@obj.respond_to?(name).should == true
end
end
it "has attribute values the same as a Time object in UTC" do
@obj.usec.should == 0
@obj.nsec.should == 0
@obj.subsec.should == 0
@obj.tv_usec.should == 0
@obj.tv_nsec.should == 0
@obj.utc_offset.should == 0
@obj.zone.should == "UTC"
@obj.isdst.should == Time.new.utc.isdst
end
end
context "#name method" do
it "uses the optional #name method for marshaling" do
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
time_loaded = Marshal.load(Marshal.dump(time))
time_loaded.zone.should == "Asia/Colombo"
time_loaded.utc_offset.should == 5*3600+30*60
end
it "cannot marshal Time if #name method isn't implemented" do
zone = TimeSpecs::Timezone.new(offset: (5*3600+30*60))
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
-> {
Marshal.dump(time)
}.should raise_error(NoMethodError, /undefined method `name' for/)
end
end
it "the #abbr method is used by '%Z' in #strftime" do
zone = TimeSpecs::TimezoneWithAbbr.new(name: "Asia/Colombo")
time = Time.new(2000, 1, 1, 12, 0, 0, zone)
time.strftime("%Z").should == "MMT"
end
# At loading marshaled data, a timezone name will be converted to a timezone object
# by find_timezone class method, if the method is defined.
# Similarly, that class method will be called when a timezone argument does not have
# the necessary methods mentioned above.
context "subject's class implements .find_timezone method" do
it "calls .find_timezone to build a time object at loading marshaled data" do
zone = TimeSpecs::TimezoneWithName.new(name: "Asia/Colombo")
time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone)
time_loaded = Marshal.load(Marshal.dump(time))
time_loaded.zone.should be_kind_of TimeSpecs::TimezoneWithName
time_loaded.zone.name.should == "Asia/Colombo"
time_loaded.utc_offset.should == 5*3600+30*60
end
it "calls .find_timezone to build a time object if passed zone name as a timezone argument" do
time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, "Asia/Colombo")
time.zone.should be_kind_of TimeSpecs::TimezoneWithName
time.zone.name.should == "Asia/Colombo"
time = TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, "some invalid zone name")
time.zone.should be_kind_of TimeSpecs::TimezoneWithName
time.zone.name.should == "some invalid zone name"
end
it "does not call .find_timezone if passed any not string/numeric/timezone timezone argument" do
[Object.new, [], {}, :"some zone"].each do |zone|
-> {
TimeSpecs::TimeWithFindTimezone.new(2000, 1, 1, 12, 0, 0, zone)
}.should raise_error(TypeError, /can't convert \w+ into an exact number/)
end
end
end
end
end
|