Skip to content

Commit

Permalink
Add ability to specify timezone database location via environment var…
Browse files Browse the repository at this point in the history
…iable

This adds the ability for a user to populate the TZDATA environment
variable to specify the location in which to look for the timezone
database, which allows for a runtime query of this location. Without
this, date currently only supports compile time determination of the
database location.

This facilitates the ability to:
* Use a timezone database provided by a package manager
* Keep the timezone database up to date without re-compilation
* Be flexible about where the timezone database is stored

Closes HowardHinnant#788
  • Loading branch information
samuel-emrys committed Jan 13, 2024
1 parent 88a3b15 commit 9755167
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/tz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
# endif // __MINGW32__

# include <windows.h>
# if !defined(S_ISDIR) && defined(S_IFMT) && defined(_S_IFDIR)
# define S_ISDIR(m) (((m) & S_IFMT) == _S_IFDIR)
# endif
#endif // _WIN32

#include "date/tz_private.h"
Expand Down Expand Up @@ -411,6 +414,14 @@ access_install()
#undef STRINGIZE
#endif // !INSTALL

{
static char* tz_local_env = getenv("TZDATA");
if (tz_local_env != nullptr) {
static std::string tz_local_env_s = tz_local_env;
return tz_local_env_s;
}
}

return install;
}

Expand Down Expand Up @@ -465,6 +476,14 @@ discover_tz_dir()
CONSTDATA auto tz_dir_default = "/usr/share/zoneinfo";
CONSTDATA auto tz_dir_buildroot = "/usr/share/zoneinfo/uclibc";

{
static char* tz_local_env = getenv("TZDATA");
if (tz_local_env != nullptr) {
static std::string tz_local_env_s = tz_local_env;
return tz_local_env_s;
}
}

// Check special path which is valid for buildroot with uclibc builds
if(stat(tz_dir_buildroot, &sb) == 0 && S_ISDIR(sb.st_mode))
return tz_dir_buildroot;
Expand Down

0 comments on commit 9755167

Please sign in to comment.