Skip to content

Commit

Permalink
fix date and date time arithmatic
Browse files Browse the repository at this point in the history
  • Loading branch information
LiBinfeng-01 committed Sep 18, 2024
1 parent 1f33137 commit 3a187e2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ public static Expression daysAdd(DateTimeV2Literal date, IntegerLiteral day) {
/**
* datetime arithmetic function hours-add.
*/
@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateLiteral date, IntegerLiteral hour) {
return date.plusHours(hour.getValue());
}

@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateV2Literal date, IntegerLiteral hour) {
return date.plusHours(hour.getValue());
}

@ExecFunction(name = "hours_add")
public static Expression hoursAdd(DateTimeLiteral date, IntegerLiteral hour) {
return date.plusHours(hour.getValue());
Expand All @@ -187,6 +197,16 @@ public static Expression hoursAdd(DateTimeV2Literal date, IntegerLiteral hour) {
/**
* datetime arithmetic function minutes-add.
*/
@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateLiteral date, IntegerLiteral minute) {
return date.plusMinutes(minute.getValue());
}

@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateV2Literal date, IntegerLiteral minute) {
return date.plusMinutes(minute.getValue());
}

@ExecFunction(name = "minutes_add")
public static Expression minutesAdd(DateTimeLiteral date, IntegerLiteral minute) {
return date.plusMinutes(minute.getValue());
Expand All @@ -200,6 +220,16 @@ public static Expression minutesAdd(DateTimeV2Literal date, IntegerLiteral minut
/**
* datetime arithmetic function seconds-add.
*/
@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateLiteral date, IntegerLiteral second) {
return date.plusSeconds(second.getValue());
}

@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateV2Literal date, IntegerLiteral second) {
return date.plusSeconds(second.getValue());
}

@ExecFunction(name = "seconds_add")
public static Expression secondsAdd(DateTimeLiteral date, IntegerLiteral second) {
return date.plusSeconds(second.getValue());
Expand Down Expand Up @@ -404,4 +434,14 @@ public static Expression dateDiff(DateTimeV2Literal date1, DateTimeV2Literal dat
private static int dateDiff(LocalDateTime date1, LocalDateTime date2) {
return ((int) ChronoUnit.DAYS.between(date2.toLocalDate(), date1.toLocalDate()));
}

@ExecFunction(name = "to_days")
public static Expression toDays(DateLiteral date) {
return new IntegerLiteral((int) date.getDay());
}

@ExecFunction(name = "to_days")
public static Expression toDays(DateV2Literal date) {
return new IntegerLiteral((int) date.getDay());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ public Expression plusYears(long years) {
return fromJavaDateType(toJavaDateType().plusYears(years));
}

public Expression plusHours(long hours) {
return fromJavaDateType(toJavaDateType().plusHours(hours));
}

public Expression plusMinutes(long minutes) {
return fromJavaDateType(toJavaDateType().plusMinutes(minutes));
}

public Expression plusSeconds(long seconds) {
return fromJavaDateType(toJavaDateType().plusSeconds(seconds));
}

// When performing addition or subtraction with MicroSeconds, the precision must
// be set to 6 to display it completely.
public Expression plusMicroSeconds(long microSeconds) {
return DateTimeV2Literal.fromJavaDateType(toJavaDateType().plusNanos(microSeconds * 1000L), 6);
}

public Expression plusMilliSeconds(long microSeconds) {
return plusMicroSeconds(microSeconds * 1000L);
}

public LocalDateTime toJavaDateType() {
return LocalDateTime.of(((int) getYear()), ((int) getMonth()), ((int) getDay()), 0, 0, 0);
}
Expand Down

0 comments on commit 3a187e2

Please sign in to comment.