From 6cb0c05514456801892cbafc6ea2ba9b7fb67544 Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Thu, 20 Jan 2005 19:56:27 +0000 Subject: [PATCH] 2005-01-20 Jeff Johnston * libc/time/strftime.c (strftime): Change %r and %x to be compliant to POSIX standard for "C" locale. Allow %E and %O modifiers to be ignored as long as they precede valid specifiers according to POSIX. --- newlib/ChangeLog | 7 +++ newlib/libc/time/strftime.c | 113 ++++++++++++++++++++++++++++++------ 2 files changed, 103 insertions(+), 17 deletions(-) diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 82723087d..489e2cf0a 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,10 @@ +2005-01-20 Jeff Johnston + + * libc/time/strftime.c (strftime): Change %r and %x to be compliant + to POSIX standard for "C" locale. Allow %E and %O modifiers + to be ignored as long as they precede valid specifiers according + to POSIX. + 2005-01-19 Shaun Jackman * libc/stdlib/setenv_r.c (_setenv_r): Call tzset() if the TZ diff --git a/newlib/libc/time/strftime.c b/newlib/libc/time/strftime.c index 9084d9ffb..4984fcf43 100644 --- a/newlib/libc/time/strftime.c +++ b/newlib/libc/time/strftime.c @@ -86,6 +86,9 @@ The minute, formatted with two digits. o %p Either `<>' or `<>' as appropriate. +o %r +Equivalent to "%I:%M:%S %p". + o %S The second, formatted with two digits. @@ -103,13 +106,11 @@ as beginning with the first Monday in a year. o o %x -A string representing the complete date, in a format like -. Mon Apr 01 1992 +A string representing the complete date, equivalent to "%m/%d/%y". o %X A string representing the full time of day (hours, minutes, and -seconds), in a format like -. 13:13:13 +seconds), equivalent to "%T". o %y The last two digits of the year. @@ -145,6 +146,7 @@ ANSI C requires <>, but does not specify the contents of #include #include #include +#include #include "local.h" static _CONST int dname_len[7] = @@ -186,6 +188,9 @@ _DEFUN (strftime, (s, maxsize, format, tim_p), break; format++; + +check_format: + switch (*format) { case 'a': @@ -261,6 +266,22 @@ _DEFUN (strftime, (s, maxsize, format, tim_p), else return 0; break; + case 'E': + ++format; + switch (*format) + { + case 'c': + case 'C': + case 'x': + case 'X': + case 'y': + case 'Y': + /* Ignore. */ + goto check_format; + default: + --format; + } + break; case 'e': if (count < maxsize - 2) { @@ -332,6 +353,29 @@ _DEFUN (strftime, (s, maxsize, format, tim_p), else return 0; break; + case 'O': + ++format; + switch (*format) + { + case 'd': + case 'e': + case 'H': + case 'I': + case 'm': + case 'M': + case 'S': + case 'u': + case 'U': + case 'V': + case 'w': + case 'W': + case 'y': + /* Ignore. */ + goto check_format; + default: + --format; + } + break; case 'p': if (count < maxsize - 2) { @@ -345,6 +389,39 @@ _DEFUN (strftime, (s, maxsize, format, tim_p), else return 0; break; + case 'r': + if (count < maxsize - 11) + { + if (tim_p->tm_hour == 0 || + tim_p->tm_hour == 12) + { + s[count++] = '1'; + s[count++] = '2'; + } + else + { + sprintf (&s[count], "%.2d", tim_p->tm_hour % 12); + count += 2; + } + s[count++] = ':'; + sprintf (&s[count], "%2.2d", + tim_p->tm_min); + count += 2; + s[count++] = ':'; + sprintf (&s[count], "%2.2d", + tim_p->tm_sec); + count += 2; + s[count++] = ' '; + if (tim_p->tm_hour < 12) + s[count++] = 'A'; + else + s[count++] = 'P'; + + s[count++] = 'M'; + } + else + return 0; + break; case 'S': if (count < maxsize - 2) { @@ -389,20 +466,22 @@ _DEFUN (strftime, (s, maxsize, format, tim_p), return 0; break; case 'x': - if (count < maxsize - 15) + if (count < maxsize - 8) { - for (i = 0; i < 3; i++) - s[count++] = - dname[tim_p->tm_wday][i]; - s[count++] = ' '; - for (i = 0; i < 3; i++) - s[count++] = - mname[tim_p->tm_mon][i]; - - sprintf (&s[count], - " %.2d %.4d", tim_p->tm_mday, - 1900 + tim_p->tm_year); - count += 8; + sprintf (&s[count], "%.2d", + tim_p->tm_mon + 1); + count += 2; + s[count++] = '/'; + sprintf (&s[count], "%.2d", + tim_p->tm_mday); + count += 2; + s[count++] = '/'; + /* The year could be greater than 100, so we need the value + modulo 100. The year could be negative, so we need to + correct for a possible negative remainder. */ + sprintf (&s[count], "%2.2d", + (tim_p->tm_year % 100 + 100) % 100); + count += 2; } else return 0; -- 2.43.5