src/net/fortuna/ical4j/model/TimeZone.java

changeset 0
fb9019fb1bf7
child 3
73bdfa70b04e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/net/fortuna/ical4j/model/TimeZone.java	Tue Feb 10 18:12:00 2015 +0100
     1.3 @@ -0,0 +1,177 @@
     1.4 +/**
     1.5 + * Copyright (c) 2012, Ben Fortuna
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + *
    1.12 + *  o Redistributions of source code must retain the above copyright
    1.13 + * notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *  o Redistributions in binary form must reproduce the above copyright
    1.16 + * notice, this list of conditions and the following disclaimer in the
    1.17 + * documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + *  o Neither the name of Ben Fortuna nor the names of any other contributors
    1.20 + * may be used to endorse or promote products derived from this software
    1.21 + * without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 + */
    1.35 +package net.fortuna.ical4j.model;
    1.36 +
    1.37 +import java.util.Calendar;
    1.38 +import java.util.Collections;
    1.39 +import java.util.Date;
    1.40 +import java.util.List;
    1.41 +
    1.42 +import net.fortuna.ical4j.model.component.Daylight;
    1.43 +import net.fortuna.ical4j.model.component.Observance;
    1.44 +import net.fortuna.ical4j.model.component.VTimeZone;
    1.45 +import net.fortuna.ical4j.model.property.TzId;
    1.46 +import net.fortuna.ical4j.model.property.TzOffsetTo;
    1.47 +
    1.48 +/**
    1.49 + * $Id$
    1.50 + *
    1.51 + * Created on 13/09/2005
    1.52 + *
    1.53 + * A Java timezone implementation based on an underlying VTimeZone
    1.54 + * definition.
    1.55 + * @author Ben Fortuna
    1.56 + */
    1.57 +public class TimeZone extends java.util.TimeZone {
    1.58 +    
    1.59 +    private static final long serialVersionUID = -5620979316746547234L;
    1.60 +    
    1.61 +    private final VTimeZone vTimeZone;
    1.62 +    private final int rawOffset;
    1.63 +    
    1.64 +    /**
    1.65 +     * Constructs a new instance based on the specified VTimeZone.
    1.66 +     * @param vTimeZone a VTIMEZONE object instance
    1.67 +     */
    1.68 +    public TimeZone(final VTimeZone vTimeZone) {
    1.69 +        this.vTimeZone = vTimeZone;
    1.70 +        final TzId tzId = (TzId) vTimeZone.getProperty(Property.TZID);
    1.71 +        setID(tzId.getValue());
    1.72 +        this.rawOffset = getRawOffset(vTimeZone);
    1.73 +    }
    1.74 +
    1.75 +    /**
    1.76 +     * {@inheritDoc}
    1.77 +     */
    1.78 +    public final int getOffset(final int era, final int year, final int month, final int day,
    1.79 +            final int dayOfWeek, final int milliseconds) {
    1.80 +        
    1.81 +        final Calendar cal = Calendar.getInstance();
    1.82 +        cal.set(Calendar.ERA, era);
    1.83 +        cal.set(Calendar.YEAR, year);
    1.84 +        cal.set(Calendar.MONTH, month);
    1.85 +        cal.set(Calendar.DAY_OF_YEAR, day);
    1.86 +        cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    1.87 +        cal.set(Calendar.MILLISECOND, milliseconds);
    1.88 +        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(cal.getTime()));
    1.89 +        if (observance != null) {
    1.90 +            final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
    1.91 +            return (int) offset.getOffset().getOffset();
    1.92 +        }
    1.93 +        return 0;
    1.94 +    }
    1.95 +
    1.96 +    /**
    1.97 +     * {@inheritDoc}
    1.98 +     */
    1.99 +    public int getOffset(long date) {
   1.100 +        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
   1.101 +        if (observance != null) {
   1.102 +            final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
   1.103 +            return (int) offset.getOffset().getOffset();
   1.104 +        }
   1.105 +        return 0;
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * {@inheritDoc}
   1.110 +     */
   1.111 +    public final int getRawOffset() {
   1.112 +        return rawOffset;
   1.113 +    }
   1.114 +
   1.115 +    /**
   1.116 +     * Determines if the specified date is in daylight time according to
   1.117 +     * this timezone. This is done by finding the latest supporting
   1.118 +     * observance for the specified date and identifying whether it is
   1.119 +     * daylight time.
   1.120 +     * @param date a date instance
   1.121 +     * @return true if the specified date is in daylight time, otherwise false
   1.122 +     */
   1.123 +    public final boolean inDaylightTime(final Date date) {
   1.124 +        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
   1.125 +        return (observance != null && observance instanceof Daylight);
   1.126 +    }
   1.127 +
   1.128 +    /**
   1.129 +     * {@inheritDoc}
   1.130 +     */
   1.131 +    public final void setRawOffset(final int offsetMillis) {
   1.132 +        throw new UnsupportedOperationException("Updates to the VTIMEZONE object must be performed directly");
   1.133 +    }
   1.134 +
   1.135 +    /**
   1.136 +     * {@inheritDoc}
   1.137 +     */
   1.138 +    public final boolean useDaylightTime() {
   1.139 +        final ComponentList daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT);
   1.140 +        return (!daylights.isEmpty());
   1.141 +    }
   1.142 +
   1.143 +    /**
   1.144 +     * @return Returns the VTimeZone backing this instance.
   1.145 +     */
   1.146 +    public final VTimeZone getVTimeZone() {
   1.147 +        return vTimeZone;
   1.148 +    }
   1.149 +
   1.150 +    private static final int getRawOffset(VTimeZone vt) {
   1.151 +        // per spec, rawoffset is the raw offset at the current date
   1.152 +        final DateTime now = new DateTime();
   1.153 +        
   1.154 +        List seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD);
   1.155 +        // if no standard time use daylight time..
   1.156 +        if (seasonalTimes.size() == 0) {
   1.157 +            seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT);
   1.158 +        }
   1.159 +        Observance latestSeasonalTime = null;
   1.160 +        Date latestOnset = null;
   1.161 +        for (int i = 0; i < seasonalTimes.size(); i++) {
   1.162 +            Observance seasonalTime = (Observance) seasonalTimes.get(i);
   1.163 +            Date onset = seasonalTime.getLatestOnset(now);
   1.164 +            if (onset == null) {
   1.165 +                continue;
   1.166 +            }
   1.167 +            if (latestOnset == null || onset.after(latestOnset)) {
   1.168 +                latestOnset = onset;
   1.169 +                latestSeasonalTime = seasonalTime;
   1.170 +            }
   1.171 +        }
   1.172 +        if (latestSeasonalTime != null) {
   1.173 +            final TzOffsetTo offsetTo = (TzOffsetTo) latestSeasonalTime.getProperty(Property.TZOFFSETTO);
   1.174 +            if (offsetTo != null) {
   1.175 +                return (int) offsetTo.getOffset().getOffset();
   1.176 +            }
   1.177 +        }
   1.178 +        return 0;
   1.179 +    }
   1.180 +}

mercurial