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

Tue, 10 Feb 2015 18:12:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 18:12:00 +0100
changeset 0
fb9019fb1bf7
child 3
73bdfa70b04e
permissions
-rw-r--r--

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

     1 /**
     2  * Copyright (c) 2012, Ben Fortuna
     3  * All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions
     7  * are met:
     8  *
     9  *  o Redistributions of source code must retain the above copyright
    10  * notice, this list of conditions and the following disclaimer.
    11  *
    12  *  o Redistributions in binary form must reproduce the above copyright
    13  * notice, this list of conditions and the following disclaimer in the
    14  * documentation and/or other materials provided with the distribution.
    15  *
    16  *  o Neither the name of Ben Fortuna nor the names of any other contributors
    17  * may be used to endorse or promote products derived from this software
    18  * without specific prior written permission.
    19  *
    20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  */
    32 package net.fortuna.ical4j.model;
    34 import java.util.Calendar;
    35 import java.util.Collections;
    36 import java.util.Date;
    37 import java.util.List;
    39 import net.fortuna.ical4j.model.component.Daylight;
    40 import net.fortuna.ical4j.model.component.Observance;
    41 import net.fortuna.ical4j.model.component.VTimeZone;
    42 import net.fortuna.ical4j.model.property.TzId;
    43 import net.fortuna.ical4j.model.property.TzOffsetTo;
    45 /**
    46  * $Id$
    47  *
    48  * Created on 13/09/2005
    49  *
    50  * A Java timezone implementation based on an underlying VTimeZone
    51  * definition.
    52  * @author Ben Fortuna
    53  */
    54 public class TimeZone extends java.util.TimeZone {
    56     private static final long serialVersionUID = -5620979316746547234L;
    58     private final VTimeZone vTimeZone;
    59     private final int rawOffset;
    61     /**
    62      * Constructs a new instance based on the specified VTimeZone.
    63      * @param vTimeZone a VTIMEZONE object instance
    64      */
    65     public TimeZone(final VTimeZone vTimeZone) {
    66         this.vTimeZone = vTimeZone;
    67         final TzId tzId = (TzId) vTimeZone.getProperty(Property.TZID);
    68         setID(tzId.getValue());
    69         this.rawOffset = getRawOffset(vTimeZone);
    70     }
    72     /**
    73      * {@inheritDoc}
    74      */
    75     public final int getOffset(final int era, final int year, final int month, final int day,
    76             final int dayOfWeek, final int milliseconds) {
    78         final Calendar cal = Calendar.getInstance();
    79         cal.set(Calendar.ERA, era);
    80         cal.set(Calendar.YEAR, year);
    81         cal.set(Calendar.MONTH, month);
    82         cal.set(Calendar.DAY_OF_YEAR, day);
    83         cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    84         cal.set(Calendar.MILLISECOND, milliseconds);
    85         final Observance observance = vTimeZone.getApplicableObservance(new DateTime(cal.getTime()));
    86         if (observance != null) {
    87             final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
    88             return (int) offset.getOffset().getOffset();
    89         }
    90         return 0;
    91     }
    93     /**
    94      * {@inheritDoc}
    95      */
    96     public int getOffset(long date) {
    97         final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
    98         if (observance != null) {
    99             final TzOffsetTo offset = (TzOffsetTo) observance.getProperty(Property.TZOFFSETTO);
   100             return (int) offset.getOffset().getOffset();
   101         }
   102         return 0;
   103     }
   105     /**
   106      * {@inheritDoc}
   107      */
   108     public final int getRawOffset() {
   109         return rawOffset;
   110     }
   112     /**
   113      * Determines if the specified date is in daylight time according to
   114      * this timezone. This is done by finding the latest supporting
   115      * observance for the specified date and identifying whether it is
   116      * daylight time.
   117      * @param date a date instance
   118      * @return true if the specified date is in daylight time, otherwise false
   119      */
   120     public final boolean inDaylightTime(final Date date) {
   121         final Observance observance = vTimeZone.getApplicableObservance(new DateTime(date));
   122         return (observance != null && observance instanceof Daylight);
   123     }
   125     /**
   126      * {@inheritDoc}
   127      */
   128     public final void setRawOffset(final int offsetMillis) {
   129         throw new UnsupportedOperationException("Updates to the VTIMEZONE object must be performed directly");
   130     }
   132     /**
   133      * {@inheritDoc}
   134      */
   135     public final boolean useDaylightTime() {
   136         final ComponentList daylights = vTimeZone.getObservances().getComponents(Observance.DAYLIGHT);
   137         return (!daylights.isEmpty());
   138     }
   140     /**
   141      * @return Returns the VTimeZone backing this instance.
   142      */
   143     public final VTimeZone getVTimeZone() {
   144         return vTimeZone;
   145     }
   147     private static final int getRawOffset(VTimeZone vt) {
   148         // per spec, rawoffset is the raw offset at the current date
   149         final DateTime now = new DateTime();
   151         List seasonalTimes = vt.getObservances().getComponents(Observance.STANDARD);
   152         // if no standard time use daylight time..
   153         if (seasonalTimes.size() == 0) {
   154             seasonalTimes = vt.getObservances().getComponents(Observance.DAYLIGHT);
   155         }
   156         Observance latestSeasonalTime = null;
   157         Date latestOnset = null;
   158         for (int i = 0; i < seasonalTimes.size(); i++) {
   159             Observance seasonalTime = (Observance) seasonalTimes.get(i);
   160             Date onset = seasonalTime.getLatestOnset(now);
   161             if (onset == null) {
   162                 continue;
   163             }
   164             if (latestOnset == null || onset.after(latestOnset)) {
   165                 latestOnset = onset;
   166                 latestSeasonalTime = seasonalTime;
   167             }
   168         }
   169         if (latestSeasonalTime != null) {
   170             final TzOffsetTo offsetTo = (TzOffsetTo) latestSeasonalTime.getProperty(Property.TZOFFSETTO);
   171             if (offsetTo != null) {
   172                 return (int) offsetTo.getOffset().getOffset();
   173             }
   174         }
   175         return 0;
   176     }
   177 }

mercurial