Using output-range overloads of SysTime.toISO{Ext}String with formatting code

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Jul 9 16:34:24 UTC 2019


On Tuesday, 9 July 2019 at 08:51:59 UTC, Mitacha wrote:
> I've managed to make it work using 'alias this' and wrapper 
> struct.
> https://run.dlang.io/is/3SMEFZ
> It's not an elegant solution, there could be a better way to do 
> this.

Yea, a wrapper struct with custom `toString` seems the most 
obvious way forward.  No need to bother with `alias this`, 
though, since one only needs the wrapper struct at the point 
where one wants to format the datetime info.  A wrapper struct 
like this:

     struct ISOExtSysTime
     {
         private SysTime systime;

         public void toString (Output) (ref Output output)
             if (isOutputRange!(Output, char))
         {
             this.systime.toISOExtString(output);
         }
     }

allows easy usage along the lines of:

     writefln!"%s"(ISOExtSysTime(sys_time));

... and one could easily write a small factory function to 
shorten the code if needed.


More information about the Digitalmars-d-learn mailing list