[your code here]
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu May 9 12:34:21 UTC 2019
On Saturday, May 4, 2019 2:06:59 AM MDT Bastiaan Veelo via Digitalmars-d
wrote:
> On Saturday, 4 May 2019 at 03:15:59 UTC, Hello wrote:
> > auto today()
> > {
> >
> > import std.datetime;
> > auto d = cast(DateTime) Clock.currTime();
> > import std.format: format;
> > return format("%04d/%02d/%02d", d.year, d.month, d.day);
> >
> > }
> >
> > void main()
> > {
> >
> > import std.stdio;
> > today.writeln;
> >
> > }
>
> The cast is unnecessary, and there is writefln:
>
> void main()
> {
> import std.datetime, std.stdio;
> auto d = Clock.currTime();
> writefln("%04d/%02d/%02d", d.year, d.month, d.day);
> }
>
> https://run.dlang.io/is/wT6Cn7
The cast isn't necessary, but it's more efficient. Internally, it basically
doing the equivalent of casting to DateTime every time you ask for the
pieces of the date/time such as the year or hour. So, whether is casting is
better or not depends on whether you want shorter code or faster code.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list