[前][次][番号順一覧][スレッド一覧]

ruby-changes:51814

From: nobu <ko1@a...>
Date: Tue, 24 Jul 2018 16:47:08 +0900 (JST)
Subject: [ruby-changes:51814] nobu:r64028 (trunk): time.rb: yday support

nobu	2018-07-24 16:47:01 +0900 (Tue, 24 Jul 2018)

  New Revision: 64028

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64028

  Log:
    time.rb: yday support
    
    * lib/time.rb (Time.make_time): added yday support.
      [ruby-core:87545] [Bug #14860]

  Modified files:
    trunk/lib/time.rb
    trunk/test/test_time.rb
Index: lib/time.rb
===================================================================
--- lib/time.rb	(revision 64027)
+++ lib/time.rb	(revision 64028)
@@ -245,8 +245,8 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L245
     end
     private :apply_offset
 
-    def make_time(date, year, mon, day, hour, min, sec, sec_fraction, zone, now)
-      if !year && !mon && !day && !hour && !min && !sec && !sec_fraction
+    def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, now)
+      if !year && !yday && !mon && !day && !hour && !min && !sec && !sec_fraction
         raise ArgumentError, "no time information in #{date.inspect}"
       end
 
@@ -256,6 +256,17 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L256
         off = zone_offset(zone, off_year) if zone
       end
 
+      if yday
+        mon, day = (yday-1).divmod(31)
+        mon += 1
+        day += 1
+        t = make_time(date, year, nil, mon, day, hour, min, sec, sec_fraction, zone, now)
+        diff = yday - t.yday
+        return t if diff.zero?
+        day += diff
+        return make_time(date, year, nil, mon, day, hour, min, sec, sec_fraction, zone, now)
+      end
+
       if now
         if off
           now = now.getlocal(off) if now.utc_offset != off
@@ -363,7 +374,7 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L374
       d = Date._parse(date, comp)
       year = d[:year]
       year = yield(year) if year && !comp
-      make_time(date, year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
+      make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
     end
 
     #
@@ -441,7 +452,7 @@ class Time https://github.com/ruby/ruby/blob/trunk/lib/time.rb#L452
       else
         year = d[:year]
         year = yield(year) if year && block_given?
-        t = make_time(date, year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
+        t = make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
       end
       t
     end
Index: test/test_time.rb
===================================================================
--- test/test_time.rb	(revision 64027)
+++ test/test_time.rb	(revision 64028)
@@ -495,6 +495,16 @@ class TestTimeExtension < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/test_time.rb#L495
     assert_equal(true, t.utc?)
   end
 
+  def test_strptime_j
+    t = Time.strptime("2018-365", "%Y-%j")
+    assert_equal(2018, t.year)
+    assert_equal(12, t.mon)
+    assert_equal(31, t.day)
+    assert_equal(0, t.hour)
+    assert_equal(0, t.min)
+    assert_equal(0, t.sec)
+  end
+
   def test_nsec
     assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec)
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]