slinkydeveloper commented on a change in pull request #17878:
URL: https://github.com/apache/flink/pull/17878#discussion_r755268199
##########
File path:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java
##########
@@ -412,92 +492,267 @@ public static Long toTimestamp(String dateStr, TimeZone
tz) {
* @param format date time string format
* @param tz the time zone
*/
- private static Long toTimestamp(String dateStr, String format, TimeZone
tz) {
+ private static long parseTimestampMillis(String dateStr, String format,
TimeZone tz)
+ throws ParseException {
SimpleDateFormat formatter = FORMATTER_CACHE.get(format);
formatter.setTimeZone(tz);
- try {
- return formatter.parse(dateStr).getTime();
- } catch (ParseException e) {
- return null;
- }
- }
-
- public static Long toTimestamp(String dateStr, String format) {
- return toTimestamp(dateStr, format, UTC_ZONE);
+ return formatter.parse(dateStr).getTime();
}
/**
* Parse date time string to timestamp based on the given time zone string
and format. Returns
* null if parsing failed.
*
* @param dateStr the date time string
- * @param format the date time string format
* @param tzStr the time zone id string
*/
- public static Long toTimestampTz(String dateStr, String format, String
tzStr) {
+ private static long parseTimestampTz(String dateStr, String tzStr) throws
ParseException {
TimeZone tz = TIMEZONE_CACHE.get(tzStr);
- return toTimestamp(dateStr, format, tz);
- }
-
- public static Long toTimestampTz(String dateStr, String tzStr) {
- // use "yyyy-MM-dd HH:mm:ss" as default format
- return toTimestampTz(dateStr, TIMESTAMP_FORMAT_STRING, tzStr);
+ return parseTimestampMillis(dateStr,
DateTimeUtils.TIMESTAMP_FORMAT_STRING, tz);
}
- //
--------------------------------------------------------------------------------------------
- // String --> Date conversion
- //
--------------------------------------------------------------------------------------------
-
/** Returns the epoch days since 1970-01-01. */
- public static int strToDate(String dateStr, String fromFormat) {
+ public static int parseDate(String dateStr, String fromFormat) {
// It is OK to use UTC, we just want get the epoch days
// TODO use offset, better performance
- long ts = parseToTimeMillis(dateStr, fromFormat,
TimeZone.getTimeZone("UTC"));
+ long ts = internalParseTimestampMillis(dateStr, fromFormat,
TimeZone.getTimeZone("UTC"));
ZoneId zoneId = ZoneId.of("UTC");
Instant instant = Instant.ofEpochMilli(ts);
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
return ymdToUnixDate(zdt.getYear(), zdt.getMonthValue(),
zdt.getDayOfMonth());
}
- //
--------------------------------------------------------------------------------------------
- // DATE_FORMAT
- //
--------------------------------------------------------------------------------------------
-
- /**
- * Format a timestamp as specific.
- *
- * @param ts the {@link TimestampData} to format.
- * @param format the string formatter.
- * @param zoneId the ZoneId.
- */
- public static String dateFormat(TimestampData ts, String format, ZoneId
zoneId) {
- DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(format);
- Instant instant = ts.toInstant();
- return LocalDateTime.ofInstant(instant, zoneId).format(formatter);
- }
-
- public static String dateFormat(TimestampData ts, String format) {
- return dateFormat(ts, format, ZoneId.of("UTC"));
+ public static Integer parseDate(String s) {
+ // allow timestamp str to date, e.g. 2017-12-12 09:30:00.0
+ int ws1 = s.indexOf(" ");
+ if (ws1 > 0) {
+ s = s.substring(0, ws1);
+ }
+ int hyphen1 = s.indexOf('-');
+ int y;
+ int m;
+ int d;
+ if (hyphen1 < 0) {
+ if (!isInteger(s.trim())) {
+ return null;
+ }
+ y = Integer.parseInt(s.trim());
+ m = 1;
+ d = 1;
+ } else {
+ if (!isInteger(s.substring(0, hyphen1).trim())) {
+ return null;
+ }
+ y = Integer.parseInt(s.substring(0, hyphen1).trim());
+ final int hyphen2 = s.indexOf('-', hyphen1 + 1);
+ if (hyphen2 < 0) {
+ if (!isInteger(s.substring(hyphen1 + 1).trim())) {
+ return null;
+ }
+ m = Integer.parseInt(s.substring(hyphen1 + 1).trim());
+ d = 1;
+ } else {
+ if (!isInteger(s.substring(hyphen1 + 1, hyphen2).trim())) {
+ return null;
+ }
+ m = Integer.parseInt(s.substring(hyphen1 + 1, hyphen2).trim());
+ if (!isInteger(s.substring(hyphen2 + 1).trim())) {
+ return null;
+ }
+ d = Integer.parseInt(s.substring(hyphen2 + 1).trim());
+ }
+ }
+ if (!isIllegalDate(y, m, d)) {
+ return null;
+ }
+ return ymdToUnixDate(y, m, d);
}
- public static String dateFormat(TimestampData ts, String format, TimeZone
zone) {
- return dateFormat(ts, format, zone.toZoneId());
+ public static Integer parseTime(String v) {
+ final int start = 0;
+ final int colon1 = v.indexOf(':', start);
+ // timezone hh:mm:ss[.ssssss][[+|-]hh:mm:ss]
+ // refer https://www.w3.org/TR/NOTE-datetime
+ int timezoneHour;
+ int timezoneMinute;
+ int hour;
+ int minute;
+ int second;
+ int milli;
+ int operator = -1;
+ int end = v.length();
+ int timezone = v.indexOf('-', start);
+ if (timezone < 0) {
+ timezone = v.indexOf('+', start);
+ operator = 1;
+ }
+ if (timezone < 0) {
+ timezoneHour = 0;
+ timezoneMinute = 0;
+ } else {
+ end = timezone;
+ final int colon3 = v.indexOf(':', timezone);
+ if (colon3 < 0) {
+ if (!isInteger(v.substring(timezone + 1).trim())) {
+ return null;
+ }
+ timezoneHour = Integer.parseInt(v.substring(timezone +
1).trim());
+ timezoneMinute = 0;
+ } else {
+ if (!isInteger(v.substring(timezone + 1, colon3).trim())) {
+ return null;
+ }
+ timezoneHour = Integer.parseInt(v.substring(timezone + 1,
colon3).trim());
+ if (!isInteger(v.substring(colon3 + 1).trim())) {
+ return null;
+ }
+ timezoneMinute = Integer.parseInt(v.substring(colon3 +
1).trim());
+ }
+ }
+ if (colon1 < 0) {
+ if (!isInteger(v.substring(start, end).trim())) {
+ return null;
+ }
+ hour = Integer.parseInt(v.substring(start, end).trim());
+ minute = 0;
+ second = 0;
+ milli = 0;
+ } else {
+ if (!isInteger(v.substring(start, colon1).trim())) {
+ return null;
+ }
+ hour = Integer.parseInt(v.substring(start, colon1).trim());
+ final int colon2 = v.indexOf(':', colon1 + 1);
+ if (colon2 < 0) {
+ if (!isInteger(v.substring(colon1 + 1, end).trim())) {
+ return null;
+ }
+ minute = Integer.parseInt(v.substring(colon1 + 1, end).trim());
+ second = 0;
+ milli = 0;
+ } else {
+ if (!isInteger(v.substring(colon1 + 1, colon2).trim())) {
+ return null;
+ }
+ minute = Integer.parseInt(v.substring(colon1 + 1,
colon2).trim());
+ int dot = v.indexOf('.', colon2);
+ if (dot < 0) {
+ if (!isInteger(v.substring(colon2 + 1, end).trim())) {
+ return null;
+ }
+ second = Integer.parseInt(v.substring(colon2 + 1,
end).trim());
+ milli = 0;
+ } else {
+ if (!isInteger(v.substring(colon2 + 1, dot).trim())) {
+ return null;
+ }
+ second = Integer.parseInt(v.substring(colon2 + 1,
dot).trim());
+ milli = parseFraction(v.substring(dot + 1, end).trim());
+ }
+ }
+ }
+ hour += operator * timezoneHour;
+ minute += operator * timezoneMinute;
+ return hour * (int) MILLIS_PER_HOUR
+ + minute * (int) MILLIS_PER_MINUTE
+ + second * (int) MILLIS_PER_SECOND
+ + milli;
}
/**
- * Format a timestamp as specific.
+ * Parses a fraction, multiplying the first character by {@code
multiplier}, the second
+ * character by {@code multiplier / 10}, the third character by {@code
multiplier / 100}, and so
+ * forth.
*
- * @param ts the timestamp to format.
- * @param format the string formatter.
- * @param tz the time zone
+ * <p>For example, {@code parseFraction("1234", 100)} yields {@code 123}.
*/
- public static String dateFormat(long ts, String format, TimeZone tz) {
- SimpleDateFormat formatter = FORMATTER_CACHE.get(format);
- formatter.setTimeZone(tz);
- Date dateTime = new Date(ts);
- return formatter.format(dateTime);
- }
-
+ private static int parseFraction(String v) {
+ int multiplier = 100;
+ int r = 0;
+ for (int i = 0; i < v.length(); i++) {
+ char c = v.charAt(i);
+ int x = c < '0' || c > '9' ? 0 : (c - '0');
+ r += multiplier * x;
+ if (multiplier < 10) {
+ // We're at the last digit. Check for rounding.
+ if (i + 1 < v.length() && v.charAt(i + 1) >= '5') {
+ ++r;
+ }
+ break;
+ }
+ multiplier /= 10;
+ }
+ return r;
+ }
+
+ //
--------------------------------------------------------------------------------------------
+ // Format
+ //
--------------------------------------------------------------------------------------------
+
+ public static String formatTimestamp(TimestampData ts, String format) {
+ return formatTimestamp(ts, format, ZoneId.of("UTC"));
+ }
+
+ public static String formatTimestamp(TimestampData ts, String format,
TimeZone zone) {
+ return formatTimestamp(ts, format, zone.toZoneId());
+ }
+
+ private static String formatTimestamp(TimestampData ts, int precision) {
+ LocalDateTime ldt = ts.toLocalDateTime();
+
+ String fraction = pad(9, ldt.getNano());
+ while (fraction.length() > precision && fraction.endsWith("0")) {
+ fraction = fraction.substring(0, fraction.length() - 1);
+ }
+
+ StringBuilder ymdhms =
+ ymdhms(
+ new StringBuilder(),
+ ldt.getYear(),
+ ldt.getMonthValue(),
+ ldt.getDayOfMonth(),
+ ldt.getHour(),
+ ldt.getMinute(),
+ ldt.getSecond());
+
+ if (fraction.length() > 0) {
+ ymdhms.append(".").append(fraction);
+ }
+
+ return ymdhms.toString();
+ }
+
+ public static String formatTimestamp(TimestampData ts, TimeZone tz, int
precision) {
+ return formatTimestamp(timestampWithLocalZoneToTimestamp(ts, tz),
precision);
+ }
+
+ /**
+ * Format a timestamp as specific.
+ *
+ * @param ts the {@link TimestampData} to format.
+ * @param format the string formatter.
+ * @param zoneId the ZoneId.
+ */
+ private static String formatTimestamp(TimestampData ts, String format,
ZoneId zoneId) {
+ DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(format);
+ Instant instant = ts.toInstant();
+ return LocalDateTime.ofInstant(instant, zoneId).format(formatter);
+ }
+
+ /**
+ * Format a timestamp as specific.
Review comment:
Removed javadoc
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]