Skip to content

Commit

Permalink
[doc][feature] add doc about this function, add timezone arg in from_…
Browse files Browse the repository at this point in the history
…unixtime function and unit test
  • Loading branch information
wei.zhao committed Sep 14, 2023
1 parent a9d9aa0 commit db15a73
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
24 changes: 24 additions & 0 deletions docs/en/transform-v2/sql-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,30 @@ Example:

YEAR(CREATED)

### FROM_UNIXTIME

```FROM_UNIXTIME (unixtime, formatString,timeZone)```

Convert the number of seconds from the UNIX epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment.

The most important format characters are: y year, M month, d day, H hour, m minute, s second. For details of the format, see `java.time.format.DateTimeFormatter`.

`timeZone` is optional, default value is system's time zone. `timezone` value can be a `UTC+ timezone offset`, for example, `UTC+8` represents the Asia/Shanghai time zone, see `java.time.ZoneId`

This method returns a string.

Example:

// use default zone

CALL FROM_UNIXTIME(1672502400, 'yyyy-MM-dd HH:mm:ss')

or

// use given zone

CALL FROM_UNIXTIME(1672502400, 'yyyy-MM-dd HH:mm:ss','UTC+6')

## System Functions

### CAST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,13 @@ public static String fromUnixTime(List<Object> args) {
return null;
}
String format = (String) args.get(1);
ZoneId zoneId = ZoneId.systemDefault();
if (args.size() == 3) {
String timeZone = (String) args.get(2);
zoneId = ZoneId.of(timeZone);
}
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
LocalDateTime datetime =
Instant.ofEpochSecond(unixTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDateTime datetime = Instant.ofEpochSecond(unixTime).atZone(zoneId).toLocalDateTime();
return df.format(datetime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,28 @@ public void testFromUnixtimeFunction() {
new SeaTunnelRowType(
new String[] {"unixtime"}, new SeaTunnelDataType[] {BasicType.LONG_TYPE});

// 1672502400 means `2023-01-01 00:00:00` in unix time
Long unixTime = 1672502400L;
SeaTunnelRow inputRow = new SeaTunnelRow(new Long[] {unixTime});

// transform by `from_unixtime` function
sqlEngine.init(
"test",
null,
rowType,
"select from_unixtime(unixtime,'yyyy-MM-dd HH:mm:ss') as ts from test");

// 1672502400 means `2023-01-01 00:00:00` in unix time
Long unixTime = 1672502400L;
SeaTunnelRow inputRow = new SeaTunnelRow(new Long[] {unixTime});

// transform by sql
SeaTunnelRow outRow = sqlEngine.transformBySQL(inputRow);
Object field = outRow.getField(0);

Assertions.assertEquals("2023-01-01 00:00:00", field.toString());

// transform by `from_unixtime` time zone function
sqlEngine.init(
"test",
null,
rowType,
"select from_unixtime(unixtime,'yyyy-MM-dd HH:mm:ss','UTC+6') as ts from test");
SeaTunnelRow outRow1 = sqlEngine.transformBySQL(inputRow);
Object field1 = outRow1.getField(0);
Assertions.assertEquals("2022-12-31 22:00:00", field1.toString());
}
}

0 comments on commit db15a73

Please sign in to comment.