From a0cc5b19b7fa4207496a74d2ef8f39480eac44b5 Mon Sep 17 00:00:00 2001 From: shugo Date: Thu, 21 Aug 2008 14:57:35 +0000 Subject: * strftime.c (rb_strftime): supported %F and %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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18755 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- strftime.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'strftime.c') diff --git a/strftime.c b/strftime.c index 811173cd7b..cdc1539648 100644 --- a/strftime.c +++ b/strftime.c @@ -45,7 +45,7 @@ * January 1996 */ -#include "ruby/config.h" +#include "ruby/ruby.h" #ifndef GAWK #include @@ -61,6 +61,7 @@ #include #endif #endif +#include /* defaults: season to taste */ #define SYSV_EXT 1 /* stuff in System V ascftime routine */ @@ -193,6 +194,7 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept #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 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept *s++ = *format; continue; } + precision = -1; again: switch (*++format) { case '\0': @@ -587,11 +590,49 @@ rb_strftime(char *s, size_t maxsize, const char *format, const struct tm *timept 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'; -- cgit v1.2.3