Project

General

Profile

Feature #9549 ยป 540.patch

sferik (Erik Michaels-Ober), 02/21/2014 09:05 PM

View differences:

lib/time.rb
389 389
    # %Z :: Time zone name
390 390
    # %% :: Literal "%" character
391 391

  
392
    def strptime(date, format, now=self.now)
392
    def strptime(date='-4712-01-01', format='%F', now=self.now)
393 393
      d = Date._strptime(date, format)
394 394
      raise ArgumentError, "invalid strptime format - `#{format}'" unless d
395 395
      if seconds = d[:seconds]
test/test_time.rb
402 402
    assert_equal(Time.at(1).localtime, Time.strptime("1", "%s"))
403 403
    assert_equal(false, Time.strptime('0', '%s').utc?)
404 404
    assert_equal(3600, Time.strptime('0 +0100', '%s %z').utc_offset)
405
    assert_equal(Date.strptime.to_time, Time.strptime)
405 406
  end
406 407

  
407 408
  def test_nsec
408
- 
lib/time.rb
389 389
    # %Z :: Time zone name
390 390
    # %% :: Literal "%" character
391 391

  
392
    def strptime(date='-4712-01-01', format='%F', now=self.now)
393
      d = Date._strptime(date, format)
394
      raise ArgumentError, "invalid strptime format - `#{format}'" unless d
395
      if seconds = d[:seconds]
396
        if offset = d[:offset]
392
    def strptime(time='-4712-01-01', format='%F', now=self.now)
393
      date = Date.strptime(time, format) # will raise an ArgumentError if the time is invalid
394
      hash = Date._strptime(time, format)
395
      raise ArgumentError, "invalid strptime format - `#{format}'" unless hash
396
      if seconds = hash[:seconds]
397
        if offset = hash[:offset]
397 398
          Time.at(seconds).localtime(offset)
398 399
        else
399 400
          Time.at(seconds)
400 401
        end
401 402
      else
402
        year = d[:year]
403
        year = date.year
403 404
        year = yield(year) if year && block_given?
404
        make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
405
        make_time(year, date.mon, date.mday, hash[:hour], hash[:min], hash[:sec], hash[:sec_fraction], hash[:zone], now)
405 406
      end
406 407
    end
407 408

  
test/test_time.rb
403 403
    assert_equal(false, Time.strptime('0', '%s').utc?)
404 404
    assert_equal(3600, Time.strptime('0 +0100', '%s %z').utc_offset)
405 405
    assert_equal(Date.strptime.to_time, Time.strptime)
406
    assert_raise(ArgumentError) {
407
      Time.strptime('31/2/2014', '%d/%m/%Y')
408
    }
406 409
  end
407 410

  
408 411
  def test_nsec
409
- 
lib/time.rb
320 320
    #
321 321
    # You must require 'time' to use this method.
322 322
    #
323
    def parse(date, now=self.now)
323
    def parse(time, now=self.now)
324 324
      comp = !block_given?
325
      d = Date._parse(date, comp)
326
      if !d[:year] && !d[:mon] && !d[:mday] && !d[:hour] && !d[:min] && !d[:sec] && !d[:sec_fraction]
325
      hash = Date._parse(time, comp)
326
      if !hash[:year] && !hash[:mon] && !hash[:mday] && !hash[:hour] && !hash[:min] && !hash[:sec] && !hash[:sec_fraction]
327 327
        raise ArgumentError, "no time information in #{date.inspect}"
328 328
      end
329
      year = d[:year]
329
      year = hash[:year]
330 330
      year = yield(year) if year && !comp
331
      make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
331
      make_time(year, hash[:mon], hash[:mday], hash[:hour], hash[:min], hash[:sec], hash[:sec_fraction], hash[:zone], now)
332 332
    end
333 333

  
334 334
    #
335
-