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

branch
ICAL4J_EMBED_1
changeset 15
cc93757aeca3
parent 14
5ae3e5665a0b
child 18
6dcaece8ec41
     1.1 --- a/src/net/fortuna/ical4j/model/TimeZone.java	Thu Feb 12 18:02:00 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,219 +0,0 @@
     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 dayOfMonth,
    1.79 -            final int dayOfWeek, final int milliseconds) {
    1.80 -
    1.81 -        // calculate time of day
    1.82 -        int ms = milliseconds;
    1.83 -        final int hour = ms / 3600000;
    1.84 -        ms -= hour*3600000;
    1.85 -        final int minute = ms / 60000;
    1.86 -        ms -= minute*60000;
    1.87 -        final int second = ms / 1000;
    1.88 -        ms -= second*1000;
    1.89 -
    1.90 -        final Calendar cal = Calendar.getInstance();
    1.91 -        cal.clear();	// don't retain current date/time, it may disturb the calculation		
    1.92 -        
    1.93 -    	// set date and time
    1.94 -        cal.set(Calendar.ERA, era);
    1.95 -        cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    1.96 -        cal.set(year, month, dayOfMonth, hour, minute, second);
    1.97 -        cal.set(Calendar.MILLISECOND, ms);
    1.98 -        
    1.99 -        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(cal.getTime()));
   1.100 -        if (observance != null) {
   1.101 -            final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
   1.102 -            return (int) offset.getOffset().getOffset();
   1.103 -        }
   1.104 -        return 0;
   1.105 -    }
   1.106 -
   1.107 -    /**
   1.108 -     * {@inheritDoc}
   1.109 -     */
   1.110 -    public int getOffset(long date) {
   1.111 -        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
   1.112 -        if (observance != null) {
   1.113 -            final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
   1.114 -            if (offset.getOffset().getOffset() < getRawOffset()) {
   1.115 -            	return getRawOffset();
   1.116 -            }
   1.117 -            else {
   1.118 -            	return (int) offset.getOffset().getOffset();
   1.119 -            }
   1.120 -        }
   1.121 -        return 0;
   1.122 -    }
   1.123 -
   1.124 -    /**
   1.125 -     * {@inheritDoc}
   1.126 -     */
   1.127 -    public final int getRawOffset() {
   1.128 -        return rawOffset;
   1.129 -    }
   1.130 -
   1.131 -    /**
   1.132 -     * Determines if the specified date is in daylight time according to
   1.133 -     * this timezone. This is done by finding the latest supporting
   1.134 -     * observance for the specified date and identifying whether it is
   1.135 -     * daylight time.
   1.136 -     * @param date a date instance
   1.137 -     * @return true if the specified date is in daylight time, otherwise false
   1.138 -     */
   1.139 -    public final boolean inDaylightTime(final Date date) {
   1.140 -        final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
   1.141 -        return (observance != null && observance instanceof Daylight);
   1.142 -    }
   1.143 -
   1.144 -    /**
   1.145 -     * {@inheritDoc}
   1.146 -     */
   1.147 -    public final void setRawOffset(final int offsetMillis) {
   1.148 -        throw new UnsupportedOperationException("Updates to the VTIMEZONE object must be performed directly");
   1.149 -    }
   1.150 -
   1.151 -    /**
   1.152 -     * {@inheritDoc}
   1.153 -     */
   1.154 -    public final boolean useDaylightTime() {
   1.155 -        final ComponentList daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT);
   1.156 -        return (!daylights.isEmpty());
   1.157 -    }
   1.158 -
   1.159 -    /**
   1.160 -     * @return Returns the VTimeZone backing this instance.
   1.161 -     */
   1.162 -    public final VTimeZone getVTimeZone() {
   1.163 -        return vTimeZone;
   1.164 -    }
   1.165 -
   1.166 -    private static final int getRawOffset(VTimeZone vt) {
   1.167 -        
   1.168 -        List seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD);
   1.169 -        // if no standard time use daylight time..
   1.170 -        if (seasonalTimes.isEmpty()) {
   1.171 -            seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT);
   1.172 -            if (seasonalTimes.isEmpty()) {
   1.173 -                return 0;
   1.174 -            }
   1.175 -        }
   1.176 -        Observance latestSeasonalTime = null;
   1.177 -        if (seasonalTimes.size() > 1) {
   1.178 -            // per java spec and when dealing with historical time,
   1.179 -            // rawoffset is the raw offset at the current date
   1.180 -            final DateTime now = new DateTime();
   1.181 -            Date latestOnset = null;
   1.182 -            for (int i = 0; i < seasonalTimes.size(); i++) {
   1.183 -                Observance seasonalTime = (Observance) seasonalTimes.get(i);
   1.184 -                Date onset = seasonalTime.getLatestOnset(now);
   1.185 -                if (onset == null) {
   1.186 -                    continue;
   1.187 -                }
   1.188 -                if (latestOnset == null || onset.after(latestOnset)) {
   1.189 -                    latestOnset = onset;
   1.190 -                    latestSeasonalTime = seasonalTime;
   1.191 -                }
   1.192 -            }
   1.193 -        } else {
   1.194 -            latestSeasonalTime = (Observance)seasonalTimes.get(0);
   1.195 -        }
   1.196 -        if (latestSeasonalTime != null) {
   1.197 -            final TzOffsetTo offsetTo = (TzOffsetTo) latestSeasonalTime.getProperty(Property.TZOFFSETTO);
   1.198 -            if (offsetTo != null) {
   1.199 -                return (int) offsetTo.getOffset().getOffset();
   1.200 -            }
   1.201 -        }
   1.202 -        return 0;
   1.203 -    }
   1.204 -
   1.205 -    public boolean equals(Object o) {
   1.206 -        if (this == o) return true;
   1.207 -        if (o == null || getClass() != o.getClass()) return false;
   1.208 -
   1.209 -        TimeZone timeZone = (TimeZone) o;
   1.210 -
   1.211 -        if (rawOffset != timeZone.rawOffset) return false;
   1.212 -        if (vTimeZone != null ? !vTimeZone.equals(timeZone.vTimeZone) : timeZone.vTimeZone != null) return false;
   1.213 -
   1.214 -        return true;
   1.215 -    }
   1.216 -
   1.217 -    public int hashCode() {
   1.218 -        int result = vTimeZone != null ? vTimeZone.hashCode() : 0;
   1.219 -        result = 31 * result + rawOffset;
   1.220 -        return result;
   1.221 -    }
   1.222 -}

mercurial