All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Enum.Time Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Enum;

import com.alachisoft.ncache.runtime.util.NCDateTime;

import java.text.ParseException;
import java.util.Date;

/**
 * @author Muneeb Shahid
 */
public enum Time {

    SEC, //second
    mSEC, //milliSecond
    uSEC, //microSecond
    nSEC;   //nanoSecond

    public static java.util.Date ReferenceTime;

    static {
        try {
            ReferenceTime = NCDateTime.getUTCDate(new Date(2016, 1, 1, 0, 0, 0));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param time value to be converted
     * @param unit current unit of time
     * @return
     */
    public static double toNanoSeconds(long time, Time unit) {
        if (unit.equals(Time.SEC)) {
            return time * 1000000000.0;
        } else if (unit.equals(Time.mSEC)) {
            return time * 1000000.0;
        } else if (unit.equals(Time.uSEC)) {
            return time * 1000.0;
        } else {
            return time * 1.0;
        }
    }

    /**
     * @param time value to be converted
     * @param unit current unit of time
     * @return
     */
    public static double toMicroSeconds(long time, Time unit) {
        if (unit.equals(Time.SEC)) {
            return time * 1000000.0;
        } else if (unit.equals(Time.mSEC)) {
            return time * 1000.0;
        } else if (unit.equals(Time.nSEC)) {
            return time / 1000.0;
        } else {
            return time * 1.0;
        }
    }

    /**
     * @param time value to be converted
     * @param unit current unit of time
     * @return
     */
    public static double toMilliSeconds(long time, Time unit) {
        if (unit.equals(Time.SEC)) {
            return time * 1000.0;
        } else if (unit.equals(Time.uSEC)) {
            return time / 1000.0;
        } else if (unit.equals(Time.nSEC)) {
            return time / 1000000.0;
        } else {
            return time * 1.0;
        }
    }

    /**
     * @param time value to be converted
     * @param unit current unit of time
     * @return
     */
    public static double toSeconds(long time, Time unit) {
        if (unit.equals(Time.mSEC)) {
            return time / 1000.0;
        } else if (unit.equals(Time.nSEC)) {
            return time / 1000000000.0;
        } else if (unit.equals(Time.uSEC)) {
            return time / 1000000.0;
        } else {
            return time * 1.0;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy