Skip to content

Commit e71b6ef

Browse files
tree data OPTIMIZE avoid repeated tzset() calls (#2363)
The tzset() function initializes the tzname variable from the TZ environment variable. There doesn't seem to be any valid usecase of calling this repeatedly. It is an overhead that can be avoided.
1 parent 73f0ab8 commit e71b6ef

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/tree_data_common.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,14 +1620,29 @@ 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+
16231638
LIBYANG_API_DEF int
16241639
ly_time_tz_offset_at(time_t time)
16251640
{
16261641
struct tm tm_local, tm_utc;
16271642
int result = 0;
16281643

16291644
/* init timezone */
1630-
tzset();
1645+
ly_tzset_once();
16311646

16321647
/* get local and UTC time */
16331648
localtime_r(&time, &tm_local);
@@ -1777,7 +1792,7 @@ ly_time_time2str(time_t time, const char *fractions_s, char **str)
17771792
LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
17781793

17791794
/* init timezone */
1780-
tzset();
1795+
ly_tzset_once();
17811796

17821797
/* convert */
17831798
if (!localtime_r(&time, &tm)) {

0 commit comments

Comments
 (0)