This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 5596320027388e8b82fa25463d6772edfa238825
Author: ganjing <[email protected]>
AuthorDate: Mon Nov 23 10:20:53 2026 +0800

    libc/time: fix strftime %C, %x and %r and add %c and %D.
    
    Compared with the C locale output of glibc for 6 hours of the day and 36
    conversion specifiers, strftime() differed in these places:
    
     - %c and %D were not implemented and produced an empty string.
     - %C printed tm_year / 100 instead of (tm_year + 1900) / 100, so 1999
       gave "00" instead of "19".
     - %x used tm_mon without adding 1 and a 4-digit year, giving
       "01/03/1999" for 1999-02-03 instead of "02/03/99".
     - %r printed hour 0 as "00", hour 12 as "01" because of a comparison
       used as a value, and used lower case "am"/"pm" instead of "AM"/"PM".
    
    Add %c as "%a %b %e %H:%M:%S %Y" and %D as "%m/%d/%y", and correct the
    other three. After the change all 216 outputs (36 specifiers x 6 hours) on
    the simulator match glibc.
    
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/time/lib_strftime.c | 52 +++++++++++++++++++++++++++++++++++--------
 1 file changed, 43 insertions(+), 9 deletions(-)

diff --git a/libs/libc/time/lib_strftime.c b/libs/libc/time/lib_strftime.c
index 43e7dbc7a05..2f4c2a61efc 100644
--- a/libs/libc/time/lib_strftime.c
+++ b/libs/libc/time/lib_strftime.c
@@ -227,8 +227,11 @@ static int get_week_year(FAR const struct tm *time)
  *   %A     The full weekday name according to the current locale.
  *   %b     The abbreviated month name according to the current locale.
  *   %B     The full month name according to the current locale.
+ *   %c     The preferred date and time representation for the current
+ *          locale ("%a %b %e %H:%M:%S %Y" in the C locale).
  *   %C     The century number (year/100) as a 2-digit integer. (SU)
  *   %d     The day of the month as a decimal number (range 01 to 31).
+ *   %D     Equivalent to %m/%d/%y.
  *   %e     Like %d, the day of the month as a decimal number, but a leading
  *          zero is replaced by a space.
  *   %F     The full date format but with no time fields
@@ -533,13 +536,31 @@ process_next:
             }
             break;
 
+          /* %c: Locale date and time, "%a %b %e %H:%M:%S %Y" in the C
+           * locale.
+           */
+
+          case 'c':
+            {
+              if (tm->tm_wday < 7 && tm->tm_mon < 12)
+                {
+                  len = snprintf(dest, chleft,
+                                 "%s %s %2d %02d:%02d:%02d %04d",
+                                 g_abbrev_wdayname[tm->tm_wday],
+                                 g_abbrev_monthname[tm->tm_mon],
+                                 tm->tm_mday, tm->tm_hour, tm->tm_min,
+                                 tm->tm_sec, tm->tm_year + TM_YEAR_BASE);
+                }
+            }
+            break;
+
           /* %x Locale date without time */
 
           case 'x':
             {
-              len = snprintf(dest, chleft, "%02d/%02d/%04d",
-                             tm->tm_mon, tm->tm_mday,
-                             tm->tm_year + TM_YEAR_BASE);
+              len = snprintf(dest, chleft, "%02d/%02d/%02d",
+                             tm->tm_mon + 1, tm->tm_mday,
+                             (tm->tm_year + TM_YEAR_BASE) % 100);
             }
             break;
 
@@ -583,7 +604,8 @@ process_next:
 
           case 'C':
             {
-              len = snprintf(dest, chleft, "%02d", tm->tm_year / 100);
+              len = snprintf(dest, chleft, "%02d",
+                             (tm->tm_year + TM_YEAR_BASE) / 100);
             }
             break;
 
@@ -597,6 +619,16 @@ process_next:
             }
             break;
 
+          /* %D: Date in "%m/%d/%y" format */
+
+          case 'D':
+            {
+              len = snprintf(dest, chleft, "%02d/%02d/%02d",
+                             tm->tm_mon + 1, tm->tm_mday,
+                             (tm->tm_year + TM_YEAR_BASE) % 100);
+            }
+            break;
+
           /* %F: ISO 8601 date format: "%Y-%m-%d" */
 
           case 'F':
@@ -639,16 +671,18 @@ process_next:
             {
               if (tm->tm_hour >= 12)
                 {
-                  str = "pm";
+                  str = "PM";
                 }
               else
                 {
-                  str = "am";
+                  str = "AM";
                 }
 
-              value = tm->tm_hour == 12 ?
-                         tm->tm_hour == 12 :
-                         tm->tm_hour % (HOURSPERDAY / 2);
+              value = tm->tm_hour % (HOURSPERDAY / 2);
+              if (value == 0)
+                {
+                  value = HOURSPERDAY / 2;
+                }
 
               len = snprintf(dest, chleft, "%02d:%02d:%02d %s",
                              value, tm->tm_min, tm->tm_sec, str);

Reply via email to