Skip to content

Commit 703a641

Browse files
committed
Optimize code and add test
1 parent 4feeb78 commit 703a641

File tree

6 files changed

+313
-55
lines changed

6 files changed

+313
-55
lines changed

docs/content.zh/docs/core-concept/transform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Flink CDC uses [Calcite](https://calcite.apache.org/) to parse expressions and [
162162
| TO_TIMESTAMP(string1[, string2]) | toTimestamp(string1[, string2]) | Converts date time string string1 with format string2 (by default: 'yyyy-MM-dd HH:mm:ss') to a timestamp, without time zone. |
163163
| FROM_UNIXTIME(numeric[, string]) | fromUnixtime(NUMERIC[, STRING]) | Returns a representation of the numeric argument as a value in string format (default is ‘yyyy-MM-dd HH:mm:ss’). numeric is an internal timestamp value representing seconds since ‘1970-01-01 00:00:00’ UTC, such as produced by the UNIX_TIMESTAMP() function. The return value is expressed in the session time zone (specified in TableConfig). E.g., FROM_UNIXTIME(44) returns ‘1970-01-01 00:00:44’ if in UTC time zone, but returns ‘1970-01-01 09:00:44’ if in ‘Asia/Tokyo’ time zone. |
164164
| UNIX_TIMESTAMP() | unixTimestamp() | Gets current Unix timestamp in seconds. This function is not deterministic which means the value would be recalculated for each record. |
165-
| UNIX_TIMESTAMP(string1[, string2]) | unixTimestamp(STRING1[, STRING2]) | Converts a date time string string1 with format string2 (by default: yyyy-MM-dd HH:mm:ss if not specified) to Unix timestamp (in seconds), using the specified timezone in table config.If a time zone is specified in the date time string and parsed by UTC+X format such as “yyyy-MM-dd HH:mm:ss.SSS X”, this function will use the specified timezone in the date time string instead of the timezone in table config. If the date time string can not be parsed, the default value Long.MIN_VALUE(-9223372036854775808) will be returned.|
165+
| UNIX_TIMESTAMP(string1[, string2]) | unixTimestamp(STRING1[, STRING2]) | Converts a date time string string1 with format string2 (by default: yyyy-MM-dd HH:mm:ss if not specified) to Unix timestamp (in seconds), using the specified timezone in table config.<br/>If a time zone is specified in the date time string and parsed by UTC+X format such as “yyyy-MM-dd HH:mm:ss.SSS X”, this function will use the specified timezone in the date time string instead of the timezone in table config. If the date time string can not be parsed, the default value Long.MIN_VALUE(-9223372036854775808) will be returned.|
166166

167167
## Conditional Functions
168168

docs/content/docs/core-concept/transform.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Flink CDC uses [Calcite](https://calcite.apache.org/) to parse expressions and [
162162
| TO_TIMESTAMP(string1[, string2]) | toTimestamp(string1[, string2]) | Converts date time string string1 with format string2 (by default: 'yyyy-MM-dd HH:mm:ss') to a timestamp, without time zone. |
163163
| FROM_UNIXTIME(numeric[, string]) | fromUnixtime(NUMERIC[, STRING]) | Returns a representation of the numeric argument as a value in string format (default is ‘yyyy-MM-dd HH:mm:ss’). numeric is an internal timestamp value representing seconds since ‘1970-01-01 00:00:00’ UTC, such as produced by the UNIX_TIMESTAMP() function. The return value is expressed in the session time zone (specified in TableConfig). E.g., FROM_UNIXTIME(44) returns ‘1970-01-01 00:00:44’ if in UTC time zone, but returns ‘1970-01-01 09:00:44’ if in ‘Asia/Tokyo’ time zone. |
164164
| UNIX_TIMESTAMP() | unixTimestamp() | Gets current Unix timestamp in seconds. This function is not deterministic which means the value would be recalculated for each record. |
165-
| UNIX_TIMESTAMP(string1[, string2]) | unixTimestamp(STRING1[, STRING2]) | Converts a date time string string1 with format string2 (by default: yyyy-MM-dd HH:mm:ss if not specified) to Unix timestamp (in seconds), using the specified timezone in table config.If a time zone is specified in the date time string and parsed by UTC+X format such as “yyyy-MM-dd HH:mm:ss.SSS X”, this function will use the specified timezone in the date time string instead of the timezone in table config. If the date time string can not be parsed, the default value Long.MIN_VALUE(-9223372036854775808) will be returned.|
165+
| UNIX_TIMESTAMP(string1[, string2]) | unixTimestamp(STRING1[, STRING2]) | Converts a date time string string1 with format string2 (by default: yyyy-MM-dd HH:mm:ss if not specified) to Unix timestamp (in seconds), using the specified timezone in table config.<br/>If a time zone is specified in the date time string and parsed by UTC+X format such as “yyyy-MM-dd HH:mm:ss.SSS X”, this function will use the specified timezone in the date time string instead of the timezone in table config. If the date time string can not be parsed, the default value Long.MIN_VALUE(-9223372036854775808) will be returned.|
166166

167167
## Conditional Functions
168168

flink-cdc-common/src/main/java/org/apache/flink/cdc/common/utils/DateTimeUtils.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ private static int ymdToJulian(int year, int month, int day) {
146146
* Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
147147
* "yyyy-MM-dd HH:mm:ss" format.
148148
*/
149-
public static String formatUnixTimestamp(long unixtime, TimeZone tz) {
150-
return formatUnixTimestamp(unixtime, TIMESTAMP_FORMAT_STRING, tz);
149+
public static String formatUnixTimestamp(long unixTime, TimeZone timeZone) {
150+
return formatUnixTimestamp(unixTime, TIMESTAMP_FORMAT_STRING, timeZone);
151151
}
152152

153153
/**
154154
* Convert unix timestamp (seconds since '1970-01-01 00:00:00' UTC) to datetime string in the
155155
* given format.
156156
*/
157-
public static String formatUnixTimestamp(long unixtime, String format, TimeZone tz) {
157+
public static String formatUnixTimestamp(long unixTime, String format, TimeZone timeZone) {
158158
SimpleDateFormat formatter = FORMATTER_CACHE.get(format);
159-
formatter.setTimeZone(tz);
160-
Date date = new Date(unixtime * 1000);
159+
formatter.setTimeZone(timeZone);
160+
Date date = new Date(unixTime * 1000);
161161
try {
162162
return formatter.format(date);
163163
} catch (Exception e) {
@@ -166,25 +166,20 @@ public static String formatUnixTimestamp(long unixtime, String format, TimeZone
166166
}
167167
}
168168

169-
/** Returns the value of the timestamp to seconds since '1970-01-01 00:00:00' UTC. */
170-
public static long unixTimestamp(long ts) {
171-
return ts / 1000;
172-
}
173-
174169
/**
175170
* Returns the value of the argument as an unsigned integer in seconds since '1970-01-01
176171
* 00:00:00' UTC.
177172
*/
178-
public static long unixTimestamp(String dateStr, TimeZone tz) {
179-
return unixTimestamp(dateStr, TIMESTAMP_FORMAT_STRING, tz);
173+
public static long unixTimestamp(String dateStr, TimeZone timeZone) {
174+
return unixTimestamp(dateStr, TIMESTAMP_FORMAT_STRING, timeZone);
180175
}
181176

182177
/**
183178
* Returns the value of the argument as an unsigned integer in seconds since '1970-01-01
184179
* 00:00:00' UTC.
185180
*/
186-
public static long unixTimestamp(String dateStr, String format, TimeZone tz) {
187-
long ts = internalParseTimestampMillis(dateStr, format, tz);
181+
public static long unixTimestamp(String dateStr, String format, TimeZone timeZone) {
182+
long ts = internalParseTimestampMillis(dateStr, format, timeZone);
188183
if (ts == Long.MIN_VALUE) {
189184
return Long.MIN_VALUE;
190185
} else {

flink-cdc-runtime/src/main/java/org/apache/flink/cdc/runtime/functions/SystemFunctionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static String fromUnixtime(long seconds, String format, String timezone)
8989
}
9090

9191
public static long unixTimestamp(long epochTime, String timezone) {
92-
return DateTimeUtils.unixTimestamp(epochTime);
92+
return epochTime / 1000;
9393
}
9494

9595
public static long unixTimestamp(String dateTimeStr, long epochTime, String timezone) {

0 commit comments

Comments
 (0)