From d155c836c65f526f7f8613e85c2439ca30860bc7 Mon Sep 17 00:00:00 2001 From: CF Mitrah Date: Wed, 11 Dec 2024 16:29:19 +0530 Subject: [PATCH 1/2] Added a fix for the ticket LDEV-5173. --- .../international/LSParseDateTime.java | 22 +++++++++++++++++++ .../functions/string/ParseDateTime.java | 3 +++ 2 files changed, 25 insertions(+) diff --git a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java index ac9a438f9d..d8696f7e90 100644 --- a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java +++ b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java @@ -22,6 +22,9 @@ package lucee.runtime.functions.international; import java.text.DateFormat; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Date; import java.util.Locale; import java.util.TimeZone; @@ -73,6 +76,25 @@ private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate String strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(oDate)); + if (format != null && "epoch".equalsIgnoreCase(format.trim())) { + String seconds = oDate.toString(); + long timeSeconds = Long.parseLong(seconds); + long milliseconds = timeSeconds*1000; + Instant instant = Instant.ofEpochMilli(milliseconds); + LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); + strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(localDateTime)); + return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale)); + } + else if (format != null && "epochms".equalsIgnoreCase(format.trim())) { + String seconds = oDate.toString(); + long timeSeconds = Long.parseLong(seconds); + long milliseconds = timeSeconds; + Instant instant = Instant.ofEpochMilli(milliseconds); + LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); + strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(localDateTime)); + return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale)); + } + // regular parse date time if (StringUtil.isEmpty(format, true)) return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale)); diff --git a/core/src/main/java/lucee/runtime/functions/string/ParseDateTime.java b/core/src/main/java/lucee/runtime/functions/string/ParseDateTime.java index 23c5057d17..91676e24d4 100644 --- a/core/src/main/java/lucee/runtime/functions/string/ParseDateTime.java +++ b/core/src/main/java/lucee/runtime/functions/string/ParseDateTime.java @@ -53,6 +53,9 @@ public static lucee.runtime.type.dt.DateTime call(PageContext pc, Object oDate, private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate, String popConversion, TimeZone tz) throws PageException { if (!StringUtil.isEmpty(popConversion) && !"standard".equalsIgnoreCase(popConversion = popConversion.trim()) && !"pop".equalsIgnoreCase(popConversion.trim())) { + if("epoch".equalsIgnoreCase(popConversion.trim()) || "epochms".equalsIgnoreCase(popConversion.trim())){ + return LSParseDateTime.call(pc, oDate, Locale.US, tz.getID(), popConversion); + } popConversion = DateTimeFormat.convertMask(popConversion); return LSParseDateTime.call(pc, oDate, Locale.US, tz.getID(), popConversion); } From 11be18864ca83434251fd1e9e11019122a705fdc Mon Sep 17 00:00:00 2001 From: Saravanamuthu Aka CF Mitrah Date: Mon, 16 Dec 2024 12:20:55 +0530 Subject: [PATCH 2/2] Update LSParseDateTime.java --- .../functions/international/LSParseDateTime.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java index d8696f7e90..a135a7fa8e 100644 --- a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java +++ b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java @@ -77,19 +77,14 @@ private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate String strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(oDate)); if (format != null && "epoch".equalsIgnoreCase(format.trim())) { - String seconds = oDate.toString(); - long timeSeconds = Long.parseLong(seconds); - long milliseconds = timeSeconds*1000; - Instant instant = Instant.ofEpochMilli(milliseconds); + Instant instant = Instant.ofEpochMilli(Long.parseLong(oDate.toString()) * 1000); LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(localDateTime)); return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale)); } + else if (format != null && "epochms".equalsIgnoreCase(format.trim())) { - String seconds = oDate.toString(); - long timeSeconds = Long.parseLong(seconds); - long milliseconds = timeSeconds; - Instant instant = Instant.ofEpochMilli(milliseconds); + Instant instant = Instant.ofEpochMilli(Long.parseLong(oDate.toString())); LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime(); strDate = StringUtil.replaceSpecialWhiteSpace(Caster.toString(localDateTime)); return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale)); @@ -139,4 +134,4 @@ public static final boolean isUSLike(Locale locale) { if (locale == null) return false; return locale.getLanguage().equalsIgnoreCase("en") && (StringUtil.isEmpty(locale.getCountry()) || "US".equalsIgnoreCase(locale.getCountry())); } -} \ No newline at end of file +}