Proposal for custom time string formatting in std.datetime

Kapps Kapps at NotValidEmail.com
Thu Dec 22 02:33:16 PST 2011


On 21/12/2011 9:41 PM, Jonathan M Davis wrote:
> 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

Every language seems to force you memorize a bunch of format strings for 
different toString functions, which is quite annoying. I highly doubt 
I'm the only person who has to look up the format strings every single 
time I want to use a custom toString (besides an obvious one like YYYY).

It would be nice if D did better in this regard. Something taking 
advantage of enums, such as 'time.toCustomString!(Year, ", ", LongHour, 
":", LongMinute, " ", AMPM)'. The problem with that particular example, 
is that it's... quite ugly. And bloated. Perhaps there would be a way to 
make it not quite so bloated however, while still retaining the 
simplicity of compile-time enums.

Perhaps this doesn't matter so much in D though, given that template 
arguments are passed in and thus can be evaluated at compile-time. I 
definitely like the current approach shown in docs, but it's still 
painful to try and figure out something like
assert(st.toCustomString!"%4yplus%04+Y-%emon-%02D%02H:%02m:%02s%f%tz"()
        == "2010-Jul-04 07:06:12").



More information about the Digitalmars-d mailing list