
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
com.github.perfectacle:time-machine
Advanced tools
Time travel at the time with various kinds of Java 8 Date/Time API
in Maven
<dependency>
<groupId>com.github.perfectacle</groupId>
<artifactId>time-machine</artifactId>
<version>1.0.0</version>
</dependency>
in Gradle
implementation 'com.github.perfectacle:time-machine:1.0.0'
Suppose you have a code that uses the now() method in the Date/Time API of Java 8.
public class SomeClass {
public static boolean someMethod() {
return someMethod2();
}
private static boolean someMethod2() {
return LocalTime.now().isAfter(LocalTime.of(12, 0, 0));
}
}
Time is out of control.
public class SomeClassTest {
// In only PM, this test passed
@Test
void someMethod() {
assertTrue(SomeClass.someMethod());
}
}
Therefore, meaningless parameters(current time of now) must be passed continuously.
public class SomeClass {
public static boolean someMethod(final LocalTime now) {
return someMethod2(now);
}
private static boolean someMethod2(final LocalTime now) {
return now.isAfter(LocalTime.of(12, 0, 0));
}
}
We can control time only by repeating this meaningless act(passing the parameters).
public class SomeClassTest {
// This test passed in always by passing parameter(current time of now)
@Test
void someMethod() {
assertTrue(SomeClass.someMethod(LocalTime.of(13, 0, 0)));
}
}
If the public method internally calls dozens of private methods, would you pass the current time to the parameter each time?
To eliminate this inefficiency, you should use Time Machine.
public class SomeClass {
public static boolean someMethod() {
return someMethod2();
}
private static boolean someMethod2() {
return TimeMachine.nowOfLocalTime().isAfter(LocalTime.of(12, 0, 0));
}
}
public class SomeClassTest {
// This test passed in always
@Test
void someMethod() {
TimeMachine.travelAt(LocalTime.of(13, 0, 0));
assertTrue(SomeClass.someMethod());
}
}
public class SomeClassTest {
@Test
void whenNotTraveled() {
// Reset to time machine to avoid being affected by other tests.
TimeMachine.reset();
final Instant nowOfInstant = Instant.now();
final Instant nowOfInstantWithTimeMachine = TimeMachine.nowOfInstant();
assertEquals(nowOfInstant.minusNanos(nowOfInstant.getNano()), nowOfInstantWithTimeMachine.minusNanos(nowOfInstantWithTimeMachine.getNano()));
assertEquals(ZonedDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfZonedDateTime().withNano(0));
assertEquals(OffsetDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfOffsetDateTime().withNano(0));
assertEquals(OffsetTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfOffsetTime().withNano(0));
assertEquals(LocalDateTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfLocalDateTime().withNano(0));
assertEquals(LocalDate.now(ZoneOffset.UTC), TimeMachine.nowOfLocalDate());
assertEquals(LocalTime.now(ZoneOffset.UTC).withNano(0), TimeMachine.nowOfLocalTime().withNano(0));
}
}
FAQs
Time travel at the time with various kinds of Java 8 Date/Time API
We found that com.github.perfectacle:time-machine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.