Last active
April 25, 2016 12:27
-
-
Save oc/aab6e336146c753eef5d59e27d1210ad to your computer and use it in GitHub Desktop.
AD til UNIX timestamps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fafafafafafa.useradmin.utils; | |
public class ADUtils { | |
private static final long AD_TO_UNIX_DIFF = 11644473600L; | |
private static final long DESI_NS_TO_SEC = 10000000L; | |
public static final long AD_MIN_TS = 126865728000000000L; | |
public static final long AD_MAX_TS = 9223372036854775807L; | |
/** | |
* AD Timestamps are calculated from 1st Jan 1601 | |
* Unix timestamps (Epoch) is calculated from 1st Jan 1970 | |
* AD Timestamps are in 100 ns precision. (From 100'nanoseconds -> to ms -> to seconds and back to milliseconds) | |
* | |
* @param adTimestamp raw value of AD timestamp | |
* @return Long Instant | |
*/ | |
public static long toInstant(long adTimestamp) { | |
return ((adTimestamp) / DESI_NS_TO_SEC - AD_TO_UNIX_DIFF) * 1000; | |
} | |
/** | |
* Converting instants to AD TIMESTAMPs unfortunately have a precision loss. | |
* | |
* @param instant | |
* @return | |
*/ | |
public static long toADTimestamp(long instant) { | |
return (instant / 1000 + AD_TO_UNIX_DIFF) * DESI_NS_TO_SEC; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment