1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/Iso8601.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,155 @@ 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.text.DateFormat; 1.38 +import java.util.Date; 1.39 + 1.40 +import net.fortuna.ical4j.util.CompatibilityHints; 1.41 +import net.fortuna.ical4j.util.Dates; 1.42 +import net.fortuna.ical4j.util.TimeZones; 1.43 + 1.44 +/** 1.45 + * $Id$ 1.46 + * 1.47 + * Created on 30/06/2005 1.48 + * 1.49 + * Base class for date and time representations as defined 1.50 + * by the ISO 8601 standard. Sub-classes must ensure that either the correct 1.51 + * precision is used in constructor arguments, or that <code>Object.equals()</code> 1.52 + * is overridden to ensure equality checking is consistent with the type. 1.53 + * @author Ben Fortuna 1.54 + */ 1.55 +public abstract class Iso8601 extends Date { 1.56 + 1.57 + /** 1.58 + * 1.59 + */ 1.60 + private static final long serialVersionUID = -4290728005713946811L; 1.61 + 1.62 + private DateFormat format; 1.63 + 1.64 + private DateFormat gmtFormat; 1.65 + 1.66 + private int precision; 1.67 + 1.68 + /** 1.69 + * @param time a time value in milliseconds 1.70 + * @param pattern the formatting pattern to apply 1.71 + * @param precision the precision to apply 1.72 + * @param tz the timezone for the instance 1.73 + * @see Dates#PRECISION_DAY 1.74 + * @see Dates#PRECISION_SECOND 1.75 + */ 1.76 + public Iso8601(final long time, final String pattern, final int precision, java.util.TimeZone tz) { 1.77 + super(Dates.round(time, precision, tz)); //, TimeZone.getTimeZone(TimeZones.GMT_ID))); 1.78 +// format = new SimpleDateFormat(pattern); 1.79 + format = CalendarDateFormatFactory.getInstance(pattern); 1.80 + format.setTimeZone(tz); 1.81 + format.setLenient(CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)); 1.82 + // use GMT timezone to avoid daylight savings rules affecting floating 1.83 + // time values.. 1.84 +// gmtFormat = new SimpleDateFormat(pattern); 1.85 +// gmtFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID)); 1.86 + this.precision = precision; 1.87 + } 1.88 + 1.89 + /** 1.90 + * @param pattern the formatting pattern to apply 1.91 + * @param precision the precision to apply 1.92 + * @param tz the timezone for the instance 1.93 + * @see Dates#PRECISION_DAY 1.94 + * @see Dates#PRECISION_SECOND 1.95 + */ 1.96 + public Iso8601(final String pattern, final int precision, java.util.TimeZone tz) { 1.97 + this(Dates.getCurrentTimeRounded(), pattern, precision, tz); 1.98 + } 1.99 + 1.100 + /** 1.101 + * @param time a time value as a date 1.102 + * @param pattern the formatting pattern to apply 1.103 + * @param precision the precision to apply 1.104 + * @param tz the timezone for the instance 1.105 + * @see Dates#PRECISION_DAY 1.106 + * @see Dates#PRECISION_SECOND 1.107 + */ 1.108 + public Iso8601(final Date time, final String pattern, final int precision, java.util.TimeZone tz) { 1.109 + this(time.getTime(), pattern, precision, tz); 1.110 + } 1.111 + 1.112 + /** 1.113 + * {@inheritDoc} 1.114 + */ 1.115 + public String toString() { 1.116 + // if time is floating avoid daylight saving rules when generating 1.117 + // string representation of date.. 1.118 + if (!(format.getTimeZone() instanceof TimeZone)) { 1.119 + if (gmtFormat == null) { 1.120 + gmtFormat = (DateFormat) format.clone(); 1.121 + gmtFormat.setTimeZone(TimeZone.getTimeZone(TimeZones.GMT_ID)); 1.122 + } 1.123 + if (format.getTimeZone().inDaylightTime(this) 1.124 + && format.getTimeZone().inDaylightTime(new Date(getTime() - 1))) { 1.125 + 1.126 + return gmtFormat.format(new Date(getTime() 1.127 + + format.getTimeZone().getRawOffset() 1.128 + + format.getTimeZone().getDSTSavings())); 1.129 +// return format.format(new Date(getTime() - format.getTimeZone().getDSTSavings())); 1.130 + } 1.131 +// return gmtFormat.format(new Date(getTime() + format.getTimeZone().getOffset(getTime()))); 1.132 + return gmtFormat.format(new Date(getTime() + format.getTimeZone().getRawOffset())); 1.133 + } 1.134 + return format.format(this); 1.135 + } 1.136 + 1.137 + /** 1.138 + * @return Returns the format. 1.139 + */ 1.140 + protected final DateFormat getFormat() { 1.141 + return format; 1.142 + } 1.143 + 1.144 + /** 1.145 + * {@inheritDoc} 1.146 + */ 1.147 + public void setTime(final long time) { 1.148 + // need to check for null format due to Android java.util.Date(long) constructor 1.149 + // calling this method.. 1.150 + if (format != null) { 1.151 + super.setTime(Dates.round(time, precision, format.getTimeZone())); 1.152 + } 1.153 + else { 1.154 + // XXX: what do we do here?? 1.155 + super.setTime(time); 1.156 + } 1.157 + } 1.158 +}