Skip to content

Commit 12bcacc

Browse files
std.datetime.timezone: Respect the TZDIR environment variable (dlang#10776)
Fixes issue dlang#10775.
1 parent 8716c89 commit 12bcacc

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

std/datetime/timezone.d

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public:
291291
// be there, but since PosixTimeZone _does_ use leap seconds if
292292
// the time zone file does, we'll test that functionality if the
293293
// appropriate files exist.
294-
if (chainPath(PosixTimeZone.defaultTZDatabaseDir, "right", tzName).exists)
294+
if (chainPath(PosixTimeZone.getDefaultTZDatabaseDir(), "right", tzName).exists)
295295
{
296296
auto leapTZ = PosixTimeZone.getTimeZone("right/" ~ tzName);
297297

@@ -2014,7 +2014,8 @@ public:
20142014
the TZDatabaseDir version to pass an arbitrary path at compile-time,
20152015
rather than hard-coding it here. Android concatenates all time zone
20162016
data into a single file called tzdata and stores it in the directory
2017-
below.
2017+
below. If the TZDIR environment variable is set, it is consulted
2018+
before this constant.
20182019
+/
20192020
enum defaultTZDatabaseDir = "";
20202021
}
@@ -2040,6 +2041,18 @@ public:
20402041
enum defaultTZDatabaseDir = "";
20412042
}
20422043

2044+
private static string getDefaultTZDatabaseDir()
2045+
{
2046+
import core.stdc.stdlib : getenv;
2047+
import std.string : fromStringz;
2048+
2049+
auto dir = getenv("TZDIR");
2050+
if (dir)
2051+
return fromStringz(dir).idup;
2052+
2053+
return defaultTZDatabaseDir;
2054+
}
2055+
20432056

20442057
/++
20452058
Returns a $(LREF TimeZone) with the give name per the TZ Database. The
@@ -2067,7 +2080,7 @@ public:
20672080
+/
20682081
// TODO make it possible for tzDatabaseDir to be gzipped tar file rather than an uncompressed
20692082
// directory.
2070-
static immutable(PosixTimeZone) getTimeZone(string name, string tzDatabaseDir = defaultTZDatabaseDir) @trusted
2083+
static immutable(PosixTimeZone) getTimeZone(string name, string tzDatabaseDir = getDefaultTZDatabaseDir()) @trusted
20712084
{
20722085
import std.algorithm.sorting : sort;
20732086
import std.conv : to;
@@ -2418,7 +2431,7 @@ public:
24182431
Throws:
24192432
`FileException` if it fails to read from disk.
24202433
+/
2421-
static string[] getInstalledTZNames(string subName = "", string tzDatabaseDir = defaultTZDatabaseDir) @safe
2434+
static string[] getInstalledTZNames(string subName = "", string tzDatabaseDir = getDefaultTZDatabaseDir()) @safe
24222435
{
24232436
import std.algorithm.sorting : sort;
24242437
import std.array : appender;
@@ -2510,11 +2523,12 @@ public:
25102523
{}
25112524
else
25122525
{
2513-
foreach (DirEntry de; dirEntries(defaultTZDatabaseDir, SpanMode.depth))
2526+
string tzDatabaseDir = getDefaultTZDatabaseDir();
2527+
foreach (DirEntry de; dirEntries(tzDatabaseDir, SpanMode.depth))
25142528
{
25152529
if (de.isFile)
25162530
{
2517-
auto tzName = de.name[defaultTZDatabaseDir.length .. $];
2531+
auto tzName = de.name[tzDatabaseDir.length .. $];
25182532

25192533
if (!canFind(tzNames, tzName))
25202534
assertThrown!DateTimeException(testPTZFailure(tzName));

0 commit comments

Comments
 (0)