DateIntervalFormatter on Linux

I'm working on a new server-side Swift app. Developing on my Mac, I was able to use DateIntervalFormatter easily, but when I deployed to a Linux server, there were a few issues. First, and less important, the Style enum has mismatched values:

On the Mac, these are .none, .short, etc.

More importantly, of course, is that the class itself isn't implemented. I looked into writing it myself, hoping to be able to forward to DateFormatter for most of the locale complexity, but DateIntervalFormatter seems to be a bit more complicated than that. I think implementing it myself is a bit above my head, but I wanted to start a thread for discussion and somewhat formally ask for an implementation of this class. If I'm reading the status page right, this is one of the last pieces we need for full implementations of all the date/time parts of Foundation.


Ive done an implementation of the formatter which just wraps ICU's udtitvfmt_format() function, documented at ICU-docs - International Components for Unicode Docs | icu-docs

The code is DateIntervalFormatter: Basic Implementaion · spevans/swift-corelibs-foundation@7dcac74 · GitHub but it requires a lot of tests to be added to make sure that it matches Darwin's behaviour for different Locales and Timezones and for different format strings. The formatter also requires some optimisation to not create a new one each time unless a property has changed.

If you have some time please add some more tests, then we can try and get this into a PR and into master.

Note it doesn't currently compile on Linux due to a header issue but works fine in Xcode. I'll carry on looking at that part.


PR: DateIntervalFormatter: Basic Implementation by spevans · Pull Request #1931 · apple/swift-corelibs-foundation · GitHub

Works on Linux now, just requires some more tests and an understanding of what the .calendar property does.


The calendar property changes the calendar of the locale that is set on the formatter.

I’ve bumped up this in priority and I can have a bug-for-bug version of this ready ASAP.

1 Like

Locale.calendar looks to be read-only according to Apple Developer Documentation

Yeah; it needs a little bit of contortion to set up an appropriate locale identifier and reparse it.

@spevans I'll comment more in the actual PR, but the design of DateIntervalFormatter on Darwin takes a bit of a different direction than what you've got in the PR.

On Darwin, we use the properties set on the DateIntervalFormatter to configure a DateFormatter to generate a format string template, then configure the UDateIntervalFormat with that format string template. If the user sets an explicit date template on the formatter, we use that.

On mutation of any of the mutable properties, we regenerate a format template, hence why the property is mutable. It doesn't have anything to do with Locale.calendar, though — Locale.calendar is defined exclusively by the locale identifier and shouldn't factor into this except as a fallback in case the calendar is not set. (That is also why Locale.calendar is read-only)

I think I see what you mean, makes more sense now given the properties of the DateFormatter. I'll update the PR.

1 Like

My bad; I added some confusion there.