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

ruby-changes:35215

From: suke <ko1@a...>
Date: Wed, 27 Aug 2014 19:57:42 +0900 (JST)
Subject: [ruby-changes:35215] suke:r47297 (trunk): * ext/win32ole/win32ole.c (vtdate2rbtime): try to convert millisecond

suke	2014-08-27 19:57:28 +0900 (Wed, 27 Aug 2014)

  New Revision: 47297

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

  Log:
    * ext/win32ole/win32ole.c (vtdate2rbtime): try to convert millisecond
      of VT_DATE VARIANT to nsec of Time object.
    * test/win32ole/test_win32ole_variant.rb
      (test_conversion_dbl2date_with_msec): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/ext/win32ole/win32ole.c
    trunk/test/win32ole/test_win32ole_variant.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47296)
+++ ChangeLog	(revision 47297)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Aug 27 19:52:33 2014  Masaki Suketa <masaki.suketa@n...>
+
+	* ext/win32ole/win32ole.c (vtdate2rbtime): try to convert millisecond
+	  of VT_DATE VARIANT to nsec of Time object.
+	* test/win32ole/test_win32ole_variant.rb
+	  (test_conversion_dbl2date_with_msec): ditto.
+
 Wed Aug 27 09:57:29 2014  SHIBATA Hiroshi  <shibata.hiroshi@g...>
 
 	* test/ruby/test_complex.rb: removed unreachable code.
Index: ext/win32ole/win32ole.c
===================================================================
--- ext/win32ole/win32ole.c	(revision 47296)
+++ ext/win32ole/win32ole.c	(revision 47297)
@@ -26,7 +26,7 @@ https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L26
 const IID IID_IMultiLanguage2 = {0xDCCFC164, 0x2B38, 0x11d2, {0xB7, 0xEC, 0x00, 0xC0, 0x4F, 0x8F, 0x5D, 0x9A}};
 #endif
 
-#define WIN32OLE_VERSION "1.7.8"
+#define WIN32OLE_VERSION "1.7.9"
 
 typedef HRESULT (STDAPICALLTYPE FNCOCREATEINSTANCEEX)
     (REFCLSID, IUnknown*, DWORD, COSERVERINFO*, DWORD, MULTI_QI*);
@@ -405,7 +405,7 @@ static double https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L405
 rbtime2vtdate(VALUE tmobj)
 {
     SYSTEMTIME st;
-    double t = 0;
+    double t;
     memset(&st, 0, sizeof(SYSTEMTIME));
     st.wYear = FIX2INT(rb_funcall(tmobj, rb_intern("year"), 0));
     st.wMonth = FIX2INT(rb_funcall(tmobj, rb_intern("month"), 0));
@@ -423,8 +423,8 @@ vtdate2rbtime(double date) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L423
 {
     SYSTEMTIME st;
     VALUE v;
+    double msec;
     VariantTimeToSystemTime(date, &st);
-
     v = rb_funcall(rb_cTime, rb_intern("new"), 6,
 		      INT2FIX(st.wYear),
 		      INT2FIX(st.wMonth),
@@ -432,8 +432,21 @@ vtdate2rbtime(double date) https://github.com/ruby/ruby/blob/trunk/ext/win32ole/win32ole.c#L432
 		      INT2FIX(st.wHour),
 		      INT2FIX(st.wMinute),
 		      INT2FIX(st.wSecond));
-    if (st.wMilliseconds > 0) {
-	return rb_funcall(v, rb_intern("+"), 1, rb_float_new((double)(st.wMilliseconds / 1000.0)));
+    /*
+     * Unfortunately VariantTimeToSystemTime always ignores the
+     * wMilliseconds of SYSTEMTIME struct(The wMilliseconds is 0).
+     * So, we need to calculate milliseconds by ourselves.
+     */
+    msec = fabs(date);
+    msec -= floor(date);
+    msec *= 24 * 60;
+    msec -= floor(msec);
+    msec *= 60;
+    msec -= st.wSecond;
+    msec = round(msec * 1000);
+    msec /= 1000;
+    if (msec != 0) {
+        return rb_funcall(v, rb_intern("+"), 1, rb_float_new(msec));
     }
     return v;
 }
Index: test/win32ole/test_win32ole_variant.rb
===================================================================
--- test/win32ole/test_win32ole_variant.rb	(revision 47296)
+++ test/win32ole/test_win32ole_variant.rb	(revision 47297)
@@ -388,6 +388,14 @@ if defined?(WIN32OLE_VARIANT) https://github.com/ruby/ruby/blob/trunk/test/win32ole/test_win32ole_variant.rb#L388
       assert_equal(dt, obj.value)
     end
 
+    def test_conversion_dbl2date_with_msec
+      # Date is "2014/8/27 12:34:56.789"
+      obj = WIN32OLE_VARIANT.new(41878.524268391200167, WIN32OLE::VARIANT::VT_DATE)
+      t = obj.value
+      assert_equal("2014-08-27 12:34:56", t.strftime('%Y-%m-%d %H:%M:%S'))
+      assert_equal(789, (t.nsec / 1000000).round)
+    end
+
     # this test failed because of VariantTimeToSystemTime
     # and SystemTimeToVariantTime API ignores wMilliseconds
     # member of SYSTEMTIME  struct.

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

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