blob: 63b12fd331b03800129bcd6a1a733917c2c78475 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package se.leap.bitmaskclient.base.utils;
import androidx.annotation.VisibleForTesting;
import java.util.Calendar;
import de.blinkt.openvpn.core.NativeUtils;
public class TimezoneHelper {
public interface TimezoneInterface {
int getCurrentTimezone();
}
private static TimezoneInterface instance = new DefaultTimezoneHelper();
@VisibleForTesting
public TimezoneHelper(TimezoneInterface timezoneInterface) {
if (!NativeUtils.isUnitTest()) {
throw new IllegalStateException("TimezoneHelper injected with timezoneInterface outside of an unit test");
}
instance = timezoneInterface;
}
public static TimezoneInterface get() {
return instance;
}
public static int timezoneDistance(int localTimezone, int remoteTimezone) { // Distance along the numberline of Prime Meridian centric, assumes UTC-11 through UTC+12
int dist = Math.abs(localTimezone - remoteTimezone);
// Farther than 12 timezones and it's shorter around the "back"
if (dist > 12)
dist = 12 - (dist - 12); // Well i'll be. Absolute values make equations do funny things.
return dist;
}
public static int getCurrentTimezone() {
return get().getCurrentTimezone();
}
private static class DefaultTimezoneHelper implements TimezoneInterface {
@Override
public int getCurrentTimezone() {
return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
}
}
}
|