SysTime at compile time

Jonathan M Davis jmdavisProg at gmx.com
Fri Aug 23 12:06:51 PDT 2013


On Friday, August 23, 2013 10:39:56 Paolo Invernizzi wrote:
> There's a way to use a SysTime at compile-time? It seems
> impossible right now for the presence of the TimeZone.
> 
> I want to insert some time-related fields in some structs, but I
> need to have some compile-time check with 'static assert' here
> and there...

Getting the system time would require calling C functions (which never works 
in CTFE), so you can't do what you're trying to do with any CTFE solution, let 
alone SysTime. Even if there were no TimeZone, it couldn't be done.

You might be able to do something with __DATE__, __TIME__, or __TIMESTAMP__ 
though:

http://dlang.org/lex.html#specialtokens

However, they're generated when they're compiled, not when running a function, 
so they coludn't be generated by a CTFE function as it ran, and their values 
might stay the same across the entire build anyway, which may or may not be a 
problem depending on what you're doing. They also go to seconds at the highest 
precision, which may or may not be a problem depending on what you're doing. 
But I think that they're your only chance at getting the time at compile time.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list