time_t to simple date string conversion
Jonathan M Davis
jmdavisProg at gmx.com
Tue Apr 5 15:20:02 PDT 2011
> On Tue, 05 Apr 2011 17:24:11 -0400, Kai Meyer <kai at unixlords.com> wrote:
> > I'm reading documentation on std.datetime, and it appears there are
> > added features that I don't have in 2.51 (Linux). Did features like
> > 'SysTime' get added after 2.51?
> >
> > Does anybody have a one-liner to convert a time_t to a date string that
> > should work for me?
>
> auto t = time();
> auto systime = SystemTime(unixTimeToStdTime(t)); // to system time
That's correct. However, unless you're really just getting the current time
instead of getting the time_t from somewhere else, then you'd just do
auto systime = Clock.currTime();
The OP didn't specify where the time_t was coming from though, and if the
time_t comes from somewhere else, then what you gave was correct.
> from there, you have many options to create the right string. You can
> start with just writing it (via toString):
>
> writeln(systime);
>
> There's also:
>
> http://www.digitalmars.com/d/2.0/phobos/std_datetime.html#toSimpleString
> http://www.digitalmars.com/d/2.0/phobos/std_datetime.html#toISOString
> http://www.digitalmars.com/d/2.0/phobos/std_datetime.html#toISOExtendedStri
> ng
>
> and doing it directly:
>
> writefln("%s/%s/%s", t.month, t.day, t.year);
>
> Don't see a way to print the month in text format, but maybe I'm
> overlooking it.
toSimpleString (which toString calls) does give the short version of the month
in it, but during the review process, a number of the folks reviewing it were
against putting code in there relating to printing the month out as a name
rather than a number, because that gets it to dealing locales. The result is
that the only stuff in std.datetime that does anything with the month's name
as a string is toSimpleString (and therefore toString) and fromSimpleString on
SysTime, Date, and DateTime. Everything else is numbers (though the Month enum
does use the 3-letter English abbreviations for the names of its enum values).
I wouldn't have minded having English-specific stuff in there with locales
being dealt with elsewhere, but it essentially got pushed out entirely, so
there's next to no English-specific stuff in there, and it will _all_ be in
locale stuff, if we ever _do_ any locale stuff.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list