ext/win32ole/win32ole.c (rbtime2vtdate, vtdate2rbtime): fix

the bug in conversion of milliseconds. [Bug #10258]

test/win32ole/test_win32ole_variant.rb
(test_conversion_dbl2date_with_msec,
test_conversion_time2date_with_msec): use assert_in_delta instead
of assert_equal to treat an acceptable error range.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47658 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
suke 2014-09-21 02:18:37 +00:00
Родитель e41fde8b77
Коммит 8bb93fc8ae
3 изменённых файлов: 28 добавлений и 11 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Sun Sep 21 11:16:56 2014 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c (rbtime2vtdate, vtdate2rbtime): fix
the bug in conversion of milliseconds. [Bug #10258]
* test/win32ole/test_win32ole_variant.rb
(test_conversion_dbl2date_with_msec,
test_conversion_time2date_with_msec): use assert_in_delta instead
of assert_equal to treat an acceptable error range.
Sun Sep 21 11:03:32 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* signal.c (ruby_signal): although "EINVAL from sigaction(2) is

Просмотреть файл

@ -26,7 +26,7 @@
const IID IID_IMultiLanguage2 = {0xDCCFC164, 0x2B38, 0x11d2, {0xB7, 0xEC, 0x00, 0xC0, 0x4F, 0x8F, 0x5D, 0x9A}};
#endif
#define WIN32OLE_VERSION "1.8.2"
#define WIN32OLE_VERSION "1.8.3"
typedef HRESULT (STDAPICALLTYPE FNCOCREATEINSTANCEEX)
(REFCLSID, IUnknown*, DWORD, COSERVERINFO*, DWORD, MULTI_QI*);
@ -408,14 +408,13 @@ rbtime2vtdate(VALUE tmobj)
double t;
double nsec;
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));
st.wDay = FIX2INT(rb_funcall(tmobj, rb_intern("mday"), 0));
st.wHour = FIX2INT(rb_funcall(tmobj, rb_intern("hour"), 0));
st.wMinute = FIX2INT(rb_funcall(tmobj, rb_intern("min"), 0));
st.wSecond = FIX2INT(rb_funcall(tmobj, rb_intern("sec"), 0));
st.wMilliseconds = FIX2INT(rb_funcall(tmobj, rb_intern("nsec"), 0)) / 1000000;
st.wMilliseconds = 0;
SystemTimeToVariantTime(&st, &t);
/*
@ -436,6 +435,7 @@ vtdate2rbtime(double date)
SYSTEMTIME st;
VALUE v;
double msec;
double sec;
VariantTimeToSystemTime(date, &st);
v = rb_funcall(rb_cTime, rb_intern("new"), 6,
INT2FIX(st.wYear),
@ -444,19 +444,26 @@ vtdate2rbtime(double date)
INT2FIX(st.wHour),
INT2FIX(st.wMinute),
INT2FIX(st.wSecond));
st.wYear = FIX2INT(rb_funcall(v, rb_intern("year"), 0));
st.wMonth = FIX2INT(rb_funcall(v, rb_intern("month"), 0));
st.wDay = FIX2INT(rb_funcall(v, rb_intern("mday"), 0));
st.wHour = FIX2INT(rb_funcall(v, rb_intern("hour"), 0));
st.wMinute = FIX2INT(rb_funcall(v, rb_intern("min"), 0));
st.wSecond = FIX2INT(rb_funcall(v, rb_intern("sec"), 0));
st.wMilliseconds = 0;
SystemTimeToVariantTime(&st, &sec);
/*
* 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 = date - sec;
msec *= 24 * 60;
msec -= floor(msec);
msec *= 60;
msec -= st.wSecond;
msec = round(msec * 1000);
msec /= 1000;
if (msec >= 59) {
msec -= 60;
}
if (msec != 0) {
return rb_funcall(v, rb_intern("+"), 1, rb_float_new(msec));
}

Просмотреть файл

@ -393,7 +393,7 @@ if defined?(WIN32OLE_VARIANT)
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)
assert_in_delta(0.789, t.nsec / 1000000000.0, 0.001)
end
def test_conversion_time2date_with_msec
@ -401,12 +401,12 @@ if defined?(WIN32OLE_VARIANT)
t0 += 0.789
t1 = WIN32OLE_VARIANT.new(t0).value
assert_equal("2014-08-27 12:34:56", t1.strftime('%Y-%m-%d %H:%M:%S'))
assert_equal(789, (t1.nsec / 1000000).round)
assert_in_delta(0.789, t1.nsec / 1000000000.0, 0.001)
t0 = Time.now
t1 = WIN32OLE_VARIANT.new(t0).value
assert_equal(t0.strftime('%Y-%m-%d %H:%M:%S'), t1.strftime('%Y-%m-%d %H:%M:%S'))
assert_equal(t0.nsec.round(-6), t1.nsec.round(-6))
assert_in_delta(t0.nsec/1000000000.0, t1.nsec / 1000000000.0, 0.001)
end
# this test failed because of VariantTimeToSystemTime