Thread overview | ||||||
---|---|---|---|---|---|---|
|
August 23, 2013 SysTime at compile time | ||||
---|---|---|---|---|
| ||||
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... Suggestions are welcome, thanks! Paolo Invernizzi |
August 23, 2013 Re: SysTime at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paolo Invernizzi | 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 |
August 23, 2013 Re: SysTime at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Friday, 23 August 2013 at 19:07:06 UTC, Jonathan M Davis wrote:
> 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.
Thank you Jonathan, in reality I'm not interested in obtaining the current time ('Clock.currTIme' et similia), but only in checking some basic behaviour involving already forged times.
Something works, in a limited and constrained way, but it seems that there's a problem with 'Rebindable' in CTFE:
---
enum t = SysTime(0, UTC());
pragma(msg, t); // SysTime(0L, Rebindable(, (UTC("UTC", "UTC", "UTC"))))
// enum t2 = t + msecs(1000); // typecons.d(956): Error: cannot read uninitialized variable this.original in ctfe
enum d = SysTime(1000, UTC()) - SysTime(10, UTC());
pragma(msg, d); // Duration(990L)
---
Paolo Invernizzi
|
August 23, 2013 Re: SysTime at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paolo Invernizzi | On Friday, August 23, 2013 22:46:34 Paolo Invernizzi wrote: > Thank you Jonathan, in reality I'm not interested in obtaining the current time ('Clock.currTIme' et similia), but only in checking some basic behaviour involving already forged times. > > Something works, in a limited and constrained way, but it seems that there's a problem with 'Rebindable' in CTFE: > > --- > enum t = SysTime(0, UTC()); > pragma(msg, t); // SysTime(0L, Rebindable(, (UTC("UTC", "UTC", > "UTC")))) > // enum t2 = t + msecs(1000); // typecons.d(956): Error: cannot > read uninitialized variable this.original in ctfe > enum d = SysTime(1000, UTC()) - SysTime(10, UTC()); > pragma(msg, d); // Duration(990L) > --- Rebindable should work in CTFE eventually: http://d.puremagic.com/issues/show_bug.cgi?id=10035 However, LocalTime will never work in CTFE, because it has to call tzset. So, it may work eventually to create a SysTime with UTC or a SimpleTimeZone but not LocalTime, and unfortunately, that means that I can never make SysTime.init valid without adding a bunch of null checks to SysTime (which I don't want to do) or making it so that LocalTime always has to check whether tzset has been called and call it if it hasn't instead of calling it when you get the singleton (which I don't really want to do either). So, you might be able to get some of what you want eventually, but SysTime will never fully work at compile time with the local time zone or with getting the current time. - Jonathan M Davis |
Copyright © 1999-2021 by the D Language Foundation