SysTime bug or feature?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 5 22:54:31 PDT 2015


On Monday, October 05, 2015 18:12:06 tchaloupka via Digitalmars-d-learn wrote:
> This code:
>
> import std.stdio;
> import std.datetime;
>
> void main()
> {
>      SysTime t = SysTime.init;
>      writeln(t);
> }
>
> results in segfault with dmd-2.068.2
>
> Is it ok?

It is by design, albeit undesirable. When SysTime was originally written, it
was impossible to have a default value for a class reference other than
null. So, unless SysTime was going to take the performance hit of constantly
checking whether its TimeZone was null, SysTime.init was going to segfault
if you did anything with it that required its TimeZone. And I wasn't about
to have it constantly checking for null. In the vast majority of cases, the
value of a SysTime comes from Clock.currTime() or from parsing a string, and
if code is trying to do anything but assign to a SysTime which is
SysTime.init, then that means that it failed to initialize it like it should
have.

At some point in the last couple of years, it became possible to directly
initialize a class reference that was a member variable (or static, or at
module level, etc.) with an immutable class object. So, when the issue that
you're bringing up was brought up last year, I tried to create a new
TimeZone class that would specifically be for SysTime.init so that
SysTime.init would still be unique (so that st is SysTime.init would still
work properly), and it would print out "SysTime.init" for toString and be
treated as 00001-01-01T00:00:00+00:00 for most everything else, but a
compiler bugs with regards to Rebindable made it so that it didn't work.

I need to take another stab at it and see if it works now, but until the
compiler issue is resolved, SysTime.init will continue to segfault if you do
anything with it which requires its TimeZone to be used (which includes
toString).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list