Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not necessary to defer today(). #1146

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/com/hubspot/jinjava/lib/fn/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,13 @@ public static ZonedDateTime today(String... var) {
);
}
}

ZonedDateTime dateTime = getDateTimeArg(null, zoneOffset);
long currentMillis = JinjavaInterpreter
.getCurrentMaybe()
.map(JinjavaInterpreter::getConfig)
.map(JinjavaConfig::getDateTimeProvider)
.map(DateTimeProvider::getCurrentTimeMillis)
.orElse(System.currentTimeMillis());
ZonedDateTime dateTime = getDateTimeArg(currentMillis, zoneOffset);
return dateTime.toLocalDate().atStartOfDay(zoneOffset);
}

Expand Down
10 changes: 3 additions & 7 deletions src/test/java/com/hubspot/jinjava/lib/fn/TodayFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.InvalidArgumentException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.mode.EagerExecutionMode;
Expand Down Expand Up @@ -65,7 +64,6 @@ public void itIgnoresNullTimezone() {
assertThat(Functions.today((String) null).getZone()).isEqualTo(ZoneOffset.UTC);
}

@Test(expected = DeferredValueException.class)
public void itDefersWhenExecutingEagerly() {
JinjavaInterpreter.pushCurrent(
new JinjavaInterpreter(
Expand All @@ -77,10 +75,8 @@ public void itDefersWhenExecutingEagerly() {
.build()
)
);
try {
Functions.today(ZONE_NAME);
} finally {
JinjavaInterpreter.popCurrent();
}

ZonedDateTime today = Functions.today(ZONE_NAME);
assertThat(today.getYear()).isGreaterThan(2023);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -772,13 +772,6 @@ public void itSplitsResolvedExpression() {
.containsExactlyInAnyOrder("['a', 'b']", "'a'", "'b'");
}

@Test
public void itHandlesToday() {
context.put("foo", "bar");
assertThat(eagerResolveExpression("foo ~ today()").toString())
.isEqualTo("'bar' ~ today()");
}

@Test
public void itHandlesRandom() {
assertThat(eagerResolveExpression("range(1)|random").toString())
Expand Down