www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: std.date

reply Steve Teale <steve.teale britseyeview.com> writes:
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? Steve
Nov 17 2010
next sibling parent Kagamin <spam here.lot> writes:
Steve Teale Wrote:

 So if I want to write a timed log entry, what's the recommendation?

I won't dare to use std.date.
Nov 17 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 17 2010
prev sibling parent Steve Teale <steve.teale britseyeview.com> writes:
On Wed, 17 Nov 2010 22:18:09 -0800, Jonathan M Davis wrote:

 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).
 
  
 - Jonathan M Davis

Ah! It was Clock.currTime() that I had not spotted. Thanks Jonathan
Nov 18 2010