DateTime formatting

bauss jj_1337 at live.dk
Thu Dec 21 04:56:06 UTC 2017


On Wednesday, 20 December 2017 at 22:38:06 UTC, Jonathan M Davis 
wrote:
> On Wednesday, December 20, 2017 21:36:00 bauss via 
> Digitalmars-d-learn wrote:
>> On Wednesday, 20 December 2017 at 18:50:37 UTC, Jonathan M 
>> Davis
>>
>> wrote:
>> > On Wednesday, December 20, 2017 14:30:55 bauss via
>> >
>> > Digitalmars-d-learn wrote:
>> >> I can't seem to find anything in Phobos that allows you to 
>> >> specify custom formats for dates.
>> >>
>> >> Am I on my own or is there already such functionality?
>> >>
>> >> I'm interested in a formatter with the possibility to 
>> >> output ex.:
>> >>
>> >> December 20th, 2017 10:00 AM
>> >>
>> >> All I could find was toSimpleString(), but it seem to use a
>> >> pre-defined format like:
>> >> YYYY-Mon-DD HH:MM:SS
>> >
>> > At present, the only formats that Phobos supports are ISO, 
>> > ISO Extended, and Boost's simple time format. It does not 
>> > yet have support for custom date/time formatting beyond 
>> > constructing the string yourself from the properties on a 
>> > DateTime or SysTime.
>> >
>> > This was posted about recently in the Announce group though:
>> >
>> > http://code.dlang.org/packages/datefmt
>> >
>> > It might do what you want.
>> >
>> > - Jonathan M Davis
>>
>> Unfortunately I can't rely on any third-party packages for it 
>> :p
>>
>> But thanks anyway @ both of you.
>>
>> I'll write my own function to do it :)
>
> Well, if you can't use a 3rd party package, I suspect that 
> you'd have to write your own solution even if Phobos does 
> finally get custom date/time formatting, because what you're 
> trying to do includes "th", which is English-specific, and I'm 
> not about to add localization/i18n stuff to std.datetime. One 
> of the reasons that I regret porting Boost's simple time format 
> over to std.datetime is that it includes English in it, which 
> has occasionally resulted in localization requests, and i18n is 
> really way too involved to put into Phobos.
>
> I expect that I'll get around to finishing custom date/time 
> formatting for Phobos at some point, but it's not a high 
> priority for me, and I have yet to come up with a scheme for 
> defining the formatting that doesn't seem terrible to me.
>
> - Jonathan M Davis

Well a way to achieve it without putting i18n into Phobos would 
be something like being able to specify formatters.

Ex. you could have something like a global formatter, which is 
used if you don't pass a formatter to toString().

Example:
globalTimeFormatter = (fmt, time) {
    // fmt would be the string format passed to ex. 
DateTime.toString()
    // time would be the DateTime

    // return a formatted string based on what you give
};

If you give no formatter to DateTime.toString() like:
auto formattedTime = dateTime.toString("HH:mm:ss");

Then it will automatically use the "globalTimeFormatter".

However you should be able to also "override" the globalFormatter 
when needed like:
auto formattedTime = dateTime.toString("HH:mm:ss", (fmt, time) {
    // Same concept as the global formatter ...
});

Perhaps it could be a static member of DateTime, to avoid 
confusion on which time implementations it's used for. Eg. I'd 
argue that it shouldn't be usable for SysTime.

DateTime.globalFormatter = ...;

This would leave i18n etc. up to the caller.

This can basically be done ouselves using UFC, but something 
official in Phobos would be  great addition to be honest.

It would be a relative small implementation, but very useful.


More information about the Digitalmars-d-learn mailing list