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

ruby-changes:20246

From: tadf <ko1@a...>
Date: Wed, 29 Jun 2011 23:47:12 +0900 (JST)
Subject: [ruby-changes:20246] tadf:r32294 (trunk): * ext/date/date_core.c: avoided using timev.

tadf	2011-06-29 23:47:04 +0900 (Wed, 29 Jun 2011)

  New Revision: 32294

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

  Log:
    * ext/date/date_core.c: avoided using timev.
    * ext/date/date_strftime.c: ditto.
    * ext/date/date_tmx.h: ditto.

  Modified files:
    trunk/ChangeLog
    trunk/ext/date/date_core.c
    trunk/ext/date/date_strftime.c
    trunk/ext/date/date_tmx.h
    trunk/test/date/test_date_strftime.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32293)
+++ ChangeLog	(revision 32294)
@@ -1,3 +1,9 @@
+Wed Jun 29 23:42:51 2011  Tadayoshi Funaba  <tadf@d...>
+
+	* ext/date/date_core.c: avoided using timev.
+	* ext/date/date_strftime.c: ditto.
+	* ext/date/date_tmx.h: ditto.
+
 Wed Jun 29 23:17:57 2011  WATANABE Hirofumi  <eban@r...>
 
 	* ext/openssl/ossl.h (OPENSSL_SYS_WIN32): support for mingw(msys).
Index: ext/date/date_core.c
===================================================================
--- ext/date/date_core.c	(revision 32293)
+++ ext/date/date_core.c	(revision 32294)
@@ -172,6 +172,7 @@
 #define MINUTE_IN_SECONDS 60
 #define HOUR_IN_SECONDS 3600
 #define DAY_IN_SECONDS 86400
+#define SECOND_IN_MILLISECONDS 1000
 #define SECOND_IN_NANOSECONDS 1000000000
 
 #define JC_PERIOD0 1461		/* 365.25 * 4 */
@@ -932,7 +933,15 @@
     return f_quo(n, day_in_nanoseconds);
 }
 
+#ifndef NDEBUG
 static VALUE
