src/net/fortuna/ical4j/model/Iso8601.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.text.DateFormat;
    35 import java.util.Date;
    37 import net.fortuna.ical4j.util.CompatibilityHints;
    38 import net.fortuna.ical4j.util.Dates;
    39 import net.fortuna.ical4j.util.TimeZones;
    41 /**
    42  * $Id$
    43  *
    44  * Created on 30/06/2005
    45  *
    46  * Base class for date and time representations as defined
    47  * by the ISO 8601 standard. Sub-classes must ensure that either the correct
    48  * precision is used in constructor arguments, or that <code>Object.equals()</code>
    49  * is overridden to ensure equality checking is consistent with the type.
    50  * @author Ben Fortuna
    51  */
    52 public abstract class Iso8601 extends Date {
    54     /**
    55      * 
    56      */
    57     private static final long serialVersionUID = -4290728005713946811L;
    59     private DateFormat format;
    61     private DateFormat gmtFormat;
    63     private int precision;
    65     /**
    66      * @param time a time value in milliseconds
    67      * @param pattern the formatting pattern to apply
    68      * @param precision the precision to apply
    69      * @param tz the timezone for the instance
    70      * @see Dates#PRECISION_DAY
    71      * @see Dates#PRECISION_SECOND
    72      */
    73     public Iso8601(final long time, final String pattern, final int precision, java.util.TimeZone tz) {
    74         super(Dates.round(time, precision, tz)); //, TimeZone.getTimeZone(TimeZones.GMT_ID)));
    75 //        format = new SimpleDateFormat(pattern);
    76         format = CalendarDateFormatFactory.getInstance(pattern);
    77         format.setTimeZone(tz);
    78         format.setLenient(CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING));
    79         // use GMT timezone to avoid daylight savings rules affecting floating
    80         // time values..
    81 //        gmtFormat = new SimpleDateFormat(pattern);
    82 //        gmtFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID));
    83         this.precision = precision;
    84     }
    86     /**
    87      * @param pattern the formatting pattern to apply
    88      * @param precision the precision to apply
    89      * @param tz the timezone for the instance
    90      * @see Dates#PRECISION_DAY
    91      * @see Dates#PRECISION_SECOND
    92      */
    93     public Iso8601(final String pattern, final int precision, java.util.TimeZone tz) {
    94         this(Dates.getCurrentTimeRounded(), pattern, precision, tz);
    95     }
    97     /**
    98      * @param time a time value as a date
    99      * @param pattern the formatting pattern to apply
   100      * @param precision the precision to apply
   101      * @param tz the timezone for the instance
   102      * @see Dates#PRECISION_DAY
   103      * @see Dates#PRECISION_SECOND
   104      */
   105     public Iso8601(final Date time, final String pattern, final int precision, java.util.TimeZone tz) {
   106         this(time.getTime(), pattern, precision, tz);
   107     }
   109     /**
   110      * {@inheritDoc}
   111      */
   112     public String toString() {
   113         // if time is floating avoid daylight saving rules when generating
   114         // string representation of date..
   115         if (!(format.getTimeZone() instanceof TimeZone)) {
   116             if (gmtFormat == null) {
   117                 gmtFormat = (DateFormat) format.clone();
   118                 gmtFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID));
   119             }
   120             if (format.getTimeZone().inDaylightTime(this)
   121                     && format.getTimeZone().inDaylightTime(new Date(getTime() - 1))) {
   123                 return gmtFormat.format(new Date(getTime()
   124                         + format.getTimeZone().getRawOffset()
   125                         + format.getTimeZone().getDSTSavings()));
   126 //                return format.format(new Date(getTime() - format.getTimeZone().getDSTSavings()));
   127             }
   128 //            return gmtFormat.format(new Date(getTime() + format.getTimeZone().getOffset(getTime())));
   129             return gmtFormat.format(new Date(getTime() + format.getTimeZone().getRawOffset()));
   130         }
   131         return format.format(this);
   132     }
   134     /**
   135      * @return Returns the format.
   136      */
   137     protected final DateFormat getFormat() {
   138         return format;
   139     }
   141     /**
   142      * {@inheritDoc}
   143      */
   144     public void setTime(final long time) {
   145         // need to check for null format due to Android java.util.Date(long) constructor
   146         // calling this method..
   147         if (format != null) {
   148             super.setTime(Dates.round(time, precision, format.getTimeZone()));
   149         }
   150         else {
   151             // XXX: what do we do here??
   152             super.setTime(time);
   153         }
   154     }
   155 }

mercurial