Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDEV-5173 - add support for epoch and epochms for ParseDateTime #2453

Open
wants to merge 2 commits into
base: 6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +76,20 @@ 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())) {
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())) {
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));
}

// regular parse date time
if (StringUtil.isEmpty(format, true)) return DateCaster.toDateTime(locale, strDate, tz, isUSLike(locale));

Expand Down Expand Up @@ -117,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()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading