Re: fn:format-dateTime without place parameter does not use the system time zone

John Ulric <[email protected]> Fri, 14 May 2021 18:12:22 +0200
Newsgroups gmane.text.xml.saxon.help
Message-ID <CANS5RieEArohUGfhOgU4g=4ewu1DHHK1ujZVSiA5SmAODb6PZg@mail.gmail.com>
Thanks for the clarification. Setting the configuration's default country
to "Europe/Berlin" indeed changes the behavior of the first test, which
then fails because the formatter outputs "2021-05-15 0000". Perfect.

However, could setting the country to a time zone ID have other adverse
effects? I would assume the default country might also be used as a country
variant for the default language, like with locales, or in other places of
the code. Or this always and only part of the default *language?* The
javadoc for Configuration.setDefaultLanguage says one can use a hyphenated
(HTML-style, not Java style) language tag here like "fr-CA", including the
country variant.

@Test
public void testTimeZoneWithDefaultCountry() throws SaxonApiException {
    final Configuration configuration = Configuration.newConfiguration();
    configuration.setDefaultLanguage("de-DE");
    configuration.setDefaultCountry("Europe/Berlin");
    XPathCompiler compiler = new Processor(configuration).newXPathCompiler();
    assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T22:00:00Z'),
'[Y0001]-[M01]-[D01] [H01][m01]', (), (), ())",
null).getStringValue(),
            is("2021-05-15 0000"));
    assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T22:00:00Z'),
'[Y0001]-[M01]-[D01] [H01][m01]', (), (), 'Europe/Berlin')",
null).getStringValue(),
            is("2021-05-15 0000"));
    assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T18:00:00-04:00'),
'[Y0001]-[M01]-[D01] [H01][m01]', (), (), ())",
null).getStringValue(),
            is("2021-05-15 0000"));
}


John







Am Fr., 14. Mai 2021 um 16:46 Uhr schrieb Michael Kay <
[email protected]>:

> I think this behaviour matches the spec.
>
> The spec says:
>
> If the $place argument is supplied in the form of an IANA timezone name
> that is recognized by the implementation, then the date or time being
> formatted is adjusted to the timezone offset applicable in that timezone.
>
> By implication, if it is not supplied, no adjustment takes place.
>
> The spec also says:
>
> If the $place argument is omitted or is set to an empty sequence, then
> the default place defined in the dynamic context is used.
>
> You can set a "default place" using Configuration.setDefaultCountry(), and
> I think (despite the method name and the Javadoc description) you should be
> able to set it to "Europe/Berlin" and this should work. But if you haven't
> set either an explicit place or a default place, it's not going to take it
> from the system default timezone - the timezone actually present in the
> dateTime value takes precedence.
>
> You can always "lose" the explicit timezone present in the value using
> adjust-dateTime(), in this case the default timezone will be used.
>
> Michael Kay
> Saxonica
>
>
>
> On 14 May 2021, at 14:28, John Ulric <[email protected]> wrote:
>
> Hello everyone, is it by intention (or by specification) that
> fn:formatDate does *not* use the system default time zone if no explicit
> place parameter is given?
>
> See this test against current Saxon-HE 10.5:
>
> @Test
> public void testTimeZone() throws SaxonApiException {
>     TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
>     XPathCompiler compiler = new Processor(Configuration.newConfiguration()).newXPathCompiler();
>     assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T22:00:00Z'), '[Y0001]-[M01]-[D01] [H01][m01]', (), (), ())", null).getStringValue(),
>             is("2021-05-14 2200"));
>     assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T22:00:00Z'), '[Y0001]-[M01]-[D01] [H01][m01]', (), (), 'Europe/Berlin')", null).getStringValue(),
>             is("2021-05-15 0000")); // good!
>     assertThat(compiler.evaluateSingle("Q{http://www.w3.org/2005/xpath-functions}format-dateTime(xs:dateTime('2021-05-14T18:00:00-04:00'), '[Y0001]-[M01]-[D01] [H01][m01]', (), (), ())", null).getStringValue(),
>             is("2021-05-14 1800"));
>
> }
>
> Berlin in DST is two hours ahead of GMT. The second assertion with explicit place parameter shows this correctly, displaying the next day 0000 hours. Without a given place, fn:format-dateTime seems to always display the "wall time" as written in the dateTime value, ie, neither in GMT nor in the system time zone, but no time zone at all.
>
> I have also noticed that, while the Configuration class does have settings
> for the default language and country, and these are automatically
> initialized from the system defaults, there is no setting for the default
> time zone.
>
> John
>
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[email protected]
https://lists.sourceforge.net/lists/listinfo/saxon-help