This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new f72a19d064e libs/libc/time: Add %Z to strftime() and fix the sign of
%z.
f72a19d064e is described below
commit f72a19d064e9c2934acd4ae9605a3f9b4cde90de
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 20 17:51:00 2026 +0200
libs/libc/time: Add %Z to strftime() and fix the sign of %z.
strftime() had no %Z: the time zone abbreviation that ISO C and POSIX
require was printed as nothing, and the format character was lost. %z
printed a negative offset as "+-100" or "+-030" (the sign of the hours
and of the minutes after a fixed "+").
Add %Z with the other ISO C conversions
(CONFIG_LIBC_STRFTIME_C_STANDARD_FORMATS): it prints tm_zone, which
localtime() and gmtime() set, or no characters when tm_zone is NULL.
Print %z as the sign and the absolute hours and minutes of tm_gmtoff
("-0130"). The comment of the function described %Z under %z; it now
describes both.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
libs/libc/time/Kconfig | 4 ++--
libs/libc/time/lib_strftime.c | 24 +++++++++++++++++++-----
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/libs/libc/time/Kconfig b/libs/libc/time/Kconfig
index d59119e47eb..7b233f8cfd2 100644
--- a/libs/libc/time/Kconfig
+++ b/libs/libc/time/Kconfig
@@ -11,8 +11,8 @@ config LIBC_STRFTIME_C_STANDARD_FORMATS
---help---
Enable ISO C strftime conversion specifiers that are not part of
the small always-enabled subset. Disabling this option
- removes support for %A, %B, %I, %j, %p, %U, %w, %W, %x, %X, and
- %y. The always-enabled subset is %a, %b/%h, %d, %H, %m, %M,
+ removes support for %A, %B, %I, %j, %p, %U, %w, %W, %x, %X, %y,
+ and %Z. The always-enabled subset is %a, %b/%h, %d, %H, %m, %M,
%S, %Y, and %%.
Disabling this option makes strftime smaller, but the
implementation
diff --git a/libs/libc/time/lib_strftime.c b/libs/libc/time/lib_strftime.c
index e07c8c1777d..43e7dbc7a05 100644
--- a/libs/libc/time/lib_strftime.c
+++ b/libs/libc/time/lib_strftime.c
@@ -270,7 +270,8 @@ static int get_week_year(FAR const struct tm *time)
* %X The locale's appropriate time representation, but without date.
* %y The year as a decimal number without a century (range 00 to 99).
* %Y The year as a decimal number including the century.
- * %z The timezone name or abbreviation, or by no bytes if no timezone
+ * %z The offset from UTC as +hhmm or -hhmm.
+ * %Z The timezone name or abbreviation, or by no bytes if no timezone
* information exists.
* %% A literal '%' character.
*
@@ -560,6 +561,19 @@ process_next:
len = snprintf(dest, chleft, "%02d", tm->tm_year % 100);
}
break;
+
+ /* %Z: The time zone abbreviation, or no characters if there is
+ * none.
+ */
+
+ case 'Z':
+ {
+ if (tm->tm_zone != NULL)
+ {
+ len = snprintf(dest, chleft, "%s", tm->tm_zone);
+ }
+ }
+ break;
#endif
#ifdef CONFIG_LIBC_STRFTIME_POSIX_FORMATS
@@ -694,11 +708,11 @@ process_next:
case 'z':
{
- int hour = tm->tm_gmtoff / 3600;
- int min = tm->tm_gmtoff % 3600 / 60;
- int utc_val = hour * 100 + min;
+ long off = tm->tm_gmtoff < 0 ? -tm->tm_gmtoff : tm->tm_gmtoff;
- len = snprintf(dest, chleft, "+%04d", utc_val);
+ len = snprintf(dest, chleft, "%c%02ld%02ld",
+ tm->tm_gmtoff < 0 ? '-' : '+',
+ off / 3600, off % 3600 / 60);
}
break;
#endif