|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.datetime; |
| 7 | + |
| 8 | +import com.google.common.collect.ImmutableMap; |
| 9 | +import java.time.ZonedDateTime; |
| 10 | +import java.time.format.DateTimeFormatter; |
| 11 | +import java.time.format.TextStyle; |
| 12 | +import java.time.temporal.IsoFields; |
| 13 | +import java.time.temporal.WeekFields; |
| 14 | +import java.util.Locale; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.regex.Matcher; |
| 17 | +import java.util.regex.Pattern; |
| 18 | +import org.opensearch.sql.data.model.ExprStringValue; |
| 19 | +import org.opensearch.sql.data.model.ExprValue; |
| 20 | + |
| 21 | +/** Utility class for POSIX-style strftime formatting. */ |
| 22 | +public class StrftimeFormatterUtil { |
| 23 | + |
| 24 | + // Constants |
| 25 | + private static final String PERCENT_PLACEHOLDER = "\u0001PERCENT\u0001"; |
| 26 | + private static final String PERCENT_LITERAL = "%%"; |
| 27 | + private static final int DEFAULT_NANOSECOND_PRECISION = 9; |
| 28 | + private static final int DEFAULT_MILLISECOND_PRECISION = 3; |
| 29 | + private static final int MICROSECOND_PRECISION = 6; |
| 30 | + private static final long NANOS_PER_SECOND = 1_000_000_000L; |
| 31 | + private static final long MILLIS_PER_SECOND = 1000L; |
| 32 | + private static final long MAX_UNIX_TIMESTAMP = 32536771199L; |
| 33 | + private static final int UNIX_TIMESTAMP_DIGITS = 10; |
| 34 | + |
| 35 | + // Pattern to match %N and %Q with optional precision digit |
| 36 | + private static final Pattern SUBSECOND_PATTERN = Pattern.compile("%(\\d)?([NQ])"); |
| 37 | + |
| 38 | + @FunctionalInterface |
| 39 | + private interface StrftimeFormatHandler { |
| 40 | + String format(ZonedDateTime dateTime); |
| 41 | + } |
| 42 | + |
| 43 | + private static final Map<String, StrftimeFormatHandler> STRFTIME_HANDLERS = buildHandlers(); |
| 44 | + |
| 45 | + private static Map<String, StrftimeFormatHandler> buildHandlers() { |
| 46 | + return ImmutableMap.<String, StrftimeFormatHandler>builder() |
| 47 | + // Date and time combinations |
| 48 | + .put( |
| 49 | + "%c", |
| 50 | + dt -> dt.format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss yyyy", Locale.ROOT))) |
| 51 | + .put( |
| 52 | + "%+", |
| 53 | + dt -> |
| 54 | + dt.format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT))) |
| 55 | + |
| 56 | + // Time formats |
| 57 | + .put("%Ez", StrftimeFormatterUtil::formatTimezoneOffsetMinutes) |
| 58 | + .put("%f", dt -> String.format("%06d", dt.getNano() / 1000)) |
| 59 | + .put("%H", dt -> dt.format(DateTimeFormatter.ofPattern("HH", Locale.ROOT))) |
| 60 | + .put("%I", dt -> dt.format(DateTimeFormatter.ofPattern("hh", Locale.ROOT))) |
| 61 | + .put("%k", dt -> String.format("%2d", dt.getHour())) |
| 62 | + .put("%M", dt -> dt.format(DateTimeFormatter.ofPattern("mm", Locale.ROOT))) |
| 63 | + .put("%p", dt -> dt.format(DateTimeFormatter.ofPattern("a", Locale.ROOT))) |
| 64 | + .put("%S", dt -> dt.format(DateTimeFormatter.ofPattern("ss", Locale.ROOT))) |
| 65 | + .put("%s", dt -> String.valueOf(dt.toEpochSecond())) |
| 66 | + .put("%T", dt -> dt.format(DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ROOT))) |
| 67 | + .put("%X", dt -> dt.format(DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ROOT))) |
| 68 | + |
| 69 | + // Timezone formats |
| 70 | + .put("%Z", dt -> dt.getZone().getDisplayName(TextStyle.SHORT, Locale.ROOT)) |
| 71 | + .put("%z", dt -> dt.format(DateTimeFormatter.ofPattern("xx", Locale.ROOT))) |
| 72 | + .put("%:z", dt -> dt.format(DateTimeFormatter.ofPattern("xxx", Locale.ROOT))) |
| 73 | + .put("%::z", dt -> dt.format(DateTimeFormatter.ofPattern("xxx:ss", Locale.ROOT))) |
| 74 | + .put("%:::z", dt -> dt.format(DateTimeFormatter.ofPattern("x", Locale.ROOT))) |
| 75 | + |
| 76 | + // Date formats |
| 77 | + .put("%F", dt -> dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT))) |
| 78 | + .put("%x", dt -> dt.format(DateTimeFormatter.ofPattern("MM/dd/yyyy", Locale.ROOT))) |
| 79 | + |
| 80 | + // Weekday formats |
| 81 | + .put("%A", dt -> dt.format(DateTimeFormatter.ofPattern("EEEE", Locale.ROOT))) |
| 82 | + .put("%a", dt -> dt.format(DateTimeFormatter.ofPattern("EEE", Locale.ROOT))) |
| 83 | + .put("%w", dt -> String.valueOf(dt.getDayOfWeek().getValue() % 7)) |
| 84 | + |
| 85 | + // Day formats |
| 86 | + .put("%d", dt -> dt.format(DateTimeFormatter.ofPattern("dd", Locale.ROOT))) |
| 87 | + .put("%e", dt -> String.format("%2d", dt.getDayOfMonth())) |
| 88 | + .put("%j", dt -> dt.format(DateTimeFormatter.ofPattern("DDD", Locale.ROOT))) |
| 89 | + |
| 90 | + // Week formats |
| 91 | + .put("%V", dt -> String.format("%02d", dt.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR))) |
| 92 | + .put("%U", dt -> String.format("%02d", dt.get(WeekFields.SUNDAY_START.weekOfYear()) - 1)) |
| 93 | + |
| 94 | + // Month formats |
| 95 | + .put("%b", dt -> dt.format(DateTimeFormatter.ofPattern("MMM", Locale.ROOT))) |
| 96 | + .put("%B", dt -> dt.format(DateTimeFormatter.ofPattern("MMMM", Locale.ROOT))) |
| 97 | + .put("%m", dt -> dt.format(DateTimeFormatter.ofPattern("MM", Locale.ROOT))) |
| 98 | + |
| 99 | + // Year formats |
| 100 | + .put("%C", dt -> String.format("%02d", dt.getYear() / 100)) |
| 101 | + .put("%g", dt -> String.format("%02d", dt.get(IsoFields.WEEK_BASED_YEAR) % 100)) |
| 102 | + .put("%G", dt -> String.format("%04d", dt.get(IsoFields.WEEK_BASED_YEAR))) |
| 103 | + .put("%y", dt -> dt.format(DateTimeFormatter.ofPattern("yy", Locale.ROOT))) |
| 104 | + .put("%Y", dt -> dt.format(DateTimeFormatter.ofPattern("yyyy", Locale.ROOT))) |
| 105 | + |
| 106 | + // Literal percent |
| 107 | + .put(PERCENT_LITERAL, dt -> "%") |
| 108 | + .build(); |
| 109 | + } |
| 110 | + |
| 111 | + private StrftimeFormatterUtil() {} |
| 112 | + |
| 113 | + /** |
| 114 | + * Format a ZonedDateTime using STRFTIME format specifiers. |
| 115 | + * |
| 116 | + * @param dateTime The ZonedDateTime to format |
| 117 | + * @param formatString The STRFTIME format string |
| 118 | + * @return Formatted string as ExprValue, or ExprNullValue if inputs are null |
| 119 | + */ |
| 120 | + public static ExprValue formatZonedDateTime(ZonedDateTime dateTime, String formatString) { |
| 121 | + String result = processFormatString(formatString, dateTime); |
| 122 | + return new ExprStringValue(result); |
| 123 | + } |
| 124 | + |
| 125 | + /** Process the format string and replace all format specifiers. */ |
| 126 | + private static String processFormatString(String formatString, ZonedDateTime dateTime) { |
| 127 | + // Handle %N and %Q with precision first |
| 128 | + String result = handleSubSecondFormats(formatString, dateTime); |
| 129 | + |
| 130 | + // Escape %% by replacing with placeholder |
| 131 | + result = result.replace(PERCENT_LITERAL, PERCENT_PLACEHOLDER); |
| 132 | + |
| 133 | + // Replace all other format specifiers |
| 134 | + result = replaceFormatSpecifiers(result, dateTime); |
| 135 | + |
| 136 | + // Restore literal percent signs |
| 137 | + return result.replace(PERCENT_PLACEHOLDER, "%"); |
| 138 | + } |
| 139 | + |
| 140 | + /** Replace all format specifiers in the string. */ |
| 141 | + private static String replaceFormatSpecifiers(String input, ZonedDateTime dateTime) { |
| 142 | + String result = input; |
| 143 | + for (Map.Entry<String, StrftimeFormatHandler> entry : STRFTIME_HANDLERS.entrySet()) { |
| 144 | + String specifier = entry.getKey(); |
| 145 | + if (result.contains(specifier)) { |
| 146 | + String replacement = entry.getValue().format(dateTime); |
| 147 | + result = result.replace(specifier, replacement); |
| 148 | + } |
| 149 | + } |
| 150 | + return result; |
| 151 | + } |
| 152 | + |
| 153 | + /** Handle %N and %Q subsecond formats with optional precision. */ |
| 154 | + private static String handleSubSecondFormats(String format, ZonedDateTime dateTime) { |
| 155 | + StringBuilder result = new StringBuilder(); |
| 156 | + Matcher matcher = SUBSECOND_PATTERN.matcher(format); |
| 157 | + |
| 158 | + while (matcher.find()) { |
| 159 | + String precisionStr = matcher.group(1); |
| 160 | + String type = matcher.group(2); |
| 161 | + |
| 162 | + int precision = parsePrecision(precisionStr, type); |
| 163 | + String replacement = formatSubseconds(dateTime, type, precision); |
| 164 | + |
| 165 | + matcher.appendReplacement(result, replacement); |
| 166 | + } |
| 167 | + matcher.appendTail(result); |
| 168 | + |
| 169 | + return result.toString(); |
| 170 | + } |
| 171 | + |
| 172 | + /** Parse precision value for subsecond formats. */ |
| 173 | + private static int parsePrecision(String precisionStr, String type) { |
| 174 | + if (precisionStr != null) { |
| 175 | + return Integer.parseInt(precisionStr); |
| 176 | + } |
| 177 | + // Default: %N=9 (nanoseconds), %Q=3 (milliseconds) |
| 178 | + return "N".equals(type) ? DEFAULT_NANOSECOND_PRECISION : DEFAULT_MILLISECOND_PRECISION; |
| 179 | + } |
| 180 | + |
| 181 | + /** Format subseconds based on type and precision. */ |
| 182 | + private static String formatSubseconds(ZonedDateTime dateTime, String type, int precision) { |
| 183 | + if ("N".equals(type)) { |
| 184 | + // %N - subsecond digits (nanoseconds) |
| 185 | + return formatNanoseconds(dateTime.getNano(), precision); |
| 186 | + } else { |
| 187 | + // %Q - subsecond component |
| 188 | + return formatQSubseconds(dateTime, precision); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** Format nanoseconds with specified precision. */ |
| 193 | + private static String formatNanoseconds(long nanos, int precision) { |
| 194 | + double scaled = (double) nanos / NANOS_PER_SECOND; |
| 195 | + long truncated = (long) (scaled * Math.pow(10, precision)); |
| 196 | + return String.format("%0" + precision + "d", truncated); |
| 197 | + } |
| 198 | + |
| 199 | + /** Format Q-type subseconds based on precision. */ |
| 200 | + private static String formatQSubseconds(ZonedDateTime dateTime, int precision) { |
| 201 | + switch (precision) { |
| 202 | + case MICROSECOND_PRECISION: |
| 203 | + // Microseconds |
| 204 | + long micros = dateTime.getNano() / 1000; |
| 205 | + return String.format("%06d", micros); |
| 206 | + |
| 207 | + case DEFAULT_NANOSECOND_PRECISION: |
| 208 | + // Nanoseconds |
| 209 | + return String.format("%09d", dateTime.getNano()); |
| 210 | + |
| 211 | + default: |
| 212 | + // Default to milliseconds |
| 213 | + long millis = dateTime.toInstant().toEpochMilli() % MILLIS_PER_SECOND; |
| 214 | + return String.format("%03d", millis); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + /** Format timezone offset in minutes. */ |
| 219 | + private static String formatTimezoneOffsetMinutes(ZonedDateTime dt) { |
| 220 | + int offsetMinutes = dt.getOffset().getTotalSeconds() / 60; |
| 221 | + return String.format("%+d", offsetMinutes); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Extract the first 10 digits from a timestamp to get UNIX seconds. This handles timestamps that |
| 226 | + * may be in milliseconds or nanoseconds. |
| 227 | + * |
| 228 | + * @param timestamp The timestamp value |
| 229 | + * @return UNIX timestamp in seconds |
| 230 | + */ |
| 231 | + public static long extractUnixSeconds(double timestamp) { |
| 232 | + // Return as-is if within valid Unix timestamp range |
| 233 | + if (timestamp >= -MAX_UNIX_TIMESTAMP && timestamp <= MAX_UNIX_TIMESTAMP) { |
| 234 | + return (long) timestamp; |
| 235 | + } |
| 236 | + |
| 237 | + // For larger absolute values, extract first 10 digits (assumes milliseconds/nanoseconds) |
| 238 | + return extractFirstNDigits(timestamp, UNIX_TIMESTAMP_DIGITS); |
| 239 | + } |
| 240 | + |
| 241 | + /** Extract the first N digits from a number. */ |
| 242 | + private static long extractFirstNDigits(double value, int digits) { |
| 243 | + boolean isNegative = value < 0; |
| 244 | + long absValue = Math.abs((long) value); |
| 245 | + String valueStr = String.valueOf(absValue); |
| 246 | + |
| 247 | + long result = |
| 248 | + valueStr.length() <= digits ? absValue : Long.parseLong(valueStr.substring(0, digits)); |
| 249 | + |
| 250 | + return isNegative ? -result : result; |
| 251 | + } |
| 252 | +} |
0 commit comments