$ cat t.d
import std.datetime.timezone;
class T {
const string NEW_YORK_TIMEZONE = "America/New_York";
public static const auto _timeZoneInfo = PosixTimeZone.getTimeZone(NEW_YORK_TIMEZONE);
}
$ dmd -c t.d # on Linux
/usr/include/dmd/phobos/std/file.d(1972): Error: lstat64 cannot be interpreted at compile time, because it has no available source code
# not sure what it is about, e.g. which file it's looking for?
# on Windows
$ dmd.exe -c t.d
C:\project\dmd2\windows\bin64\..\..\src\phobos\std\file.d(1947): Error: GetFileAttributesW cannot be interpreted at compile time, because it has no available source code
# LDC on Linux
$ ldc2 -c t.d
/home/zhou/project/ldc2-1.20.0-linux-x86_64/bin/../import/std/file.d(1962): Error: lstat64 cannot be interpreted at compile time, because it has no available source code
# GDC on Linux:
$ gdc-10 -c t.d
/usr/lib/gcc/x86_64-linux-gnu/10/include/d/std/file.d:1565:21: error: lstat64 cannot be interpreted at compile time, because it has no available source code
1565 | return lstat(namez, &statbuf) == 0;
| ^
/usr/lib/gcc/x86_64-linux-gnu/10/include/d/std/file.d:1524:22: note: called from here: existsImpl(((Res __tmpfordtor2620 = tempCString(name);) , __tmpfordtor2620).ptr())
1524 | return existsImpl(name.tempCString!FSChar());
| ^
/usr/lib/gcc/x86_64-linux-gnu/10/include/d/std/datetime/timezone.d:1957:37: note: called from here: exists(tzDatabaseDir)
1957 | enforce(tzDatabaseDir.exists(), new DateTimeException(format("Directory %s does not exist.", tzDatabaseDir)));
| ^
/usr/lib/gcc/x86_64-linux-gnu/10/include/d/std/datetime/timezone.d:1957:16: note: called from here: enforce(exists(tzDatabaseDir), delegate Throwable() => new TimeException(format("Directory %s does not exist.", tzDatabaseDir), "/usr/lib/gcc/x86_64-linux-gnu/10/include/d/std/datetime/timezone.d", 1957LU, null))
1957 | enforce(tzDatabaseDir.exists(), new DateTimeException(format("Directory %s does not exist.", tzDatabaseDir)));
| ^
t.d:5:69: note: called from here: getTimeZone(NEW_YORK_TIMEZONE, "/usr/share/zoneinfo/")
5 | public static const auto _timeZoneInfo = PosixTimeZone.getTimeZone(NEW_YORK_TIMEZONE);
| ^
Comment #1 by mingwu — 2020-05-19T18:01:01Z
This file? it's there:
$ ls -l /usr/share/zoneinfo/America/New_York
lrwxrwxrwx 1 root root 13 Sep 20 2019 /usr/share/zoneinfo/America/New_York -> ../posixrules
Comment #2 by razvan.nitu1305 — 2020-05-21T03:55:41Z
This is not a bug. Initializers of class members need to be evaluable at compile time so the compiler tries to interpret the body of function getTimeZone at compile time. Since lstat/GetFileAttributesW are not D functions that have available code, you end up with the (correct) error that is currently outputted. You can call those functions at runtime because you the object code is linked and you have no problems, but interpretation needs the source code.
Fortunately, there is a standard way of doing this, using a class static constructor [1] that were created for exactly this purpose:
import std.datetime.timezone;
class T {
enum NEW_YORK_TIMEZONE = "America/New_York";
public static const PosixTimeZone _timeZoneInfo;
static this()
{
_timeZoneInfo = PosixTimeZone.getTimeZone(NEW_YORK_TIMEZONE);
}
}
Closing this as invalid.
[1] https://dlang.org/spec/class.html#static-constructor
Comment #3 by git — 2024-07-03T18:59:19Z
I am experiencing this issu and because I cannot use GDC, it is impossible to diagnose the source of "`lstat` cannot be interpreted at compile time [...]".
Please provide a call trace in the error message in DMD as does GDC.
Comment #4 by robert.schadek — 2024-12-13T19:08:45Z