std.date
Jonathan M Davis
jmdavisProg at gmx.com
Wed Nov 17 22:18:09 PST 2010
On Wednesday 17 November 2010 21:35:03 Steve Teale wrote:
> Jonathan M Davis Wrote:
>
> ... (though in the case of adjusting for NTP, the internal stdTimes for the
> SysTimes
>
> > would be off as well, while in the leap second case, they aren't).
> >
> > - Jonathan M Davis
>
> OK, all, thanks for answering that question, but my primary gripe was that
> the current std.date does not have a constructor like this(). My
> assumption being that such a constructor would go to the OS and give you
> an object corresponding to now.
>
> I've looked at Jonathan's documentation, and I don't see a constructor like
> that there either.
>
> So if I want to write a timed log entry, what's the recommendation?
Structs can't have default constructors, so it's impossible to do that. In the
case of std.datetime, the way to get the current time is Clock.currTime(), and
since SysTime has a toString() method, you can just print it. So, you can do
writeln(Clock.currTime().toString()); It has other types of methods of
converting to and from strings if you want a specific format, but toString()
works just fine if you aren't picky about the format (it's also the most readable
of the various formats).
As for std.date. IIRC, you'd use getUTCTime() to get the current time as a
d_time and toUTCString() to print it. As I recall, anything that converts to or
from UTC is broken, so I wouldn't advise it.
If you really want the current time as local time and don't want to use
std.datetime before it's actually in Phobos, then I'd advise just using the
standard C functions. They're in core.stdc.time. The list can be found here:
http://www.cppreference.com/wiki/chrono/c/start
This program would print the current time:
import core.stdc.time;
import std.conv;
import std.stdio;
import std.string;
void main()
{
auto t = time(null);
writeln(strip(to!string(ctime(&t))));
}
to!string() is used because ctime() returns a char*, and strip() is used because
ctime() returns a string with a newline at the end.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list