Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2001-2015 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.jdbc.internal.joda.time.chrono;
import java.util.HashMap;
import java.util.Locale;
import com.facebook.presto.jdbc.internal.joda.time.Chronology;
import com.facebook.presto.jdbc.internal.joda.time.DateTimeConstants;
import com.facebook.presto.jdbc.internal.joda.time.DateTimeField;
import com.facebook.presto.jdbc.internal.joda.time.DateTimeZone;
import com.facebook.presto.jdbc.internal.joda.time.DurationField;
import com.facebook.presto.jdbc.internal.joda.time.IllegalFieldValueException;
import com.facebook.presto.jdbc.internal.joda.time.IllegalInstantException;
import com.facebook.presto.jdbc.internal.joda.time.ReadablePartial;
import com.facebook.presto.jdbc.internal.joda.time.field.BaseDateTimeField;
import com.facebook.presto.jdbc.internal.joda.time.field.BaseDurationField;
/**
* Wraps another Chronology to add support for time zones.
*
* ZonedChronology is thread-safe and immutable.
*
* @author Brian S O'Neill
* @author Stephen Colebourne
* @since 1.0
*/
public final class ZonedChronology extends AssembledChronology {
/** Serialization lock */
private static final long serialVersionUID = -1079258847191166848L;
/**
* Avoid calculation errors near zero.
*/
private static final long NEAR_ZERO = 7L * 24 * 60 * 60 * 1000;
/**
* Create a ZonedChronology for any chronology, overriding any time zone it
* may already have.
*
* @param base base chronology to wrap
* @param zone the time zone
* @throws IllegalArgumentException if chronology or time zone is null
*/
public static ZonedChronology getInstance(Chronology base, DateTimeZone zone) {
if (base == null) {
throw new IllegalArgumentException("Must supply a chronology");
}
base = base.withUTC();
if (base == null) {
throw new IllegalArgumentException("UTC chronology must not be null");
}
if (zone == null) {
throw new IllegalArgumentException("DateTimeZone must not be null");
}
return new ZonedChronology(base, zone);
}
static boolean useTimeArithmetic(DurationField field) {
// Use time of day arithmetic rules for unit durations less than
// typical time zone offsets.
return field != null && field.getUnitMillis() < DateTimeConstants.MILLIS_PER_HOUR * 12;
}
/**
* Restricted constructor
*
* @param base base chronology to wrap
* @param zone the time zone
*/
private ZonedChronology(Chronology base, DateTimeZone zone) {
super(base, zone);
}
public DateTimeZone getZone() {
return (DateTimeZone)getParam();
}
public Chronology withUTC() {
return getBase();
}
public Chronology withZone(DateTimeZone zone) {
if (zone == null) {
zone = DateTimeZone.getDefault();
}
if (zone == getParam()) {
return this;
}
if (zone == DateTimeZone.UTC) {
return getBase();
}
return new ZonedChronology(getBase(), zone);
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int millisOfDay)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(year, monthOfYear, dayOfMonth, millisOfDay));
}
public long getDateTimeMillis(int year, int monthOfYear, int dayOfMonth,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(year, monthOfYear, dayOfMonth,
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond));
}
public long getDateTimeMillis(long instant,
int hourOfDay, int minuteOfHour,
int secondOfMinute, int millisOfSecond)
throws IllegalArgumentException
{
return localToUTC(getBase().getDateTimeMillis
(instant + getZone().getOffset(instant),
hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond));
}
/**
* @param localInstant the instant from 1970-01-01T00:00:00 local time
* @return the instant from 1970-01-01T00:00:00Z
*/
private long localToUTC(long localInstant) {
if (localInstant == Long.MAX_VALUE) {
return Long.MAX_VALUE;
} else if (localInstant == Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
DateTimeZone zone = getZone();
int offset = zone.getOffsetFromLocal(localInstant);
long utcInstant = localInstant - offset;
if (localInstant > NEAR_ZERO && utcInstant < 0) {
return Long.MAX_VALUE;
} else if (localInstant < -NEAR_ZERO && utcInstant > 0) {
return Long.MIN_VALUE;
}
int offsetBasedOnUtc = zone.getOffset(utcInstant);
if (offset != offsetBasedOnUtc) {
throw new IllegalInstantException(localInstant, zone.getID());
}
return utcInstant;
}
protected void assemble(Fields fields) {
// Keep a local cache of converted fields so as not to create redundant
// objects.
HashMap