Skip to content

Commit d040091

Browse files
author
Martin Crossley
committed
print TZ name and ascii time separately for clarity
1 parent ec5617b commit d040091

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pico_w/wifi/ntp_system_time/ntp_system_time.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int main() {
9494
// OPTIONAL: set the 'TZ' env variable to the local POSIX timezone (in this case Europe/London,
9595
// to create your own see https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html)
9696
setenv("TZ", "BST0GMT,M3.5.0/1,M10.5.0/2", 1);
97-
97+
9898
// If the environment contains a valid 'TZ' definition then functions like ctime(), localtime()
9999
// and their variants automatically give results converted to the local timezone instead of UTC
100100
// (see below).
@@ -119,11 +119,16 @@ int main() {
119119
// implementation if desired.
120120
pico_localtime_r(&(ts.tv_sec), &tm);
121121

122-
// display individual date/time fields in human readable form
123-
printf("%s: %s", tm.tm_isdst ? tzname[0]: tzname[1], asctime(&tm));
124-
// asctime(), ctime() and their variants produce strings of the form "Mon Oct 27 22:06:08 2025\n"
125-
// - note the trailing '\n'.
126-
// extern char *tzname[2] (from time.h) holds the names of the POSIX TZ timezones if defined
122+
// display the name of the currently active local timeszone, if defined
123+
if (getenv("TZ")) {
124+
printf("%s: ", tm.tm_isdst ? tzname[0]: tzname[1]);
125+
// <time.h> defines `extern char *tzname[2]` to hold the names of the POSIX timezones
126+
} else {
127+
printf("UTC: ");
128+
}
129+
130+
// display individual date/time fields in the form "Mon Oct 27 22:06:08 2025\n"
131+
printf("%s", asctime(&tm));
127132

128133
} else {
129134
puts("system time not yet initialised");

0 commit comments

Comments
 (0)