Date formatting in D

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 8 07:46:42 PST 2017


On Wednesday, 8 March 2017 at 15:29:11 UTC, aberba wrote:
> Having to do these stuff with C is a punch in the face.
>
> Or PHP was:
>
> date(format, timestamp);

The  PHP function is basically just (translated to D):

string date(string format, time_t timestamp) {
     char[256] buffer;
     auto ret = strftime(buffer.ptr, buffer.ptr, 
toStringz(format), gmtime(&timestamp);
     return buffer[0 .. ret].idup;
}


in other words, it is a thin wrapper around the C function.

Let's see, how do we get a time_t out of D's std.datetime?

http://dpldocs.info/locate?q=time_t

The SysTime "toUnixTime" looks good:

http://dpldocs.info/experimental-docs/std.datetime.SysTime.toUnixTime.html



So an overload might be


string date(string format, SysTime time) {
     date(format, time.toUnixTime());
}


More information about the Digitalmars-d-learn mailing list