DateIntervalFormatter on Linux

foundation
(Soroush Khanlou) #1

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.

3 Likes
(Simon Evans) #2

Ive done an implementation of the formatter which just wraps ICU's udtitvfmt_format() function, documented at http://icu-project.org/apiref/icu4c/udateintervalformat_8h.html#a9c5f0e8265cf22d806f98c048b454420

The code is https://github.com/spevans/swift-corelibs-foundation/commit/7dcac746bfcb51cb00753034897b24dac6d77b2e 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.

4 Likes
(Simon Evans) #3

PR: https://github.com/apple/swift-corelibs-foundation/pull/1931

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

2 Likes
(Lily Vulcano) #4

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
(Simon Evans) #5

Locale.calendar looks to be read-only according to https://developer.apple.com/documentation/foundation/locale/2293254-calendar

(Lily Vulcano) #6

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

(Itai Ferber) #7

@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)

(Simon Evans) #8

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

1 Like
(Lily Vulcano) #9

My bad; I added some confusion there.