Skip to content

Commit 886f81a

Browse files
avoid repeated tzset() calls
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 529a594 commit 886f81a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/tree_data_common.c

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

1623+
static void
1624+
ly_tzset(void)
1625+
{
1626+
static int ly_tzset_called = 0;
1627+
1628+
if (ly_tzset_called) {
1629+
return;
1630+
}
1631+
tzset();
1632+
ly_tzset_called = 1;
1633+
}
1634+
16231635
LIBYANG_API_DEF int
16241636
ly_time_tz_offset_at(time_t time)
16251637
{
16261638
struct tm tm_local, tm_utc;
16271639
int result = 0;
16281640

16291641
/* init timezone */
1630-
tzset();
1642+
ly_tzset();
16311643

16321644
/* get local and UTC time */
16331645
localtime_r(&time, &tm_local);
@@ -1777,7 +1789,7 @@ ly_time_time2str(time_t time, const char *fractions_s, char **str)
17771789
LY_CHECK_ARG_RET(NULL, str, LY_EINVAL);
17781790

17791791
/* init timezone */
1780-
tzset();
1792+
ly_tzset();
17811793

17821794
/* convert */
17831795
if (!localtime_r(&time, &tm)) {

0 commit comments

Comments
 (0)