Skip to content
forked from arvindsv/faketime

Java agent to enable changing time in a Java project, mainly for testing.

Notifications You must be signed in to change notification settings

asleung/faketime

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Faketime

This native Java Agent allows you to change the time in a Java program, without affecting the system clock. It allows control over the System.currentTimeMillis() method, which is used by most other time-related functionality provided by the JVM.

How to use it

The easy way - For the Mac

  • Download libfaketime.jnilib into some directory, say, /path/to/libfaketime.jnilib.

  • Run your Java program (say, org.test.Main) with the agent-specific extra arguments (see issue #3), like this:

      java -agentpath:/path/to/libfaketime.jnilib \
        -XX:+UnlockDiagnosticVMOptions \
        -XX:DisableIntrinsic=_currentTimeMillis \
        -XX:CompileCommand=exclude,java/lang/System.currentTimeMillis \
        org.test.Main
    
  • In your Java code, you can set the property faketime.offset.seconds to the number of seconds you want the time altered by. For example, to add a day, you can do something like this:

      System.setProperty("faketime.offset.seconds", "86400");
    
  • That's it! Take a look at FakeTimeTest.java if you need to see some Java code which uses it.

The "hard" way - For the Mac

  • Clone this repo

  • Run this:

      gcc -shared -I $JAVA_HOME/include -Wall src/FakeTimeAgent.c -o libfaketime.jnilib
    
  • This will create libfaketime.jnilib in the current directory. Run your Java program (say, org.test.Main) with the agent-specific extra arguments, like this:

      java -agentpath:/path/to/libfaketime.jnilib \
        -XX:+UnlockDiagnosticVMOptions \
        -XX:DisableIntrinsic=_currentTimeMillis \
        -XX:CompileCommand=exclude,java/lang/System.currentTimeMillis \
        org.test.Main
    

    or, if the current directory has libfaketime.jnilib, you can use:

      java -agentlib:faketime \
        -XX:+UnlockDiagnosticVMOptions \
        -XX:DisableIntrinsic=_currentTimeMillis \
        -XX:CompileCommand=exclude,java/lang/System.currentTimeMillis org.test.Main
    
  • In your Java code, you can set the property faketime.offset.seconds to the number of seconds you want the time altered by. For example, to add a day, you can do something like this:

      System.setProperty("faketime.offset.seconds", "86400");
    
  • That's it! Take a look at FakeTimeTest.java if you need to see some Java code which uses it.

About

Java agent to enable changing time in a Java project, mainly for testing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 81.1%
  • Shell 11.9%
  • Java 7.0%