Skip to content

Commit 64c0b0f

Browse files
tree data UPDATE do not call tzset() (#2367)
* utests - call tzset after setenv TZ unit-tests can fail if run in an env where TZ is already set. This is because utest.h setup calls setenv("TZ"), but does not call tzset() immediately after that. This causes tzname, timezone, and daylight global variables to by out of sync from the newly set environment value of "TZ". To fix this, explicitly call tzset(). It's unlikely that a program would call setenv("TZ") during it's lifetime, and this should ideally only happen in a test env. * avoid tz_set calls in ly_time functions. It does not seem necessary to call `ly_tzset_once()` from `ly_time_time2str()` or `ly_time_tz_offset_at()`. If the user changes TZ env variable, they must explicitly call `tzset()` to update the global variables `tzname`, `timezone` and `daylight`
1 parent ced6435 commit 64c0b0f

File tree

2 files changed

+2
-21
lines changed

2 files changed

+2
-21
lines changed

src/tree_data_common.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,30 +1620,12 @@ ly_time_tz_offset(void)
16201620
return ly_time_tz_offset_at(time(NULL));
16211621
}
16221622

1623-
/**
1624-
* @brief Call tzset() if not already called by this process.
1625-
*/
1626-
static void
1627-
ly_tzset_once(void)
1628-
{
1629-
static int ly_tzset_called = 0;
1630-
1631-
if (ly_tzset_called) {
1632-
return;
1633-
}
1634-
tzset();
1635-
ly_tzset_called = 1;
1636-
}
1637-
16381623
LIBYANG_API_DEF int
16391624
ly_time_tz_offset_at(time_t time)
16401625
{
16411626
struct tm tm_local, tm_utc;
16421627
int result = 0;
16431628

1644-
/* init timezone */
1645-
ly_tzset_once();
1646-
16471629
/* get local and UTC time */
16481630
localtime_r(&time, &tm_local);
16491631
gmtime_r(&time, &tm_utc);
@@ -1791,9 +1773,6 @@ ly_time_time2str(time_t time, const char *fractions_s, char **str)
17911773

17921774
LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
17931775

1794-
/* init timezone */
1795-
ly_tzset_once();
1796-
17971776
/* convert */
17981777
if (!localtime_r(&time, &tm)) {
17991778
return LY_ESYS;

tests/utests/utests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,8 @@ utest_setup(void **state)
13261326

13271327
/* set CET */
13281328
setenv("TZ", "CET+02:00", 1);
1329+
/* call tzset explicitly, to update the tzname, timezone and daylight global variables */
1330+
tzset();
13291331

13301332
return 0;
13311333
}

0 commit comments

Comments
 (0)