Skip to content

Commit 815159f

Browse files
author
Martin Crossley
committed
Improve comments on UTC->local time
1 parent cea5dfb commit 815159f

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ App|Description
200200
[picow_blink_fast_clock](pico_w/wifi/blink) | Blinks the on-board LED (which is connected via the WiFi chip) with a faster system clock to show how to reconfigure communication with the WiFi chip at build time under those circumstances.
201201
[picow_iperf_server](pico_w/wifi/iperf) | Runs an "iperf" server for WiFi speed testing.
202202
[picow_ntp_client](pico_w/wifi/ntp_client) | Connects to an NTP server to fetch and display the current time.
203-
[picow_ntp_system_time](pico_w/wifi/ntp_system_time) | Creates a background time of day clock that periodically updates itself from a pool of NTP servers, and uses it to display local time.
203+
[picow_ntp_system_time](pico_w/wifi/ntp_system_time) | Creates a background time-of-day clock that periodically updates itself from a pool of NTP servers, and uses it to display local time.
204204
[picow_tcp_client](pico_w/wifi/tcp_client) | A simple TCP client. You can run [python_test_tcp_server.py](pico_w/wifi/python_test_tcp/python_test_tcp_server.py) for it to connect to.
205205
[picow_tcp_server](pico_w/wifi/tcp_server) | A simple TCP server. You can use [python_test_tcp_client.py](pico_w//wifi/python_test_tcp/python_test_tcp_client.py) to connect to it.
206206
[picow_tls_client](pico_w/wifi/tls_client) | Demonstrates how to make a HTTPS request using TLS.

pico_w/wifi/ntp_system_time/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ Creates a time of day clock that periodically synchronises itself to Internet ti
44

55
The example connects to Wi-Fi and displays the local time in the UK or another timezone that you specify, synchronised once an hour to one of the servers from [pool.ntp.org](https://www.ntppool.org/en/).
66

7-
Uses the SNTP application provided by lwIP and the Pico 'always-on timer' _(RTC on Pico/rp2040, powman timer on Pico-2/rp2350)_.
7+
Uses the SNTP application provided by lwIP and the Pico 'always-on timer' _(RTC on Pico/RP2040, powman timer on Pico-2/RP2350)_.
88

99
# Running the example
1010

11-
Provide the SSID and password of your Wi-Fi network by editing `CmakeLists.txt` or on the command line; then build and run the example as usual.
11+
Provide the SSID and password of your Wi-Fi network by editing `CMakeLists.txt` or on the command line; then build and run the example as usual.
1212

1313
You should see something like this:
1414

@@ -67,7 +67,7 @@ If you reconfigure it to use _polling mode_ then your user code should periodica
6767

6868
The SDK provides the high level [pico_aon_timer](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#group_pico_aon_timer) API to provide the same always-on timer functions on Pico and Pico-2 despite their hardware differences.
6969

70-
On the original Pico (rp2040 device) these functions use the real time clock (RTC) and on the Pico-2 (rp2350 device) the POWMAN timer.
70+
On the original Pico (RP2040) these functions use the real time clock (RTC) and on the Pico-2 (RP2350) the POWMAN timer.
7171

7272
For further details refer to the [SDK documentation](https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#group_pico_aon_timer).
7373

pico_w/wifi/ntp_system_time/ntp_system_time.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ int get_time_utc(struct timespec *ts_ptr) {
6363
int main() {
6464
stdio_init_all();
6565

66-
// If you (optionally) define a POSIX TZ here then the example will display local time instead of UTC.
67-
// For the format see https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html
68-
setenv("TZ", "BST0GMT,M3.5.0/1,M10.5.0/2", 1); // <-- this is the timezone spec for Europe/London
69-
// there is no need to call tzset()
70-
7166
// Initialise the Wi-Fi chip
7267
if (cyw43_arch_init()) {
7368
printf("Wi-Fi init failed\n");
@@ -96,19 +91,29 @@ int main() {
9691
struct timespec ts;
9792
struct tm tm;
9893

94+
// OPTIONAL: set the 'TZ' env variable to the local POSIX timezone (in this case Europe/London,
95+
// to create your own see https://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_node/libc_431.html)
96+
setenv("TZ", "BST0GMT,M3.5.0/1,M10.5.0/2", 1);
97+
// If you set 'TZ' then functions like ctime(), localtime() and their variants will automatically
98+
// give results converted to the local timezone instead of UTC (see below).
99+
99100
while (true) {
100101

101102
if(aon_timer_is_initialised) {
102103

103104
// read the current time as UTC seconds and ms since the epoch
104105
get_time_utc(&ts);
105106

106-
// if you simply want to display the local time you could now do so with
107-
// puts(ctime(&(ts.tv_sec)));
107+
// if you just want a string representation of the current time and you're not interested
108+
// in the individual date/time fields, you could simply call ctime(&(ts.tv_sec)) here
109+
// (if you have set 'TZ' then the result will be in local time, otherwise UTC)
110+
111+
// unpack the individual date/time fields (if you have set 'TZ' then the values will be
112+
// in local time, otherwise UTC)
113+
pico_localtime_r(&(ts.tv_sec), &tm);
108114

109-
// to unpack the hours/mins/seconds etc for the local time zone (if defined, see above)
110-
pico_localtime_r(&(ts.tv_sec), &tm); // convert UTC linear time to broken-down local time
111-
printf("%s: %s", tm.tm_isdst ? "BST": "GMT", asctime(&tm)); // display as text
115+
// display individual date/time fields in human readable form
116+
printf("%s: %s", tm.tm_isdst ? "BST": "GMT", asctime(&tm));
112117

113118
} else {
114119
puts("system time not yet initialised");

0 commit comments

Comments
 (0)