Proposal for custom time string formatting in std.datetime

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 21 19:41:31 PST 2011


Okay. At the moment, the time point types in std.datetime have functions for 
converting to and from strings of standard formats but not custom formats, so 
functions for that need to be added. I've come up with a proposal for how 
they're going to work and would like some feedback on it.

Originally, I was going to make them work like strftime and strptime, since it 
was my understanding that those functions were fairly standard among various 
programming languags. And it _does_ look like a variety of programming 
languages have something similar (Java, Ruby, Python, etc.), but the exact set 
of flags that they use is not standard, so there _isn't_ really a standard to 
follow, just similar functions across a variety of programming languages. And 
honestly, strftime and strptime aren't very good. They're fairly limited IMHO, 
and the choice of flags is fairly arbitrary, so it seems like a good idea to 
design our own, assuming that we can make something better.

Stewart Gordon has a library that takes a different approach ( 
http://pr.stewartsplace.org.uk/d/sutil/datetime_format.html ). It does away 
with % flags and uses maximul munch with each of the flags being name such that 
they don't overlap in a way that would make certain combinations of flags 
impossible. It then requires that characters which are not part of the flags be 
surrounded by single quotes. It's an interesting approach, but it isn't as 
flexible as it could be because of its use of maximul munch instead of % flags.

So, I've come up with something new which tries to take the best of both. On 
the whole, I think that it's fairly straightforward, and the flags are 
generally recognizable and memorable (though there are a lot). It's also 
definitely extremely flexible (e.g. you can pass it functions to generate 
portions of the string if the existing flags don't get you quite what you 
need). But I'd like some feedback on it before I spend a lot of time on the 
implementation.

This page has the docs for std.datetime with everything else but the proposed 
custom formatting functions for SysTime stripped out of it:

http://jmdavis.github.com/d-programming-language.org/std_datetime.html

So, what do you think?

- Jonathan M Davis


More information about the Digitalmars-d mailing list