Thread overview
December 20
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


December 20
On 12/20/17 9:30 AM, bauss 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?

You are on your own. Some of the pieces are available, but having support for everything you may want to do with calendars and representation is quite a lot to add to Phobos.

> I'm interested in a formatter with the possibility to output ex.:
> 
> December 20th, 2017 10:00 AM

You will need to build yourself. I checked code.dlang.org, there doesn't seem to be anything (surprising).

-Steve
December 20
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

December 20
On 12/20/17 1:50 PM, Jonathan M Davis wrote:
> This was posted about recently in the Announce group though:
> 
> http://code.dlang.org/packages/datefmt

Hm... a search for "date" on code.dlang.org does not show this. Why not?

http://code.dlang.org/search?q=date

-Steve
December 20
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 :)
December 20
On Wednesday, December 20, 2017 14:14:32 Steven Schveighoffer via Digitalmars-d-learn wrote:
> On 12/20/17 1:50 PM, Jonathan M Davis wrote:
> > This was posted about recently in the Announce group though:
> >
> > http://code.dlang.org/packages/datefmt
>
> Hm... a search for "date" on code.dlang.org does not show this. Why not?
>
> http://code.dlang.org/search?q=date

Yeah, I don't know. Very weird. I only knew about it, because I remembered the recent D.Announce post on it and could look up the name there.

- Jonathan M Davis

December 20
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

December 21
On Wednesday, 20 December 2017 at 14:30:55 UTC, bauss wrote:
> I can't seem to find anything in Phobos that allows you to specify custom formats for dates.
>

sometimes it's just better to take control of things yourself ;-)

https://forum.dlang.org/post/dmxdtciktpggcxybdlcn@forum.dlang.org

December 21
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.