www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Getting the current datetime converted into a string for writing to

reply "Gary Miller" <aiguy roadrunner.com> writes:
I've been reading through and trying various code fragments to 
pull the system date/time in and format it as a string for 
display on the console.

I only need it down to the second and from my current machine so 
I don't need to use any of the more accurate clocks used for 
benchmarking right now.

All the examples seem to be setting the date or date time to some 
predetermined value instead of pulling it from the system.

It seems like this would be a very common function to use so I 
know I must be making this a lot harder than it really is.

Oh and before I forget I'd like to thank everybody on the forum 
for their great support.

I'm still a newbie at D.  I've bought the book.  I've watched the 
YouTube videos and now I'm starting to get a lot more comfortable 
with the code but it feels like it's going to take me some time 
to figure out the tricks for using all the library functions.

I started out trying to use strings for everything but quickly 
found that my code is doing a lot of string modification 
concatenation and I had to switch to char[] instead to get around 
the immutability but now a lot of the library functions use the 
string data type so I am having to figure out how to convert 
between back and forth between all the different datatypes.

It would be nice if all the primitive datatypes had a consistent 
toString cast or function but since I'm not seeing those I guess 
writeFormatted must be the preferred way of getting values into 
string format.
Mar 28 2014
next sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
On Saturday, 29 March 2014 at 05:01:14 UTC, Gary Miller wrote:
 I've been reading through and trying various code fragments to 
 pull the system date/time in and format it as a string for 
 display on the console.

 I only need it down to the second and from my current machine 
 so I don't need to use any of the more accurate clocks used for 
 benchmarking right now.
This is the easiest way i know. import std.stdio; import std.datetime; void main(string[] args) { writeln(Clock.currTime.toLocalTime.toSimpleString); }
 It would be nice if all the primitive datatypes had a 
 consistent toString cast or function but since I'm not seeing 
 those I guess writeFormatted must be the preferred way of 
 getting values into string format.
See std.conv and the 'to' function. data.to!(string) data.to!(int) etc... http://dlang.org/phobos/std_conv.html#.to
Mar 29 2014
parent reply "Andre Artus" <andre.artus gmail.com> writes:
 On Saturday, 29 March 2014 at 05:01:14 UTC, Gary Miller wrote:
 -- SNIP --

 I only need it down to the second and from my current machine 
 so I don't need to use any of the more accurate clocks used 
 for benchmarking right now.
 On Saturday, 29 March 2014 at 09:30:44 UTC, Gary Willoughby 
 wrote:
 This is the easiest way i know.

 import std.stdio;
 import std.datetime;

 void main(string[] args)
 {
 	writeln(Clock.currTime.toLocalTime.toSimpleString);
 }
toSimpleString (and toString) returns a string with fractional seconds; which I suspect is not what Gary Miller wants.
 It would be nice if all the primitive datatypes had a 
 consistent toString cast or function but since I'm not seeing 
 those I guess writeFormatted must be the preferred way of 
 getting values into string format.
See std.conv and the 'to' function. data.to!(string) data.to!(int) etc... http://dlang.org/phobos/std_conv.html#.to
Considering the international spread of D users I find it surprising that the date & time formatting functions in phobos are not localizable. When working in Windows I prefer to wrap GetDateFormatEx, but it would be nice to have an OS agnostic version.
Mar 30 2014
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, March 30, 2014 08:25:42 Andre Artus wrote:
 Considering the international spread of D users I find it
 surprising that the date & time formatting functions in phobos
 are not localizable.
If could go back, there wouldn't even be anything to localize. toSimpleString is there because it was in Boost (and it's what toString uses), but by having _three_ different string functions for each type instead of just ISO and ISO Extended. So, that's arguably bad - certainly it overcomplicates things IMHO. And toSimpleString uses English names for the months which is _definitely_ bad, because it opens up the question of localization, which I see no reason to deal with in this context, especially because it opens up a huge can of worms. By sticking to ISO and ISO Extended, we'd be completely standards compliant and would avoid the whole localization issue entirely. As it stands, we just acknowledge that it's stupid that toSimpleString is there (thereby introducing the question of localization) and have agreed that localization is outside the scope of std.datetime. Unfortunately, removing toSimpleString would obviously break code, and changing toString to do the same as toISOExtString (as it probably should) could also break code if there's code in the wild which relies on the exact format of toString. So, we're kind of stuck. - Jonathan M Davis
Mar 30 2014
prev sibling parent "Kagamin" <spam here.lot> writes:
On Sunday, 30 March 2014 at 08:25:43 UTC, Andre Artus wrote:
 Considering the international spread of D users I find it
 surprising that the date & time formatting functions in phobos
 are not localizable.
Formatting should confined to presentation tier: GUI controls, web page generation and must not escape it the rest of code, localized formatting freely used in the whole codebase is a bad idea. Console output is often implied to be technical and even machine-readable, it's a bad idea to localize it. Deep integration of localization in e.g. .net causes lots of pain, the deeper it is, the more pain in the ass it causes, to the point of throwing exceptions with machine-translated error messages.
Mar 31 2014
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Saturday, 29 March 2014 at 05:01:14 UTC, Gary Miller wrote:
 I've been reading through and trying various code fragments to 
 pull the system date/time in and format it as a string for 
 display on the console.
Here's a good primer on the datetime module: http://dlang.org/intro-to-datetime.html
Mar 29 2014