#2942 DateTime pattern with fractional seconds

Henry Tue 27 May

Hi All,

I've noticed what feels like an inconsistency when it comes to parsing DateTimes using fromLocale

If I specify a pattern that contains optional fractional seconds and toLocale then fromLocale if using the same pattern, it fails to parse when the initial DateTime does not contain the fractional seconds.

In this case, specifying a pattern of

"YYYY-MM-DD'T'hh:mm:ss.FFFFFFFFFz"

to attempt to parse valid ISO8601 DateTimes (I'm aware of the fromIso method but have a technical limitation on how I can use it)

When the fractional seconds of the DateTime are zero, or not present it errors:

fansh> pattern := "YYYY-MM-DD'T'hh:mm:ss.FFFFFFFFFz"
YYYY-MM-DD'T'hh:mm:ss.FFFFFFFFFz

fansh> DateTime.fromLocale(DateTime.fromIso("2025-05-27T11:51:28Z").toLocale(pattern), pattern)
sys::ParseErr: Invalid DateTime: '2025-05-27T11:51:28Z': sys::Err: java.lang.RuntimeException: Unexpected tz offset char: 8 [pos 18]
  fan.sys.DateTimeStr.parseDateTime (DateTimeStr.java:387)
  fan.sys.DateTime.fromLocale (DateTime.java:496)
  fan.sys.DateTime.fromLocale (DateTime.java:492)

fansh> DateTime.fromLocale(DateTime.fromIso("2025-05-27T11:51:28.000Z").toLocale(pattern), pattern)
sys::ParseErr: Invalid DateTime: '2025-05-27T11:51:28Z': sys::Err: java.lang.RuntimeException: Unexpected tz offset char: 8 [pos 18]
  fan.sys.DateTimeStr.parseDateTime (DateTimeStr.java:387)
  fan.sys.DateTime.fromLocale (DateTime.java:496)
  fan.sys.DateTime.fromLocale (DateTime.java:492)

fansh> DateTime.fromLocale(DateTime.fromIso("2025-05-27T11:51:28.555Z").toLocale(pattern), pattern)
2025-05-27T11:51:28.555Z UTC

I appreciate it may not be necessarily how the locale methods were designed, but I would expect that a DateTime printed and reparsed with the same pattern to succeed.

Login or Signup to reply.