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

ruby-changes:7236

From: shugo <ko1@a...>
Date: Thu, 21 Aug 2008 23:58:59 +0900 (JST)
Subject: [ruby-changes:7236] Ruby:r18755 (trunk): * strftime.c (rb_strftime): supported %F and %<precision>N.

shugo	2008-08-21 23:57:35 +0900 (Thu, 21 Aug 2008)

  New Revision: 18755

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

  Log:
    * strftime.c (rb_strftime): supported %F and %<precision>N.
      reverted config.h to ruby.h for Windows.
    * test/ruby/test_time.rb (TestTime::test_strftime): added tests
      for %F and %N.
    * time.c: documented %F and %N.

  Modified files:
    trunk/ChangeLog
    trunk/strftime.c
    trunk/test/ruby/test_time.rb
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 18754)
+++ time.c	(revision 18755)
@@ -2056,13 +2056,17 @@
  *    %B - The  full  month  name (``January'')
  *    %c - The preferred local date and time representation
  *    %d - Day of the month (01..31)
+ *    %F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
  *    %H - Hour of the day, 24-hour clock (00..23)
  *    %I - Hour of the day, 12-hour clock (01..12)
  *    %j - Day of the year (001..366)
  *    %L - Millisecond of the second (000..999)
  *    %m - Month of the year (01..12)
  *    %M - Minute of the hour (00..59)
- *    %N - Nanosecond of the second (000000000..999999999)
+ *    %N - Fractional seconds digits, default is 9 digits (nanosecond)
+ *            %3N  millisecond (3 digits)
+ *            %6N  microsecond (6 digits)
+ *            %9N  nanosecond (9 digits)
  *    %p - Meridian indicator (``AM''  or  ``PM'')
  *    %S - Second of the minute (00..60)
  *    %U - Week  number  of the current year,
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18754)
+++ ChangeLog	(revision 18755)
@@ -1,3 +1,13 @@
+Thu Aug 21 23:51:51 2008  Shugo Maeda  <shugo@r...>
+
+	* strftime.c (rb_strftime): supported %F and %<precision>N.
+	  reverted config.h to ruby.h for Windows.
+
+	* test/ruby/test_time.rb (TestTime::test_strftime): added tests
+	  for %F and %N.
+
+	* time.c: documented %F and %N.
+
 Thu Aug 21 20:23:26 2008  Kazuhiro NISHIYAMA  <zn@m...>
 
 	* test/ruby/test_m17n_comb.rb (TestM17NComb#test_str_crypt): add
Index: strftime.c
===================================================================
--- strftime.c	(revision 18754)
+++ strftime.c	(revision 18755)
@@ -45,7 +45,7 @@
  * January 1996
  */
 
-#include "ruby/config.h"
+#include "ruby/ruby.h"
 
 #ifndef GAWK
 #include <stdio.h>
@@ -61,6 +61,7 @@
 #include <sys/time.h>
 #endif
 #endif
+#include <math.h>
 
 /* defaults: season to taste */
 #define SYSV_EXT	1	/* stuff in System V ascftime routine */
@@ -193,6 +194,7 @@
 #endif /* HAVE_TIMEZONE */
 #endif /* HAVE_TM_NAME */
 #endif /* HAVE_TM_ZONE */
+	int precision = -1;
 
 	/* various tables, useful in North America */
 	static const char *days_a[] = {
@@ -264,6 +266,7 @@
 			*s++ = *format;
 			continue;
 		}
+		precision = -1;
 	again:
 		switch (*++format) {
 		case '\0':
@@ -587,11 +590,49 @@
 			sprintf(tbuf, "%03ld", ts->tv_nsec / 1000000);
 			break;
 
-		case 'N':	/* nanosecond, 000000000 - 999999999 */
-			sprintf(tbuf, "%09ld", ts->tv_nsec);
+		case 'N':
+			/*
+			 * fractional second digits. default is 9 digits
+			 * (nanosecond).
+			 *
+			 * %3N  millisecond (3 digits)
+			 * %6N  microsecond (6 digits)
+			 * %9N  nanosecond (9 digits)
+			 */
+			{
+				char fmt[10];
+				long n = ts->tv_nsec;
+
+				if (precision < 0 || precision > 9) {
+				    precision = 9;
+				}
+				if (precision == 0) break;
+				n /= pow(10, 9 - precision);
+				sprintf(fmt, "%%0%dld", precision);
+				sprintf(tbuf, fmt, n);
+			}
 			break;
 
+		case 'F':	/*  Equivalent to %Y-%m-%d */
+			{
+				int mon, mday;
+				mon = range(0, timeptr->tm_mon, 11) + 1;
+				mday = range(1, timeptr->tm_mday, 31);
+				sprintf(tbuf, "%ld-%02d-%02d",
+					1900L + timeptr->tm_year, mon, mday);
+			}
+			break;
+
 		default:
+			if (isdigit(*format)) {
+			    const char *p = format;
+			    while (isdigit(*p)) p++;
+			    if (*p == 'N') {
+				precision = atoi(format);
+				format = p - 1;
+				goto again;
+			    }
+			}
 			tbuf[0] = '%';
 			tbuf[1] = *format;
 			tbuf[2] = '\0';
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 18754)
+++ test/ruby/test_time.rb	(revision 18755)
@@ -383,5 +383,17 @@
     t = Time.at(946684800, 123456.789)
     assert_equal("123", t.strftime("%L"))
     assert_equal("123456789", t.strftime("%N"))
+    assert_equal("123", t.strftime("%3N"))
+    assert_equal("123456", t.strftime("%6N"))
+    assert_equal("123456789", t.strftime("%9N"))
+    assert_equal("123456789", t.strftime("%10N"))
+    assert_equal("123456789", t.strftime("%1" + "0" * 100 + "N"))
+    assert_equal("", t.strftime("%0N"))
+    assert_equal("%3S", t.strftime("%3S"))
+    fmt = "%1" + "0" * 100 + "S"
+    assert_equal(fmt, t.strftime(fmt))
+
+    t = Time.mktime(2001, 10, 1)
+    assert_equal("2001-10-01", t.strftime("%F"))
   end
 end

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

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