Android Open Source - timesync Event Calculator






From Project

Back to project page timesync.

License

The source code is released under:

Apache License

If you think the Android project timesync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.tatarka.timesync.lib;
//from   w w w  .  j a v  a 2s  . c  o  m
import java.util.Calendar;
import java.util.GregorianCalendar;

import static me.tatarka.timesync.lib.MathUtil.divCeil;

class EventCalculator {
    /**
     * Calculate when to fire the next event. This should happen at the next timestamp which is a
     * multiple of the given interval since midnight in the current timezone. Both the arguments and
     * the result are in unix time milliseconds (equivalent to {@link System#currentTimeMillis()}).
     *
     * @param currentTime the time to start from, the result will be the next event after this time
     * @param interval    the interval
     * @return the next event time.
     */
    public static long getNextEvent(long currentTime, long interval) {
        if (interval == 0) return currentTime;

        long startTime = getPreviousMidnight(currentTime);
        long span = currentTime - startTime;

        long result;
        if (span == 0) {
            // It's midnight!
            result = startTime + interval;
        } else {
            result = startTime + divCeil(span, interval) * interval;
        }
        return result;
    }

    private static long getPreviousMidnight(long currentTime) {
        Calendar date = new GregorianCalendar();
        date.setTimeInMillis(currentTime);
        date.set(Calendar.HOUR_OF_DAY, 0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        return date.getTimeInMillis();
    }
}




Java Source Code List

me.tatarka.timesync.app.MainActivity.java
me.tatarka.timesync.app.MyApplication.java
me.tatarka.timesync.app.RandomSync.java
me.tatarka.timesync.lib.BooleanFormatException.java
me.tatarka.timesync.lib.EventCalculator.java
me.tatarka.timesync.lib.MathUtil.java
me.tatarka.timesync.lib.ReceiverUtils.java
me.tatarka.timesync.lib.SuperNotCalledException.java
me.tatarka.timesync.lib.TimeSyncBootReceiver.java
me.tatarka.timesync.lib.TimeSyncNetworkReceiver.java
me.tatarka.timesync.lib.TimeSyncParser.java
me.tatarka.timesync.lib.TimeSyncPowerReceiver.java
me.tatarka.timesync.lib.TimeSyncPreferences.java
me.tatarka.timesync.lib.TimeSyncProxy.java
me.tatarka.timesync.lib.TimeSyncService.java
me.tatarka.timesync.lib.TimeSync.java