Coder Social home page Coder Social logo

faketime's Introduction

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.

faketime's People

Contributors

arvindsv avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.