+ms_to_sec(VALUE n)
+{
+    return f_quo(n, INT2FIX(SECOND_IN_MILLISECONDS));
+}
+#endif
+
+static VALUE
 ns_to_sec(VALUE n)
 {
     return f_quo(n, INT2FIX(SECOND_IN_NANOSECONDS));
@@ -976,6 +985,14 @@
 #endif
 
 static VALUE
+sec_to_ms(VALUE s)
+{
+    if (safe_mul_p(s, SECOND_IN_MILLISECONDS))
+	return LONG2FIX(FIX2LONG(s) * SECOND_IN_MILLISECONDS);
+    return f_mul(s, INT2FIX(SECOND_IN_MILLISECONDS));
+}
+
+static VALUE
 sec_to_ns(VALUE s)
 {
     if (safe_mul_p(s, SECOND_IN_NANOSECONDS))
@@ -6596,6 +6613,38 @@
 }
 
 static VALUE
+tmx_m_secs(union DateData *x)
+{
+    VALUE s;
+    int df;
+
+    s = day_to_sec(f_sub(m_real_jd(x),
+			 UNIX_EPOCH_IN_CJD));
+    if (simple_dat_p(x))
+	return s;
+    df = m_df(x);
+    if (df)
+	s = f_add(s, INT2FIX(df));
+    return s;
+}
+
+#define MILLISECOND_IN_NANOSECONDS 1000000
+
+static VALUE
+tmx_m_msecs(union DateData *x)
+{
+    VALUE s, sf;
+
+    s = sec_to_ms(tmx_m_secs(x));
+    if (simple_dat_p(x))
+	return s;
+    sf = m_sf(x);
+    if (f_nonzero_p(sf))
+	s = f_add(s, f_div(sf, INT2FIX(MILLISECOND_IN_NANOSECONDS)));
+    return s;
+}
+
+static VALUE
 tmx_m_of(union DateData *x)
 {
     return INT2FIX(m_of(x));
@@ -6607,17 +6656,6 @@
     return RSTRING_PTR(m_zone(x));
 }
 
-static VALUE
-tmx_m_timev(union DateData *x)
-{
-    if (simple_dat_p(x))
-	return day_to_sec(f_sub(m_real_jd(x),
-				UNIX_EPOCH_IN_CJD));
-    else
-	return day_to_sec(f_sub(m_ajd(x),
-				UNIX_EPOCH_IN_AJD));
-}
-
 static struct tmx_funcs tmx_funcs = {
     (VALUE (*)(void *))m_real_year,
     (int (*)(void *))m_yday,
@@ -6632,9 +6670,11 @@
     (int (*)(void *))m_hour,
     (int (*)(void *))m_min,
     (int (*)(void *))m_sec,
+    (VALUE (*)(void *))m_sf_in_sec,
+    (VALUE (*)(void *))tmx_m_secs,
+    (VALUE (*)(void *))tmx_m_msecs,
     (VALUE (*)(void *))tmx_m_of,
-    (char *(*)(void *))tmx_m_zone,
-    (VALUE (*)(void *))tmx_m_timev
+    (char *(*)(void *))tmx_m_zone
 };
 
 static void
@@ -8870,6 +8910,8 @@
 {
     if (!test_unit_v2v_iter(sec_to_day, day_to_sec))
 	return Qfalse;
+    if (!test_unit_v2v_iter(ms_to_sec, sec_to_ms))
+	return Qfalse;
     if (!test_unit_v2v_iter(ns_to_day, day_to_ns))
 	return Qfalse;
     if (!test_unit_v2v_iter(ns_to_sec, sec_to_ns))
Index: ext/date/date_strftime.c
===================================================================
--- ext/date/date_strftime.c	(revision 32293)
+++ ext/date/date_strftime.c	(revision 32294)
@@ -406,19 +406,11 @@
 			break;
 
 		case 's':
-			{
-                                VALUE sec = div(tmx_timev, INT2FIX(1));
-                                FMTV('0', 1, "d", sec);
-                        }
+			FMTV('0', 1, "d", tmx_secs);
                         continue;
 
 		case 'Q':
-			{
-				VALUE sec = div(tmx_timev,
-						rb_rational_new2(INT2FIX(1),
-								 INT2FIX(1000)));
-				FMTV('0', 1, "d", sec);
-			}
+			FMTV('0', 1, "d", tmx_msecs);
                         continue;
 
 		case 'S':	/* second, 00 - 59 */
@@ -697,7 +689,7 @@
                         NEEDS(precision);
 
 			{
-                                VALUE subsec = mod(tmx_timev, INT2FIX(1));
+                                VALUE subsec = tmx_sec_fraction;
                                 int ww;
                                 long n;
 
Index: ext/date/date_tmx.h
===================================================================
--- ext/date/date_tmx.h	(revision 32293)
+++ ext/date/date_tmx.h	(revision 32294)
@@ -15,9 +15,11 @@
     int (*hour)(void *dat);
     int (*min)(void *dat);
     int (*sec)(void *dat);
+    VALUE (*sec_fraction)(void *dat);
+    VALUE (*secs)(void *dat);
+    VALUE (*msecs)(void *dat);
     VALUE (*offset)(void *dat);
     char *(*zone)(void *dat);
-    VALUE (*timev)(void *dat);
 };
 struct tmx {
     void *dat;
@@ -39,9 +41,11 @@
 #define tmx_hour tmx_attr(hour)
 #define tmx_min tmx_attr(min)
 #define tmx_sec tmx_attr(sec)
+#define tmx_sec_fraction tmx_attr(sec_fraction)
+#define tmx_secs tmx_attr(secs)
+#define tmx_msecs tmx_attr(msecs)
 #define tmx_offset tmx_attr(offset)
 #define tmx_zone tmx_attr(zone)
-#define tmx_timev tmx_attr(timev)
 
 #endif
 
Index: test/date/test_date_strftime.rb
===================================================================
--- test/date/test_date_strftime.rb	(revision 32293)
+++ test/date/test_date_strftime.rb	(revision 32294)
@@ -194,6 +194,15 @@
     end
   end
 
+  def test_strftime_milli
+    s = '1970-01-01T00:00:00.123456789'
+    d = DateTime.parse(s)
+    assert_equal('123', d.strftime('%Q'))
+    s = '1970-01-02T02:03:04.123456789'
+    d = DateTime.parse(s)
+    assert_equal('93784123', d.strftime('%Q'))
+  end
+
   def test_strftime__minus
     d = DateTime.new(1969, 12, 31, 23, 59, 59)
     assert_equal('-1', d.strftime('%s'))

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

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