Skip to content

Commit

Permalink
Merge pull request #1146 from HubSpot/no-defer-today
Browse files Browse the repository at this point in the history
Not necessary to defer today().
  • Loading branch information
hs-lsong authored Jan 12, 2024
2 parents 6cb5485 + 2d4f109 commit 3f78a3c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
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

0 comments on commit 3f78a3c

Please sign in to comment.