www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DateTime custom string format

reply Robert Schadek via Digitalmars-d-learn writes:
Is there a function in phobos that lets me do something like
DateTime.format("MM:DD:YYYY ....") with a DateTime instance?
Jun 03 2014
parent reply Jonathan M Davis via Digitalmars-d-learn writes:
On Tue, 03 Jun 2014 19:39:14 +0200
Robert Schadek via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On 06/03/2014 07:12 PM, Jonathan M Davis via Digitalmars-d-learn
 wrote:
 On Tue, 03 Jun 2014 17:07:02 +0200
 Robert Schadek via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 Is there a function in phobos that lets me do something like
 DateTime.format("MM:DD:YYYY ....") with a DateTime instance?
Not currently. It's on my todo list. I intend to get back to it after I've finished with splitting std.datetime (which I should get to fairly soon but have been doing some cleanup in std.datetime first), but I don't know when it will actually be ready. So, for now, you'd have to use std.string.format and the getters on DateTime. - Jonathan M Davis
Ok, I had people asking me for this because of my std.logger default output format. Do you accept PRs for that?
Well, I would prefer to do it myself, but I obviously can't say that I wouldn't accept it if someone else did it and did a good job of it. The main problem however is that we need to come up with a good formatting scheme - that is the format of the custom time strings. What C has doesn't cut it, and what I proposed a while back turned out to be too complicated. There's this 3rd party library which had some interesting ideas: http://pr.stewartsplace.org.uk/d/sutil/doc/datetimeformat.html but I'm convinced that what's there is too simplistic. I'll need to dig up my old proposal and look at how I can simplify it (possibly making it more like what's at that link) without removing too much power from it. Once I have the custom time format strings sorted out, I intend to create both functions which take the format string as a runtime argument and those which take them as compile-time arguments and their corresponding "from" functions as well (e.g. toCustomString and fromCustomString). We'll end up with functions for SysTime, DateTime, Date, and TimeOfDay (possibly by templatizing functions or creating specifically named functions for each of them). My intention is to put them in std.datetime.format once std.datetime has been split (and they've been implemented) rather than sticking them on the types directly. I'll probably also look at adding a way to get at the exact pieces of information you want efficiently without having to have flags for everything in the custom time format strings, making it easier to create strings that are more exotic but still do so without having to call all of the individual getters (which isn't necessarily efficient). But by doing something like that I should be able to simplify the custom time format strings somewhat and avoid some of the more complicated constructs that I had in my previous proposal. - Jonathan M Davis
Jun 03 2014
parent reply "Brad Anderson" <eco gnuk.net> writes:
On Tuesday, 3 June 2014 at 18:22:59 UTC, Jonathan M Davis via 
Digitalmars-d-learn wrote:
 Well, I would prefer to do it myself, but I obviously can't say 
 that I
 wouldn't accept it if someone else did it and did a good job of 
 it. The main
 problem however is that we need to come up with a good 
 formatting scheme -
 that is the format of the custom time strings. What C has 
 doesn't cut it, and
 what I proposed a while back turned out to be too complicated.
Just for reference to Robert and others reading, here's Jonathan's old proposal: http://forum.dlang.org/post/mailman.1806.1324525352.24802.digitalmars-d puremagic.com
Jun 03 2014
parent Jonathan M Davis via Digitalmars-d-learn writes:
On Tue, 03 Jun 2014 22:54:11 +0000
Brad Anderson via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 On Tuesday, 3 June 2014 at 18:22:59 UTC, Jonathan M Davis via
 Digitalmars-d-learn wrote:
 Well, I would prefer to do it myself, but I obviously can't say
 that I
 wouldn't accept it if someone else did it and did a good job of
 it. The main
 problem however is that we need to come up with a good
 formatting scheme -
 that is the format of the custom time strings. What C has
 doesn't cut it, and
 what I proposed a while back turned out to be too complicated.
Just for reference to Robert and others reading, here's Jonathan's old proposal: http://forum.dlang.org/post/mailman.1806.1324525352.24802.digitalmars-d puremagic.com
The link to the docs in that past is no longer valid, but you can download a tarball which includes them here: https://drive.google.com/file/d/0B-tyih3w2oDZaUkyU0pJZl9TeEk/edit?usp=sharing After untaring the file (which should then create the folder datetime_format), just open datetime_format/phobos-prerelease/std_datetime.html in your browser, and you should see the documentation for toCustomString and fromCustomString. But as I said, the proposal was overly complicated (particularly since it tried to accept functions for processing parts of the string). Ideally, we'd probably have something that's somewhere in between what Stewart did and what I was proposing - something simpler than what I proposed but more powerful than what Stewart has. However, I'm going to have to look over both proposals again and mull over it for a bit before I can come up with a better proposal. I'll get back to it, but I'm putting it off until I've finished splitting std.datetime. - Jonathan M Davis
Jun 03 2014