Example usage for org.joda.time DateTimeUtils setCurrentMillisProvider

List of usage examples for org.joda.time DateTimeUtils setCurrentMillisProvider

Introduction

In this page you can find the example usage for org.joda.time DateTimeUtils setCurrentMillisProvider.

Prototype

public static final void setCurrentMillisProvider(MillisProvider millisProvider) throws SecurityException 

Source Link

Document

Sets the provider of the current time to class specified.

Usage

From source file:br.com.caelum.timemachine.TimeMachine.java

License:Apache License

/**
 * Runs the given {@link Block} in the date and time given when constructing
 * the TimeMachine. A {@link DateTime} created inside the given code will
 * have always the same value, equal to the DateTime given when constructing
 * the TimeMachine.//from  w w  w .  j av a2  s  .c  o  m
 *
 * @param code
 *            The code to be run in a different date and time
 * @return The result of running the given code
 */
public <T> T andExecute(Block<T> code) {
    MillisProvider provider = getCurrentTimeMillisProvider();
    DateTimeUtils.setCurrentMillisFixed(somewhereInTime.getMillis());
    try {
        return code.run();
    } finally {
        DateTimeUtils.setCurrentMillisProvider(provider);
    }
}

From source file:gobblin.util.test.RetentionTestDataGenerator.java

License:Apache License

/**
 * Create all the paths listed under {@link #TEST_DATA_CREATE_KEY}. If a path's config has a {@link #TEST_DATA_MOD_TIME_LOCAL_KEY} specified,
 * the modification time of this path is updated to this value.
 *//*from  ww w.ja  va2  s.c om*/
public void setup() throws IOException {

    if (this.setupConfig.hasPath(TEST_CURRENT_TIME_KEY)) {
        DateTimeUtils.setCurrentMillisProvider(new FixedThreadLocalMillisProvider(
                FORMATTER.parseDateTime(setupConfig.getString(TEST_CURRENT_TIME_KEY)).getMillis()));
    }

    List<? extends Config> createConfigs = setupConfig.getConfigList(TEST_DATA_CREATE_KEY);

    Collections.sort(createConfigs, new Comparator<Config>() {
        @Override
        public int compare(Config o1, Config o2) {
            return o1.getString(TEST_DATA_PATH_LOCAL_KEY).compareTo(o2.getString(TEST_DATA_PATH_LOCAL_KEY));
        }
    });

    for (Config fileToCreate : createConfigs) {
        Path fullFilePath = new Path(testTempDirPath,
                PathUtils.withoutLeadingSeparator(new Path(fileToCreate.getString(TEST_DATA_PATH_LOCAL_KEY))));

        if (!this.fs.mkdirs(fullFilePath)) {
            throw new RuntimeException("Failed to create test file " + fullFilePath);
        }
        if (fileToCreate.hasPath(TEST_DATA_MOD_TIME_LOCAL_KEY)) {
            File file = new File(PathUtils.getPathWithoutSchemeAndAuthority(fullFilePath).toString());
            boolean modifiedFile = file.setLastModified(
                    FORMATTER.parseMillis(fileToCreate.getString(TEST_DATA_MOD_TIME_LOCAL_KEY)));
            if (!modifiedFile) {
                throw new IOException(String.format("Unable to set the last modified time for file %s!", file));
            }
        }
        if (fileToCreate.hasPath(TEST_DATA_PERMISSIONS_KEY)) {
            this.fs.setPermission(fullFilePath,
                    new FsPermission(fileToCreate.getString(TEST_DATA_PERMISSIONS_KEY)));
        }
    }
}