blob: c543368d233f11aad97e7a30786167f09bea777c (
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
|
package se.leap.bitmaskclient.base.utils;
import androidx.annotation.VisibleForTesting;
import java.util.Calendar;
public class TimezoneHelper {
public interface TimezoneInterface {
int getCurrentTimezone();
}
private static TimezoneInterface instance = new DefaultTimezoneHelper();
@VisibleForTesting
public TimezoneHelper(TimezoneInterface timezoneInterface) {
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();
}
public static class DefaultTimezoneHelper implements TimezoneInterface {
@Override
public int getCurrentTimezone() {
return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
}
}
}
|