Skip to content

Commit 103ab62

Browse files
authored
Merge pull request #20023 from kfessel/p-nativ-TZ-UTC
board/native: set TZ to UTC by default
2 parents 02b0464 + fd11e3e commit 103ab62

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

boards/common/native/board_init.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @}
1515
*/
1616
#include <stdio.h>
17+
#include <stdlib.h>
1718
#include "board.h"
1819

1920
#include "board_internal.h"
@@ -79,5 +80,9 @@ VFS_AUTO_MOUNT(native, { .hostpath = FS_NATIVE_DIR }, VFS_DEFAULT_NVM(0), 0);
7980
*/
8081
void board_init(void)
8182
{
83+
if (!getenv("TZ")) {
84+
puts("TZ not set, setting UTC");
85+
}
86+
setenv("TZ", "UTC", 0);
8287
puts("RIOT " RIOT_BOARD " board initialized.");
8388
}

boards/common/native/doc.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ and @ref boards_native64. Using `BOARD=native` will automatically select the rig
2626
- Network: Tap Interface
2727
- UART: Runtime configurable - `/dev/tty*` are supported
2828
- Timers: Host timer
29+
- RTC: host time at `UTC` for consistent behavior with embedded systems.
30+
The environment variable `TZ` can be used to explicitly set a specific or hosts timezone (see [below](#setting-a-timezone)).
2931
- LEDs: One red and one green LED - state changes are printed to the UART
3032
- PWM: Dummy PWM
3133
- QDEC: Emulated according to PWM
@@ -77,5 +79,26 @@ Options:
7779
As with any platform, you can specify the sizes of your stacks, i.e. the amount of space your application can use.
7880
You may wish to use a more realistic stack size than native's `THREAD_STACKSIZE_DEFAULT` to increase realism.
7981
82+
# Setting a timezone {#setting-a-timezone}
83+
84+
RIOT native defaults to `UTC` timezone if `TZ` is not set in the environment,
85+
to keep behavior of `time.h` functions like `mktime` consistent with embedded setups,
86+
which usually do not use timezones.
87+
If your application requires the same timezone as host, the `TZ` environment variable should be set accordingly.
88+
There are several ways to achieve that:
89+
- execute `export TZ=":/etc/localtime"` prior `make term`
90+
in the very shell you intend to run `make term` in
91+
- add `export TZ=":/etc/localtime"` to your `~/.profile`
92+
- run `TZ=":/etc/localtime" make term`
93+
94+
The timezone can also be set at runtime by the application for both native and embedded devices using `setenv("TZ", <timezonestring>, 1)`. `picolibc` and `newlibc` expect `<timezonestring>` to have the form `NAME+/-hh:mm:ss<DST-handling>` (`+` pointing west -> `CET-1` adds 1 hour to UTC, see `man timezone`)
95+
e.g., `ACST-9:30ACDT-10:30,M10.1.0,M4.1.0`.
96+
If the `TZ`-string can not be interpreted by the used `libc` (`newlib`,`picolibc`,`glibc`),
97+
they default back to `UTC`.
98+
99+
@warning Some things will behave faulty, since the assumption of most pkgs and
100+
system modules is to run on a embedded system with no timezone set.
101+
One often used function that respect timezone setting is `mktime`
102+
80103
# Known Issues
81104
Check the list of open issues labeled native in the [github issue tracker](https://github.com/RIOT-OS/RIOT/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Platform%3A%20native%22)

0 commit comments

Comments
 (0)