Proposal for custom time string formatting in std.datetime

Jonathan M Davis jmdavisProg at gmx.com
Thu Dec 22 20:05:25 PST 2011


On Thursday, December 22, 2011 11:20:11 Stewart Gordon wrote:
> On 22/12/2011 03:41, Jonathan M Davis wrote:
> <snip>
> 
> > 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.
> 
> If you mean such things as writing a datum twice consecutively in two
> different formats, it can be done using an empty literal.  For example,
> "Mmm''m" would today generate "Dec12".  Not that I can see any use for such
> a format, just showing that it can be done.

I mean that you have to be way more careful about how you name the flags. For 
instance, if you have

MMM

and

Month

you have issues with stuff like MMMonth. It can definitely work, but the more 
flags that you have, the more problematic it becomes. It's also easier to 
separate out consecutive flags when reading them if you have %.

> > 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.
> How do you mean?

It's harder to have modifiers for flags without the %. For instance, what I'm 
doing with filler characters would be much more difficult with your scheme. With 
% delineating the flags, it becomes easier to handle that sort of thing.

> Looks complicated compared to mine at first sight.  Maybe I just need to
> spend a bit of time looking at it in more detail.

I don't think that it's all that complicated ultimately, but it definitely 
_looks_ complicated to begin with. I _tried_ to do the documentation in a way 
that made it less daunting, but I'm not sure that I succeeded. Actually, it's 
a bit like std.datetime in general in that sense. It really isn't all that 
complicated to use, but there's a lot of it, so it _looks_ complicated and 
therefore is more daunting than it should be.

I probably shouldn't use the standard formats as the first examples. I was 
trying to show examples which corresponded with known formats so that you 
could see how they're done, but those formats are have to be very precise in 
how they're laid out, so they have a complicated set of flags. Doing simpler 
stuff like

"%M/%D/%Y" as the primary examples would probably be better.

- Jonathan M Davis


More information about the Digitalmars-d mailing list