michael@0: /** michael@0: * Copyright (c) 2012, Ben Fortuna michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * michael@0: * o Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * michael@0: * o Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * o Neither the name of Ben Fortuna nor the names of any other contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: package net.fortuna.ical4j.model; michael@0: michael@0: import java.util.Calendar; michael@0: import java.util.Collections; michael@0: import java.util.Date; michael@0: import java.util.List; michael@0: michael@0: import net.fortuna.ical4j.model.component.Daylight; michael@0: import net.fortuna.ical4j.model.component.Observance; michael@0: import net.fortuna.ical4j.model.component.VTimeZone; michael@0: import net.fortuna.ical4j.model.property.TzId; michael@0: import net.fortuna.ical4j.model.property.TzOffsetTo; michael@0: michael@0: /** michael@0: * $Id$ michael@0: * michael@0: * Created on 13/09/2005 michael@0: * michael@0: * A Java timezone implementation based on an underlying VTimeZone michael@0: * definition. michael@0: * @author Ben Fortuna michael@0: */ michael@0: public class TimeZone extends java.util.TimeZone { michael@0: michael@0: private static final long serialVersionUID = -5620979316746547234L; michael@0: michael@0: private final VTimeZone vTimeZone; michael@0: private final int rawOffset; michael@0: michael@0: /** michael@0: * Constructs a new instance based on the specified VTimeZone. michael@0: * @param vTimeZone a VTIMEZONE object instance michael@0: */ michael@0: public TimeZone(final VTimeZone vTimeZone) { michael@0: this.vTimeZone = vTimeZone; michael@0: final TzId tzId = (TzId) vTimeZone.getProperty(Property.TZID); michael@0: setID(tzId.getValue()); michael@0: this.rawOffset = getRawOffset(vTimeZone); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@3: public final int getOffset(final int era, final int year, final int month, final int dayOfMonth, michael@0: final int dayOfWeek, final int milliseconds) { michael@3: michael@3: // calculate time of day michael@3: int ms = milliseconds; michael@3: final int hour = ms / 3600000; michael@3: ms -= hour*3600000; michael@3: final int minute = ms / 60000; michael@3: ms -= minute*60000; michael@3: final int second = ms / 1000; michael@3: ms -= second*1000; michael@3: michael@3: final Calendar cal = Calendar.getInstance(); michael@3: cal.clear(); // don't retain current date/time, it may disturb the calculation michael@0: michael@3: // set date and time michael@0: cal.set(Calendar.ERA, era); michael@0: cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); michael@3: cal.set(year, month, dayOfMonth, hour, minute, second); michael@3: cal.set(Calendar.MILLISECOND, ms); michael@3: michael@0: final Observance observance = vTimeZone.getApplicableObservance(new DateTime(cal.getTime())); michael@0: if (observance != null) { michael@0: final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO); michael@0: return (int) offset.getOffset().getOffset(); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public int getOffset(long date) { michael@0: final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date)); michael@0: if (observance != null) { michael@0: final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO); michael@3: if (offset.getOffset().getOffset() < getRawOffset()) { michael@3: return getRawOffset(); michael@3: } michael@3: else { michael@3: return (int) offset.getOffset().getOffset(); michael@3: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final int getRawOffset() { michael@0: return rawOffset; michael@0: } michael@0: michael@0: /** michael@0: * Determines if the specified date is in daylight time according to michael@0: * this timezone. This is done by finding the latest supporting michael@0: * observance for the specified date and identifying whether it is michael@0: * daylight time. michael@0: * @param date a date instance michael@0: * @return true if the specified date is in daylight time, otherwise false michael@0: */ michael@0: public final boolean inDaylightTime(final Date date) { michael@0: final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date)); michael@0: return (observance != null && observance instanceof Daylight); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final void setRawOffset(final int offsetMillis) { michael@0: throw new UnsupportedOperationException("Updates to the VTIMEZONE object must be performed directly"); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: public final boolean useDaylightTime() { michael@0: final ComponentList daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT); michael@0: return (!daylights.isEmpty()); michael@0: } michael@0: michael@0: /** michael@0: * @return Returns the VTimeZone backing this instance. michael@0: */ michael@0: public final VTimeZone getVTimeZone() { michael@0: return vTimeZone; michael@0: } michael@0: michael@0: private static final int getRawOffset(VTimeZone vt) { michael@0: michael@0: List seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD); michael@0: // if no standard time use daylight time.. michael@3: if (seasonalTimes.isEmpty()) { michael@0: seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT); michael@3: if (seasonalTimes.isEmpty()) { michael@3: return 0; michael@3: } michael@0: } michael@0: Observance latestSeasonalTime = null; michael@3: if (seasonalTimes.size() > 1) { michael@3: // per java spec and when dealing with historical time, michael@3: // rawoffset is the raw offset at the current date michael@3: final DateTime now = new DateTime(); michael@3: Date latestOnset = null; michael@3: for (int i = 0; i < seasonalTimes.size(); i++) { michael@3: Observance seasonalTime = (Observance) seasonalTimes.get(i); michael@3: Date onset = seasonalTime.getLatestOnset(now); michael@3: if (onset == null) { michael@3: continue; michael@3: } michael@3: if (latestOnset == null || onset.after(latestOnset)) { michael@3: latestOnset = onset; michael@3: latestSeasonalTime = seasonalTime; michael@3: } michael@0: } michael@3: } else { michael@3: latestSeasonalTime = (Observance)seasonalTimes.get(0); michael@0: } michael@0: if (latestSeasonalTime != null) { michael@0: final TzOffsetTo offsetTo = (TzOffsetTo) latestSeasonalTime.getProperty(Property.TZOFFSETTO); michael@0: if (offsetTo != null) { michael@0: return (int) offsetTo.getOffset().getOffset(); michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@3: michael@3: public boolean equals(Object o) { michael@3: if (this == o) return true; michael@3: if (o == null || getClass() != o.getClass()) return false; michael@3: michael@3: TimeZone timeZone = (TimeZone) o; michael@3: michael@3: if (rawOffset != timeZone.rawOffset) return false; michael@3: if (vTimeZone != null ? !vTimeZone.equals(timeZone.vTimeZone) : timeZone.vTimeZone != null) return false; michael@3: michael@3: return true; michael@3: } michael@3: michael@3: public int hashCode() { michael@3: int result = vTimeZone != null ? vTimeZone.hashCode() : 0; michael@3: result = 31 * result + rawOffset; michael@3: return result; michael@3: } michael@0: }