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@0: public final int getOffset(final int era, final int year, final int month, final int day, michael@0: final int dayOfWeek, final int milliseconds) { michael@0: michael@0: final Calendar cal = Calendar.getInstance(); michael@0: cal.set(Calendar.ERA, era); michael@0: cal.set(Calendar.YEAR, year); michael@0: cal.set(Calendar.MONTH, month); michael@0: cal.set(Calendar.DAY_OF_YEAR, day); michael@0: cal.set(Calendar.DAY_OF_WEEK, dayOfWeek); michael@0: cal.set(Calendar.MILLISECOND, milliseconds); 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@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 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: // per spec, rawoffset is the raw offset at the current date michael@0: final DateTime now = new DateTime(); michael@0: michael@0: List seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD); michael@0: // if no standard time use daylight time.. michael@0: if (seasonalTimes.size() == 0) { michael@0: seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT); michael@0: } michael@0: Observance latestSeasonalTime = null; michael@0: Date latestOnset = null; michael@0: for (int i = 0; i < seasonalTimes.size(); i++) { michael@0: Observance seasonalTime = (Observance) seasonalTimes.get(i); michael@0: Date onset = seasonalTime.getLatestOnset(now); michael@0: if (onset == null) { michael@0: continue; michael@0: } michael@0: if (latestOnset == null || onset.after(latestOnset)) { michael@0: latestOnset = onset; michael@0: latestSeasonalTime = seasonalTime; michael@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@0: }