www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - time_t to simple date string conversion

reply Kai Meyer <kai unixlords.com> writes:
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?

-Kai Meyer
Apr 05 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Apr 2011 17:24:11 -0400, Kai Meyer <kai 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 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#toISOExtendedString 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. -Steve
Apr 05 2011
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Apr 2011 17:40:12 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Tue, 05 Apr 2011 17:24:11 -0400, Kai Meyer <kai 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 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#toISOExtendedString and doing it directly: writefln("%s/%s/%s", t.month, t.day, t.year);

bleh, that should have been systime.month, systime.day, systime.year -Steve
Apr 05 2011
prev sibling next sibling parent Kai Meyer <kai unixlords.com> writes:
On 04/05/2011 03:40 PM, Steven Schveighoffer wrote:
 On Tue, 05 Apr 2011 17:24:11 -0400, Kai Meyer <kai 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 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#toISOExtendedString 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. -Steve

Ok, that works. Thanks :)
Apr 05 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 On Tue, 05 Apr 2011 17:24:11 -0400, Kai Meyer <kai 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
Apr 05 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 05 Apr 2011 18:20:02 -0400, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 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.

I think I was one of those who pushed not to have the names in there. I couldn't remember whether it had stayed in or not, hence my non-committal statement :) I agree with the way it is then. We do need some sort of locale package. But judging from Tango's, it's not a small task. Thanks for clarifying. BTW, slightly OT, have you considered reworking datetime's docs to look more like Andrei's new std.algorithm? There's an awful lot of links at the top of that page in non-related order. I really think we need to work on DDoc so that it generates more useful pages. -Steve
Apr 05 2011
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
 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?

std.datetime was completely revamped with 2.52. What existed from in 2.51 has little to do with what's in 2.52, and it wasn't even publically documented anyway. So, if you're using 2.51, you're still stuck using std.date, which is rather broken. I'd definitely recommend that you upgrade to 2.52 if you're still using 2.51. If you're using 2.51 and for some reason can't upgrade, then I'd recommend that you just use the standard C functions. That would be less error-prone than trying to use std.date, which is scheduled for deprecation anyway. And assuming that you can't upgrade, if the reason for that is because you're installed dmd with your distro, I'd recommend uninstalling it and just downloading the zip file from the main site. Your distro is not likely to keep dmd as up-to-date as downloading it yourself will, and it improves too quickly to stick with a version which is months old. It's easy to use the zip. All you have to do is unzip it to wherever you want to unzip it, and the add /path/to/unzipped/dmd2/linux/bin to your path, and it works (unless you're on a 64-bit box, then you need to install certain 32-bit packages, but it's a fairly short list, and if your distro actually includes a package for dmd, then it should be easy to figure out what they are by looking at the dependencies for that package). In any case, prior to dmd 2.52, there effectively was no std.datetime. The date stuff was the rather buggy std.date, which is now scheduled for deprecation. So, either you need to upgrade to 2.52, use std.date, or use the standard C functions. - Jonathan M Davis
Apr 05 2011
parent Kai Meyer <kai unixlords.com> writes:
On 04/05/2011 03:59 PM, Jonathan M Davis 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?

std.datetime was completely revamped with 2.52. What existed from in 2.51 has little to do with what's in 2.52, and it wasn't even publically documented anyway. So, if you're using 2.51, you're still stuck using std.date, which is rather broken. I'd definitely recommend that you upgrade to 2.52 if you're still using 2.51. If you're using 2.51 and for some reason can't upgrade, then I'd recommend that you just use the standard C functions. That would be less error-prone than trying to use std.date, which is scheduled for deprecation anyway. And assuming that you can't upgrade, if the reason for that is because you're installed dmd with your distro, I'd recommend uninstalling it and just downloading the zip file from the main site. Your distro is not likely to keep dmd as up-to-date as downloading it yourself will, and it improves too quickly to stick with a version which is months old. It's easy to use the zip. All you have to do is unzip it to wherever you want to unzip it, and the add /path/to/unzipped/dmd2/linux/bin to your path, and it works (unless you're on a 64-bit box, then you need to install certain 32-bit packages, but it's a fairly short list, and if your distro actually includes a package for dmd, then it should be easy to figure out what they are by looking at the dependencies for that package). In any case, prior to dmd 2.52, there effectively was no std.datetime. The date stuff was the rather buggy std.date, which is now scheduled for deprecation. So, either you need to upgrade to 2.52, use std.date, or use the standard C functions. - Jonathan M Davis

