package sun.util.calendar;
import java.io.ByteArrayInputStream;
import java.io.BufferedInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.SimpleTimeZone;
import java.util.concurrent.ConcurrentHashMap;
import java.util.zip.CRC32;
import sun.security.action.GetPropertyAction;
public final class ZoneInfoFile {
public static String[] getZoneIds() {
int len = regions.length + oldMappings.length;
if (!USE_OLDMAPPING) {
len += 3;
}
String[] ids = Arrays.copyOf(regions, len);
int i = regions.length;
if (!USE_OLDMAPPING) {
ids[i++] = "EST";
ids[i++] = "HST";
ids[i++] = "MST";
}
for (int j = 0; j < oldMappings.length; j++) {
ids[i++] = oldMappings[j][0];
}
return ids;
}
public static String[] getZoneIds(int rawOffset) {
List<String> ids = new ArrayList<>();
for (String id : getZoneIds()) {
ZoneInfo zi = getZoneInfo(id);
if (zi.getRawOffset() == rawOffset) {
ids.add(id);
}
}
String[] list = ids.toArray(new String[ids.size()]);
Arrays.sort(list);
return list;
}
public static ZoneInfo getZoneInfo(String zoneId) {
if (zoneId == null) {
return null;
}
ZoneInfo zi = getZoneInfo0(zoneId);
if (zi != null) {
zi = (ZoneInfo)zi.clone();
zi.setID(zoneId);
}
return zi;
}
private static ZoneInfo getZoneInfo0(String zoneId) {
try {
ZoneInfo zi = zones.get(zoneId);
if (zi != null) {
return zi;
}
String zid = zoneId;
if (aliases.containsKey(zoneId)) {
zid = aliases.get(zoneId);
}
int index = Arrays.binarySearch(regions, zid);
if (index < 0) {
return null;
}
byte[] bytes = ruleArray[indices[index]];
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
zi = getZoneInfo(dis, zid);
zones.put(zoneId, zi);
return zi;
} catch (Exception ex) {
throw new RuntimeException("Invalid binary time-zone data: TZDB:" +
zoneId + ", version: " + versionId, ex);
}
}
public static Map<String, String> getAliasMap() {
return Collections.unmodifiableMap(aliases);
}
public static String getVersion() {
return versionId;
}
public static ZoneInfo getCustomTimeZone(String originalId, int gmtOffset) {
String id = toCustomID(gmtOffset);
return new ZoneInfo(id, gmtOffset);
}
public static String toCustomID(int gmtOffset) {
char sign;
int offset = gmtOffset / 60000;
if (offset >= 0) {
sign = '+';
} else {
sign = '-';
offset = -offset;
}
int hh = offset / 60;
int mm = offset % 60;
char[] buf = new char[] { 'G', 'M', 'T', sign, '0', '0', ':', '0', '0' };
if (hh >= 10) {
buf[4] += hh / 10;
}
buf[5] += hh % 10;
if (mm != 0) {
buf[7] += mm / 10;
buf[8] += mm % 10;
}
return new String(buf);
}
private ZoneInfoFile() {
}
private static String versionId;
private final static Map<String, ZoneInfo> zones = new ConcurrentHashMap<>();
private static Map<String, String> aliases = new HashMap<>();
private static byte[][] ruleArray;
private static String[] regions;
private static int[] indices;
private static final boolean USE_OLDMAPPING;
private static String[][] oldMappings = new String[][] {
{ "ACT", "Australia/Darwin" },
{ "AET", "Australia/Sydney" },
{ "AGT", "America/Argentina/Buenos_Aires" },
{ "ART", "Africa/Cairo" },
{ "AST", "America/Anchorage" },
{ "BET", "America/Sao_Paulo" },
{ "BST", "Asia/Dhaka" },
{ "CAT", "Africa/Harare" },
{ "CNT", "America/St_Johns" },
{ "CST", "America/Chicago" },
{ "CTT", "Asia/Shanghai" },
{ "EAT", "Africa/Addis_Ababa" },
{ "ECT", "Europe/Paris" },
{ "IET", "America/Indiana/Indianapolis" },
{ "IST", "Asia/Kolkata" },
{ "JST", "Asia/Tokyo" },
{ "MIT", "Pacific/Apia" },
{ "NET", "Asia/Yerevan" },
{ "NST", "Pacific/Auckland" },
{ "PLT", "Asia/Karachi" },
{ "PNT", "America/Phoenix" },
{ "PRT", "America/Puerto_Rico" },
{ "PST", "America/Los_Angeles" },
{ "SST", "Pacific/Guadalcanal" },
{ "VST", "Asia/Ho_Chi_Minh" },
};
static {
String oldmapping = AccessController.doPrivileged(
new GetPropertyAction("sun.timezone.ids.oldmapping", "false")).toLowerCase(Locale.ROOT);
USE_OLDMAPPING = (oldmapping.equals("yes") || oldmapping.equals("true"));
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
try {
String libDir = System.getProperty("java.home") + File.separator + "lib";
try (DataInputStream dis = new DataInputStream(
new BufferedInputStream(new FileInputStream(
new File(libDir, "tzdb.dat"))))) {
load(dis);
}
} catch (Exception x) {
throw new Error(x);
}
return null;
}
});
}
private static void addOldMapping() {
for (String[] alias : oldMappings) {
aliases.put(alias[0], alias[1]);
}
if (USE_OLDMAPPING) {
aliases.put("EST", "America/New_York");
aliases.put("MST", "America/Denver");
aliases.put("HST", "Pacific/Honolulu");
} else {
zones.put("EST", new ZoneInfo("EST", -18000000));
zones.put("MST", new ZoneInfo("MST", -25200000));
zones.put("HST", new ZoneInfo("HST", -36000000));
}
}
public static boolean useOldMapping() {
return USE_OLDMAPPING;
}
private static void load(DataInputStream dis) throws ClassNotFoundException, IOException {
if (dis.readByte() != 1) {
throw new StreamCorruptedException("File format not recognised");
}
String groupId = dis.readUTF();
if ("TZDB".equals(groupId) == false) {
throw new StreamCorruptedException("File format not recognised");
}
int versionCount = dis.readShort();
for (int i = 0; i < versionCount; i++) {
versionId = dis.readUTF();
}
int regionCount = dis.readShort();
String[] regionArray = new String[regionCount];
for (int i = 0; i < regionCount; i++) {
regionArray[i] = dis.readUTF();
}
int ruleCount = dis.readShort();
ruleArray = new byte[ruleCount][];
for (int i = 0; i < ruleCount; i++) {
byte[] bytes = new byte[dis.readShort()];
dis.readFully(bytes);
ruleArray[i] = bytes;
}
for (int i = 0; i < versionCount; i++) {
regionCount = dis.readShort();
regions = new String[regionCount];
indices = new int[regionCount];
for (int j = 0; j < regionCount; j++) {
regions[j] = regionArray[dis.readShort()];
indices[j] = dis.readShort();
}
}
zones.remove("ROC");
for (int i = 0; i < versionCount; i++) {
int aliasCount = dis.readShort();
aliases.clear();
for (int j = 0; j < aliasCount; j++) {
String alias = regionArray[dis.readShort()];
String region = regionArray[dis.readShort()];
aliases.put(alias, region);
}
}
addOldMapping();
}
public static ZoneInfo getZoneInfo(DataInput in, String zoneId) throws Exception {
byte type = in.readByte();
int stdSize = in.readInt();
long[] stdTrans = new long[stdSize];
for (int i = 0; i < stdSize; i++) {
stdTrans[i] = readEpochSec(in);
}
int [] stdOffsets = new int[stdSize + 1];
for (int i = 0; i < stdOffsets.length; i++) {
stdOffsets[i] = readOffset(in);
}
int savSize = in.readInt();
long[] savTrans = new long[savSize];
for (int i = 0; i < savSize; i++) {
savTrans[i] = readEpochSec(in);
}
int[] savOffsets = new int[savSize + 1];
for (int i = 0; i < savOffsets.length; i++) {
savOffsets[i] = readOffset(in);
}
int ruleSize = in.readByte();
ZoneOffsetTransitionRule[] rules = new ZoneOffsetTransitionRule[ruleSize];
for (int i = 0; i < ruleSize; i++) {
rules[i] = new ZoneOffsetTransitionRule(in);
}
return getZoneInfo(zoneId, stdTrans, stdOffsets, savTrans, savOffsets, rules);
}
public static int readOffset(DataInput in) throws IOException {
int offsetByte = in.readByte();
return offsetByte == 127 ? in.readInt() : offsetByte * 900;
}
static long readEpochSec(DataInput in) throws IOException {
int hiByte = in.readByte() & 255;
if (hiByte == 255) {
return in.readLong();
} else {
int midByte = in.readByte() & 255;
int loByte = in.readByte() & 255;
long tot = ((hiByte << 16) + (midByte << 8) + loByte);
return (tot * 900) - 4575744000L;
}
}
private static final long UTC1900 = -2208988800L;
private static final long UTC2037 = 2145916799L;
private static final long LDT2037 = 2114380800L;
private static final long CURRT = System.currentTimeMillis()/1000;
private static ZoneInfo getZoneInfo(String zoneId,
long[] standardTransitions,
int[] standardOffsets,
long[] savingsInstantTransitions,
int[] wallOffsets,
ZoneOffsetTransitionRule[] lastRules) {
int rawOffset = 0;
int dstSavings = 0;
int checksum = 0;
int[] params = null;
boolean willGMTOffsetChange = false;
if (standardTransitions.length > 0) {
rawOffset = standardOffsets[standardOffsets.length - 1] * 1000;
willGMTOffsetChange = standardTransitions[standardTransitions.length - 1] > CURRT;
}
else
rawOffset = standardOffsets[0] * 1000;
long[] transitions = null;
int[] offsets = null;
int nOffsets = 0;
int nTrans = 0;
if (savingsInstantTransitions.length != 0) {
transitions = new long[250];
offsets = new int[100];
int lastyear = getYear(savingsInstantTransitions[savingsInstantTransitions.length - 1],
wallOffsets[savingsInstantTransitions.length - 1]);
int i = 0, k = 1;
while (i < savingsInstantTransitions.length &&
savingsInstantTransitions[i] < UTC1900) {
i++;
}
if (i < savingsInstantTransitions.length) {
if (i < savingsInstantTransitions.length) {
offsets[0] = standardOffsets[standardOffsets.length - 1] * 1000;
nOffsets = 1;
}
nOffsets = addTrans(transitions, nTrans++,
offsets, nOffsets,
UTC1900,
wallOffsets[i],
getStandardOffset(standardTransitions, standardOffsets, UTC1900));
}
for (; i < savingsInstantTransitions.length; i++) {
long trans = savingsInstantTransitions[i];
if (trans > UTC2037) {
lastyear = LASTYEAR;
break;
}
while (k < standardTransitions.length) {
long trans_s = standardTransitions[k];
if (trans_s >= UTC1900) {
if (trans_s > trans)
break;
if (trans_s < trans) {
if (nOffsets + 2 >= offsets.length) {
offsets = Arrays.copyOf(offsets, offsets.length + 100);
}
if (nTrans + 1 >= transitions.length) {
transitions = Arrays.copyOf(transitions, transitions.length + 100);
}
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
trans_s,
wallOffsets[i],
standardOffsets[k+1]);
}
}
k++;
}
if (nOffsets + 2 >= offsets.length) {
offsets = Arrays.copyOf(offsets, offsets.length + 100);
}
if (nTrans + 1 >= transitions.length) {
transitions = Arrays.copyOf(transitions, transitions.length + 100);
}
nOffsets = addTrans(transitions, nTrans++, offsets, nOffsets,
trans,
wallOffsets[i + 1],
getStandardOffset(standardTransitions, standardOffsets, trans));
}
while (k < standardTransitions.length) {
long trans = standardTransitions[k];
if (trans >= UTC1900) {
int offset = wallOffsets[i];
int offsetIndex = indexOf(offsets, 0, nOffsets, offset);
if (offsetIndex == nOffsets)
nOffsets++;
transitions[nTrans++] = ((trans * 1000) << TRANSITION_NSHIFT) |
(offsetIndex & OFFSET_MASK);
}
k++;
}
if (lastRules.length > 1) {
while (lastyear++ < LASTYEAR) {
for (ZoneOffsetTransitionRule zotr : lastRules) {
long trans = zotr.getTransitionEpochSecond(lastyear);
if (nOffsets + 2 >= offsets.length) {
offsets = Arrays.copyOf(offsets, offsets.length + 100);
}
if (nTrans + 1 >= transitions.length) {
transitions = Arrays.copyOf(transitions, transitions.length + 100);
}
nOffsets = addTrans(transitions, nTrans++,
offsets, nOffsets,
trans,
zotr.offsetAfter,
zotr.standardOffset);
}
}
ZoneOffsetTransitionRule startRule = lastRules[lastRules.length - 2];
ZoneOffsetTransitionRule endRule = lastRules[lastRules.length - 1];
params = new int[10];
if (startRule.offsetAfter - startRule.offsetBefore < 0 &&
endRule.offsetAfter - endRule.offsetBefore > 0) {
ZoneOffsetTransitionRule tmp;
tmp = startRule;
startRule = endRule;
endRule = tmp;
}
params[0] = startRule.month - 1;
int dom = startRule.dom;
int dow = startRule.dow;
if (dow == -1) {
params[1] = dom;
params[2] = 0;
} else {
if (dom < 0 || dom >= 24) {
params[1] = -1;
params[2] = toCalendarDOW[dow];
} else {
params[1] = dom;
params[2] = -toCalendarDOW[dow];
}
}
params[3] = startRule.secondOfDay * 1000;
params[4] = toSTZTime[startRule.timeDefinition];
params[5] = endRule.month - 1;
dom = endRule.dom;
dow = endRule.dow;
if (dow == -1) {
params[6] = dom;
params[7] = 0;
} else {
if (dom < 0 || dom >= 24) {
params[6] = -1;
params[7] = toCalendarDOW[dow];
} else {
params[6] = dom;
params[7] = -toCalendarDOW[dow];
}
}
params[8] = endRule.secondOfDay * 1000;
params[9] = toSTZTime[endRule.timeDefinition];
dstSavings = (startRule.offsetAfter - startRule.offsetBefore) * 1000;
if (params[2] == 6 && params[3] == 0 &&
(zoneId.equals("Asia/Amman"))) {
params[2] = 5;
params[3] = 86400000;
}
if (params[2] == 7 && params[3] == 0 &&
(zoneId.equals("Asia/Amman"))) {
params[2] = 6;
params[3] = 86400000;
}
if (params[7] == 6 && params[8] == 0 &&
(zoneId.equals("Africa/Cairo"))) {
params[7] = 5;
params[8] = 86400000;
}
} else if (nTrans > 0) {
if (lastyear < LASTYEAR) {
long trans = LDT2037 - rawOffset/1000;
int offsetIndex = indexOf(offsets, 0, nOffsets, rawOffset/1000);
if (offsetIndex == nOffsets)
nOffsets++;
transitions[nTrans++] = (trans * 1000) << TRANSITION_NSHIFT |
(offsetIndex & OFFSET_MASK);
} else if (savingsInstantTransitions.length > 2) {
int m = savingsInstantTransitions.length;
long startTrans = savingsInstantTransitions[m - 2];
int startOffset = wallOffsets[m - 2 + 1];
int startStd = getStandardOffset(standardTransitions, standardOffsets, startTrans);
long endTrans = savingsInstantTransitions[m - 1];
int endOffset = wallOffsets[m - 1 + 1];
int endStd = getStandardOffset(standardTransitions, standardOffsets, endTrans);
if (startOffset > startStd && endOffset == endStd) {
m = savingsInstantTransitions.length - 2;
ZoneOffset before = ZoneOffset.ofTotalSeconds(wallOffsets[m]);
ZoneOffset after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]);
LocalDateTime ldt = LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before);
LocalDateTime startLDT;
if (after.getTotalSeconds() > before.getTotalSeconds()) {
startLDT = ldt;
} else {
startLDT = ldt.plusSeconds(wallOffsets[m + 1] - wallOffsets[m]);
}
m = savingsInstantTransitions.length - 1;
before = ZoneOffset.ofTotalSeconds(wallOffsets[m]);
after = ZoneOffset.ofTotalSeconds(wallOffsets[m + 1]);
ldt = LocalDateTime.ofEpochSecond(savingsInstantTransitions[m], 0, before);
LocalDateTime endLDT;
if (after.getTotalSeconds() > before.getTotalSeconds()) {
endLDT = ldt.plusSeconds(wallOffsets[m + 1] - wallOffsets[m]);
} else {
endLDT = ldt;
}
params = new int[10];
params[0] = startLDT.getMonthValue() - 1;
params[1] = startLDT.getDayOfMonth();
params[2] = 0;
params[3] = startLDT.toLocalTime().toSecondOfDay() * 1000;
params[4] = SimpleTimeZone.WALL_TIME;
params[5] = endLDT.getMonthValue() - 1;
params[6] = endLDT.getDayOfMonth();
params[7] = 0;
params[8] = endLDT.toLocalTime().toSecondOfDay() * 1000;
params[9] = SimpleTimeZone.WALL_TIME;
dstSavings = (startOffset - startStd) * 1000;
}
}
}
if (transitions != null && transitions.length != nTrans) {
if (nTrans == 0) {
transitions = null;
} else {
transitions = Arrays.copyOf(transitions, nTrans);
}
}
if (offsets != null && offsets.length != nOffsets) {
if (nOffsets == 0) {
offsets = null;
} else {
offsets = Arrays.copyOf(offsets, nOffsets);
}
}
if (transitions != null) {
Checksum sum = new Checksum();
for (i = 0; i < transitions.length; i++) {
long val = transitions[i];
int dst = (int)((val >>> DST_NSHIFT) & 0xfL);
int saving = (dst == 0) ? 0 : offsets[dst];
int index = (int)(val & OFFSET_MASK);
int offset = offsets[index];
long second = (val >> TRANSITION_NSHIFT);
sum.update(second + index);
sum.update(index);
sum.update(dst == 0 ? -1 : dst);
}
checksum = (int)sum.getValue();
}
}
return new ZoneInfo(zoneId, rawOffset, dstSavings, checksum, transitions,
offsets, params, willGMTOffsetChange);
}
private static int getStandardOffset(long[] standardTransitions,
int[] standardOffsets,
long epochSec) {
int index = 0;
for (; index < standardTransitions.length; index++) {
if (epochSec < standardTransitions[index]) {
break;
}
}
return standardOffsets[index];
}
static final int SECONDS_PER_DAY = 86400;
static final int DAYS_PER_CYCLE = 146097;
static final long DAYS_0000_TO_1970 = (DAYS_PER_CYCLE * 5L) - (30L * 365L + 7L);
private static int getYear(long epochSecond, int offset) {
long second = epochSecond + offset;
long epochDay = Math.floorDiv(second, SECONDS_PER_DAY);
long zeroDay = epochDay + DAYS_0000_TO_1970;
zeroDay -= 60;
long adjust = 0;
if (zeroDay < 0) {
long adjustCycles = (zeroDay + 1) / DAYS_PER_CYCLE - 1;
adjust = adjustCycles * 400;
zeroDay += -adjustCycles * DAYS_PER_CYCLE;
}
long yearEst = (400 * zeroDay + 591) / DAYS_PER_CYCLE;
long doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
if (doyEst < 0) {
yearEst--;
doyEst = zeroDay - (365 * yearEst + yearEst / 4 - yearEst / 100 + yearEst / 400);
}
yearEst += adjust;
int marchDoy0 = (int) doyEst;
int marchMonth0 = (marchDoy0 * 5 + 2) / 153;
int month = (marchMonth0 + 2) % 12 + 1;
int dom = marchDoy0 - (marchMonth0 * 306 + 5) / 10 + 1;
yearEst += marchMonth0 / 10;
return (int)yearEst;
}
private static final int toCalendarDOW[] = new int[] {
-1,
Calendar.MONDAY,
Calendar.TUESDAY,
Calendar.WEDNESDAY,
Calendar.THURSDAY,
Calendar.FRIDAY,
Calendar.SATURDAY,
Calendar.SUNDAY
};
private static final int toSTZTime[] = new int[] {
SimpleTimeZone.UTC_TIME,
SimpleTimeZone.WALL_TIME,
SimpleTimeZone.STANDARD_TIME,
};
private static final long OFFSET_MASK = 0x0fL;
private static final long DST_MASK = 0xf0L;
private static final int DST_NSHIFT = 4;
private static final int TRANSITION_NSHIFT = 12;
private static final int LASTYEAR = 2037;
private static int indexOf(int[] offsets, int from, int nOffsets, int offset) {
offset *= 1000;
for (; from < nOffsets; from++) {
if (offsets[from] == offset)
return from;
}
offsets[from] = offset;
return from;
}
private static int addTrans(long transitions[], int nTrans,
int offsets[], int nOffsets,
long trans, int offset, int stdOffset) {
int offsetIndex = indexOf(offsets, 0, nOffsets, offset);
if (offsetIndex == nOffsets)
nOffsets++;
int dstIndex = 0;
if (offset != stdOffset) {
dstIndex = indexOf(offsets, 1, nOffsets, offset - stdOffset);
if (dstIndex == nOffsets)
nOffsets++;
}
transitions[nTrans] = ((trans * 1000) << TRANSITION_NSHIFT) |
((dstIndex << DST_NSHIFT) & DST_MASK) |
(offsetIndex & OFFSET_MASK);
return nOffsets;
}
private static class Checksum extends CRC32 {
public void update(int val) {
byte[] b = new byte[4];
b[0] = (byte)(val >>> 24);
b[1] = (byte)(val >>> 16);
b[2] = (byte)(val >>> 8);
b[3] = (byte)(val);
update(b);
}
void update(long val) {
byte[] b = new byte[8];
b[0] = (byte)(val >>> 56);
b[1] = (byte)(val >>> 48);
b[2] = (byte)(val >>> 40);
b[3] = (byte)(val >>> 32);
b[4] = (byte)(val >>> 24);
b[5] = (byte)(val >>> 16);
b[6] = (byte)(val >>> 8);
b[7] = (byte)(val);
update(b);
}
}
private static class ZoneOffsetTransitionRule {
private final int month;
private final byte dom;
private final int dow;
private final int secondOfDay;
private final boolean timeEndOfDay;
private final int timeDefinition;
private final int standardOffset;
private final int offsetBefore;
private final int offsetAfter;
ZoneOffsetTransitionRule(DataInput in) throws IOException {
int data = in.readInt();
int dowByte = (data & (7 << 19)) >>> 19;
int timeByte = (data & (31 << 14)) >>> 14;
int stdByte = (data & (255 << 4)) >>> 4;
int beforeByte = (data & (3 << 2)) >>> 2;
int afterByte = (data & 3);
this.month = data >>> 28;
this.dom = (byte)(((data & (63 << 22)) >>> 22) - 32);
this.dow = dowByte == 0 ? -1 : dowByte;
this.secondOfDay = timeByte == 31 ? in.readInt() : timeByte * 3600;
this.timeEndOfDay = timeByte == 24;
this.timeDefinition = (data & (3 << 12)) >>> 12;
this.standardOffset = stdByte == 255 ? in.readInt() : (stdByte - 128) * 900;
this.offsetBefore = beforeByte == 3 ? in.readInt() : standardOffset + beforeByte * 1800;
this.offsetAfter = afterByte == 3 ? in.readInt() : standardOffset + afterByte * 1800;
}
long getTransitionEpochSecond(int year) {
long epochDay = 0;
if (dom < 0) {
epochDay = toEpochDay(year, month, lengthOfMonth(year, month) + 1 + dom);
if (dow != -1) {
epochDay = previousOrSame(epochDay, dow);
}
} else {
epochDay = toEpochDay(year, month, dom);
if (dow != -1) {
epochDay = nextOrSame(epochDay, dow);
}
}
if (timeEndOfDay) {
epochDay += 1;
}
int difference = 0;
switch (timeDefinition) {
case 0:
difference = 0;
break;
case 1:
difference = -offsetBefore;
break;
case 2:
difference = -standardOffset;
break;
}
return epochDay * 86400 + secondOfDay + difference;
}
static final boolean isLeapYear(int year) {
return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
}
static final int lengthOfMonth(int year, int month) {
switch (month) {
case 2:
return isLeapYear(year)? 29 : 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
static final long toEpochDay(int year, int month, int day) {
long y = year;
long m = month;
long total = 0;
total += 365 * y;
if (y >= 0) {
total += (y + 3) / 4 - (y + 99) / 100 + (y + 399) / 400;
} else {
total -= y / -4 - y / -100 + y / -400;
}
total += ((367 * m - 362) / 12);
total += day - 1;
if (m > 2) {
total--;
if (!isLeapYear(year)) {
total--;
}
}
return total - DAYS_0000_TO_1970;
}
static final long previousOrSame(long epochDay, int dayOfWeek) {
return adjust(epochDay, dayOfWeek, 1);
}
static final long nextOrSame(long epochDay, int dayOfWeek) {
return adjust(epochDay, dayOfWeek, 0);
}
static final long adjust(long epochDay, int dow, int relative) {
int calDow = (int)Math.floorMod(epochDay + 3, 7L) + 1;
if (relative < 2 && calDow == dow) {
return epochDay;
}
if ((relative & 1) == 0) {
int daysDiff = calDow - dow;
return epochDay + (daysDiff >= 0 ? 7 - daysDiff : -daysDiff);
} else {
int daysDiff = dow - calDow;
return epochDay - (daysDiff >= 0 ? 7 - daysDiff : -daysDiff);
}
}
}
}