|
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; |
|
33 |
|
34 import java.text.DateFormat; |
|
35 import java.util.Date; |
|
36 |
|
37 import net.fortuna.ical4j.util.CompatibilityHints; |
|
38 import net.fortuna.ical4j.util.Dates; |
|
39 import net.fortuna.ical4j.util.TimeZones; |
|
40 |
|
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 { |
|
53 |
|
54 /** |
|
55 * |
|
56 */ |
|
57 private static final long serialVersionUID = -4290728005713946811L; |
|
58 |
|
59 private DateFormat format; |
|
60 |
|
61 private DateFormat gmtFormat; |
|
62 |
|
63 private int precision; |
|
64 |
|
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 } |
|
85 |
|
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 } |
|
96 |
|
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 } |
|
108 |
|
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))) { |
|
122 |
|
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 } |
|
133 |
|
134 /** |
|
135 * @return Returns the format. |
|
136 */ |
|
137 protected final DateFormat getFormat() { |
|
138 return format; |
|
139 } |
|
140 |
|
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 } |