So who do I bug to update the RPM build on the Downloads page? The zip and windows versions are 2.52, but the Linux RPM version is 2.51.
Apr 05 2011
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 On 04/05/2011 03:59 PM, Jonathan M Davis 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?

std.datetime was completely revamped with 2.52. What existed from in 2.51 has little to do with what's in 2.52, and it wasn't even publically documented anyway. So, if you're using 2.51, you're still stuck using std.date, which is rather broken. I'd definitely recommend that you upgrade to 2.52 if you're still using 2.51. If you're using 2.51 and for some reason can't upgrade, then I'd recommend that you just use the standard C functions. That would be less error-prone than trying to use std.date, which is scheduled for deprecation anyway. And assuming that you can't upgrade, if the reason for that is because you're installed dmd with your distro, I'd recommend uninstalling it and just downloading the zip file from the main site. Your distro is not likely to keep dmd as up-to-date as downloading it yourself will, and it improves too quickly to stick with a version which is months old. It's easy to use the zip. All you have to do is unzip it to wherever you want to unzip it, and the add /path/to/unzipped/dmd2/linux/bin to your path, and it works (unless you're on a 64-bit box, then you need to install certain 32-bit packages, but it's a fairly short list, and if your distro actually includes a package for dmd, then it should be easy to figure out what they are by looking at the dependencies for that package). In any case, prior to dmd 2.52, there effectively was no std.datetime. The date stuff was the rather buggy std.date, which is now scheduled for deprecation. So, either you need to upgrade to 2.52, use std.date, or use the standard C functions. - Jonathan M Davis

So who do I bug to update the RPM build on the Downloads page? The zip and windows versions are 2.52, but the Linux RPM version is 2.51.

I'm afraid that I don't know. I just use the zip file. It's simple and it works. Someone other than Walter takes care of the deb and rpm files, I think, but I don't recall who does. Certainly, it appears that whoever deals with the rpm file dropped the ball. If you want to get the rpm updated (and it really should be), then I'd suggest posting about it on the main newsgroup - digitalmars-D. That would have the highest visibility. - Jonathan M Davis
Apr 05 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 On Tue, 05 Apr 2011 18:20:02 -0400, Jonathan M Davis <jmdavisProg gmx.com>
 
 wrote:
 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.

I think I was one of those who pushed not to have the names in there. I couldn't remember whether it had stayed in or not, hence my non-committal statement :) I agree with the way it is then. We do need some sort of locale package. But judging from Tango's, it's not a small task. Thanks for clarifying. BTW, slightly OT, have you considered reworking datetime's docs to look more like Andrei's new std.algorithm? There's an awful lot of links at the top of that page in non-related order. I really think we need to work on DDoc so that it generates more useful pages.

What we really need is for ddoc to just generate proper links. And while what Andrei has done with std.algorithm really helps std.algorithm regardless of how well ddoc organizes its links, I don't think that the same type of thing would help std.datetime as much. The main problem with std.datetime's documentation is that it's primarily made up of structs and classes, and ddoc doesn't do anything special for structs or classes, so it just generates a big mess as far as the links go. It may be worth adjusting std.datetime manually so that it generates properly organized links (similar to what Andrei has done with std.algorithm), but I'm in the midst of reworking the unit tests at present, so I don't know when I'd get around to it. I've been hoping that someone would finally come up with a proper fix for ddoc (some folks have done _some_ work in that area, but it obviously hasn't come to fruition yet). Assuming that ddoc were fixed, that would mostly solve std.datetime's problem, though perhaps some additional organization of the few free functions would still be of value. In any case, I may work on improving std.datetime's links similar to what Andrei has done with std.algorithm, but I have other stuff to fix first. - Jonathan M Davis
Apr 05 2011