Skip to content

Commit

Permalink
LDEV-5115 - fix ISO8601 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Oct 22, 2024
1 parent 731716a commit e79c42f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 20 additions & 1 deletion core/src/main/java/lucee/commons/i18n/FormatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down Expand Up @@ -578,7 +579,25 @@ public static String format(DateTimeFormatter formatter, long millis, TimeZone t
return Instant.ofEpochMilli(millis).atZone(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()).format(formatter);
}

public static long parse(DateTimeFormatter formatter, String date, TimeZone timeZone) throws DateTimeParseException {
public static long parseSimple(DateTimeFormatter formatter, String date, TimeZone timeZone) throws DateTimeParseException {
return ZonedDateTime.parse(date, formatter).withZoneSameInstant(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()).toInstant().toEpochMilli();
}

public static long parse(DateTimeFormatter formatter, String date, TimeZone timeZone) throws DateTimeParseException {
// Parse the date using the formatter (no time zone assumption yet)
ZonedDateTime zonedDateTime = null;

try {
// Parse the date string into a ZonedDateTime
zonedDateTime = ZonedDateTime.parse(date, formatter);
}
catch (DateTimeParseException e) {
// If no time zone is provided in the input, handle it with the passed TimeZone
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
zonedDateTime = localDateTime.atZone(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}

// Convert the parsed ZonedDateTime to the desired time zone and return epoch milliseconds
return zonedDateTime.withZoneSameInstant(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate
if (locale == null) locale = pc.getLocale();
DateFormat df = FormatUtil.getDateTimeFormat(locale, tz, format);
try {
return new DateTimeImpl(FormatUtil.parse(FormatUtil.getDateTimeFormatter(locale, format), strDate, tz));

return new DateTimeImpl(df.parse(strDate));

// return new DateTimeImpl(FormatUtil.parse(FormatUtil.getDateTimeFormatter(locale, format),
// strDate, tz));
}
catch (Exception e) {
ExpressionException ee = new ExpressionException("could not parse the date [" + strDate + "] with the format [" + format + "] with the locale [" + locale
Expand All @@ -103,7 +107,7 @@ public static void main(String[] args) throws PageException {
print.e(_call(null, "2022-01-02T11:22:33+01:00", Locale.GERMANY, TimeZoneConstants.CET, "iso"));
print.e(_call(null, "2022-01-02T11:22:33.444+01:00", Locale.GERMANY, TimeZoneConstants.CET, null));
print.e(_call(null, "2022-01-02T11:22:33.444+01:00", Locale.GERMANY, TimeZoneConstants.CET, "isoms"));
print.e(_call(null, "1/30/02 7:02:33", Locale.GERMANY, TimeZoneConstants.CET, "m/dd/yy h:mm:ss"));
print.e(_call(null, "1/30/02 7:02:33", Locale.GERMANY, TimeZoneConstants.CET, "M/dd/yy h:mm:ss"));

// print.e(_call(null, "1/30/02 7:02:33", Locale.ENGLISH, TimeZoneConstants.UTC, "m/dd/yy
// h:nn:ss"));
Expand Down

0 comments on commit e79c42f

Please sign in to comment.