1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file is available under and governed by the GNU General Public
  28  * License version 2 only, as published by the Free Software Foundation.
  29  * However, the following notice accompanied the original version of this
  30  * file:
  31  *
  32  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
  33  *
  34  * All rights reserved.
  35  *
  36  * Redistribution and use in source and binary forms, with or without
  37  * modification, are permitted provided that the following conditions are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright notice,
  40  *    this list of conditions and the following disclaimer.
  41  *
  42  *  * Redistributions in binary form must reproduce the above copyright notice,
  43  *    this list of conditions and the following disclaimer in the documentation
  44  *    and/or other materials provided with the distribution.
  45  *
  46  *  * Neither the name of JSR-310 nor the names of its contributors
  47  *    may be used to endorse or promote products derived from this software
  48  *    without specific prior written permission.
  49  *
  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time.chrono;
  63 
  64 import static java.time.temporal.ChronoUnit.SECONDS;
  65 
  66 import java.io.IOException;
  67 import java.io.InvalidObjectException;
  68 import java.io.ObjectInput;
  69 import java.io.ObjectOutput;
  70 import java.io.ObjectStreamException;
  71 import java.io.Serializable;
  72 import java.time.DateTimeException;
  73 import java.time.Instant;
  74 import java.time.LocalDateTime;
  75 import java.time.ZoneId;
  76 import java.time.ZoneOffset;
  77 import java.time.temporal.ChronoField;
  78 import java.time.temporal.ChronoUnit;
  79 import java.time.temporal.Temporal;
  80 import java.time.temporal.TemporalField;
  81 import java.time.temporal.TemporalUnit;
  82 import java.time.zone.ZoneOffsetTransition;
  83 import java.time.zone.ZoneRules;
  84 import java.util.List;
  85 import java.util.Objects;
  86 
  87 /**
  88  * A date-time with a time-zone in the calendar neutral API.
  89  * <p>
  90  * {@code ZoneChronoDateTime} is an immutable representation of a date-time with a time-zone.
  91  * This class stores all date and time fields, to a precision of nanoseconds,
  92  * as well as a time-zone and zone offset.
  93  * <p>
  94  * The purpose of storing the time-zone is to distinguish the ambiguous case where
  95  * the local time-line overlaps, typically as a result of the end of daylight time.
  96  * Information about the local-time can be obtained using methods on the time-zone.
  97  *
  98  * <h3>Specification for implementors</h3>
  99  * This class is immutable and thread-safe.
 100  *
 101  * @param <D> the concrete type for the date of this date-time
 102  * @since 1.8
 103  */
 104 final class ChronoZonedDateTimeImpl<D extends ChronoLocalDate<D>>
 105         implements ChronoZonedDateTime<D>, Serializable {
 106 
 107     /**
 108      * Serialization version.
 109      */
 110     private static final long serialVersionUID = -5261813987200935591L;
 111 
 112     /**
 113      * The local date-time.
 114      */
 115     private final ChronoLocalDateTimeImpl<D> dateTime;
 116     /**
 117      * The zone offset.
 118      */
 119     private final ZoneOffset offset;
 120     /**
 121      * The zone ID.
 122      */
 123     private final ZoneId zone;
 124 
 125     //-----------------------------------------------------------------------
 126     /**
 127      * Obtains an instance from a local date-time using the preferred offset if possible.
 128      *
 129      * @param localDateTime  the local date-time, not null
 130      * @param zone  the zone identifier, not null
 131      * @param preferredOffset  the zone offset, null if no preference
 132      * @return the zoned date-time, not null
 133      */
 134     static <R extends ChronoLocalDate<R>> ChronoZonedDateTime<R> ofBest(
 135             ChronoLocalDateTimeImpl<R> localDateTime, ZoneId zone, ZoneOffset preferredOffset) {
 136         Objects.requireNonNull(localDateTime, "localDateTime");
 137         Objects.requireNonNull(zone, "zone");
 138         if (zone instanceof ZoneOffset) {
 139             return new ChronoZonedDateTimeImpl<>(localDateTime, (ZoneOffset) zone, zone);
 140         }
 141         ZoneRules rules = zone.getRules();
 142         LocalDateTime isoLDT = LocalDateTime.from(localDateTime);
 143         List<ZoneOffset> validOffsets = rules.getValidOffsets(isoLDT);
 144         ZoneOffset offset;
 145         if (validOffsets.size() == 1) {
 146             offset = validOffsets.get(0);
 147         } else if (validOffsets.size() == 0) {
 148             ZoneOffsetTransition trans = rules.getTransition(isoLDT);
 149             localDateTime = localDateTime.plusSeconds(trans.getDuration().getSeconds());
 150             offset = trans.getOffsetAfter();
 151         } else {
 152             if (preferredOffset != null && validOffsets.contains(preferredOffset)) {
 153                 offset = preferredOffset;
 154             } else {
 155                 offset = validOffsets.get(0);
 156             }
 157         }
 158         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 159         return new ChronoZonedDateTimeImpl<>(localDateTime, offset, zone);
 160     }
 161 
 162     /**
 163      * Obtains an instance from an instant using the specified time-zone.
 164      *
 165      * @param chrono  the chronology, not null
 166      * @param instant  the instant, not null
 167      * @param zone  the zone identifier, not null
 168      * @return the zoned date-time, not null
 169      */
 170     static ChronoZonedDateTimeImpl<?> ofInstant(Chronology chrono, Instant instant, ZoneId zone) {
 171         ZoneRules rules = zone.getRules();
 172         ZoneOffset offset = rules.getOffset(instant);
 173         Objects.requireNonNull(offset, "offset");  // protect against bad ZoneRules
 174         LocalDateTime ldt = LocalDateTime.ofEpochSecond(instant.getEpochSecond(), instant.getNano(), offset);
 175         ChronoLocalDateTimeImpl<?> cldt = (ChronoLocalDateTimeImpl<?>) chrono.localDateTime(ldt);
 176         return new ChronoZonedDateTimeImpl(cldt, offset, zone);
 177     }
 178 
 179     /**
 180      * Obtains an instance from an {@code Instant}.
 181      *
 182      * @param instant  the instant to create the date-time from, not null
 183      * @param zone  the time-zone to use, validated not null
 184      * @return the zoned date-time, validated not null
 185      */
 186     private ChronoZonedDateTimeImpl<D> create(Instant instant, ZoneId zone) {
 187         return (ChronoZonedDateTimeImpl<D>)ofInstant(toLocalDate().getChronology(), instant, zone);
 188     }
 189 
 190     //-----------------------------------------------------------------------
 191     /**
 192      * Constructor.
 193      *
 194      * @param dateTime  the date-time, not null
 195      * @param offset  the zone offset, not null
 196      * @param zone  the zone ID, not null
 197      */
 198     private ChronoZonedDateTimeImpl(ChronoLocalDateTimeImpl<D> dateTime, ZoneOffset offset, ZoneId zone) {
 199         this.dateTime = Objects.requireNonNull(dateTime, "dateTime");
 200         this.offset = Objects.requireNonNull(offset, "offset");
 201         this.zone = Objects.requireNonNull(zone, "zone");
 202     }
 203 
 204     //-----------------------------------------------------------------------
 205     public ZoneOffset getOffset() {
 206         return offset;
 207     }
 208 
 209     @Override
 210     public ChronoZonedDateTime<D> withEarlierOffsetAtOverlap() {
 211         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 212         if (trans != null && trans.isOverlap()) {
 213             ZoneOffset earlierOffset = trans.getOffsetBefore();
 214             if (earlierOffset.equals(offset) == false) {
 215                 return new ChronoZonedDateTimeImpl<D>(dateTime, earlierOffset, zone);
 216             }
 217         }
 218         return this;
 219     }
 220 
 221     @Override
 222     public ChronoZonedDateTime<D> withLaterOffsetAtOverlap() {
 223         ZoneOffsetTransition trans = getZone().getRules().getTransition(LocalDateTime.from(this));
 224         if (trans != null) {
 225             ZoneOffset offset = trans.getOffsetAfter();
 226             if (offset.equals(getOffset()) == false) {
 227                 return new ChronoZonedDateTimeImpl<D>(dateTime, offset, zone);
 228             }
 229         }
 230         return this;
 231     }
 232 
 233     //-----------------------------------------------------------------------
 234     @Override
 235     public ChronoLocalDateTime<D> toLocalDateTime() {
 236         return dateTime;
 237     }
 238 
 239     public ZoneId getZone() {
 240         return zone;
 241     }
 242 
 243     public ChronoZonedDateTime<D> withZoneSameLocal(ZoneId zone) {
 244         return ofBest(dateTime, zone, offset);
 245     }
 246 
 247     @Override
 248     public ChronoZonedDateTime<D> withZoneSameInstant(ZoneId zone) {
 249         Objects.requireNonNull(zone, "zone");
 250         return this.zone.equals(zone) ? this : create(dateTime.toInstant(offset), zone);
 251     }
 252 
 253     //-----------------------------------------------------------------------
 254     @Override
 255     public boolean isSupported(TemporalField field) {
 256         return field instanceof ChronoField || (field != null && field.isSupportedBy(this));
 257     }
 258 
 259     //-----------------------------------------------------------------------
 260     @Override
 261     public ChronoZonedDateTime<D> with(TemporalField field, long newValue) {
 262         if (field instanceof ChronoField) {
 263             ChronoField f = (ChronoField) field;
 264             switch (f) {
 265                 case INSTANT_SECONDS: return plus(newValue - toEpochSecond(), SECONDS);
 266                 case OFFSET_SECONDS: {
 267                     ZoneOffset offset = ZoneOffset.ofTotalSeconds(f.checkValidIntValue(newValue));
 268                     return create(dateTime.toInstant(offset), zone);
 269                 }
 270             }
 271             return ofBest(dateTime.with(field, newValue), zone, offset);
 272         }
 273         return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(field.adjustInto(this, newValue)));
 274     }
 275 
 276     //-----------------------------------------------------------------------
 277     @Override
 278     public ChronoZonedDateTime<D> plus(long amountToAdd, TemporalUnit unit) {
 279         if (unit instanceof ChronoUnit) {
 280             return with(dateTime.plus(amountToAdd, unit));
 281         }
 282         return (ChronoZonedDateTime<D>)(toLocalDate().getChronology().ensureChronoZonedDateTime(unit.addTo(this, amountToAdd)));   /// TODO: Generics replacement Risk!
 283     }
 284 
 285     //-----------------------------------------------------------------------
 286     @Override
 287     public long periodUntil(Temporal endDateTime, TemporalUnit unit) {
 288         if (endDateTime instanceof ChronoZonedDateTime == false) {
 289             throw new DateTimeException("Unable to calculate period between objects of two different types");
 290         }
 291         @SuppressWarnings("unchecked")
 292         ChronoZonedDateTime<D> end = (ChronoZonedDateTime<D>) endDateTime;
 293         if (toLocalDate().getChronology().equals(end.toLocalDate().getChronology()) == false) {
 294             throw new DateTimeException("Unable to calculate period between two different chronologies");
 295         }
 296         if (unit instanceof ChronoUnit) {
 297             end = end.withZoneSameInstant(offset);
 298             return dateTime.periodUntil(end.toLocalDateTime(), unit);
 299         }
 300         return unit.between(this, endDateTime);
 301     }
 302 
 303     //-----------------------------------------------------------------------
 304     private Object writeReplace() {
 305         return new Ser(Ser.CHRONO_ZONE_DATE_TIME_TYPE, this);
 306     }
 307 
 308     /**
 309      * Defend against malicious streams.
 310      * @return never
 311      * @throws InvalidObjectException always
 312      */
 313     private Object readResolve() throws ObjectStreamException {
 314         throw new InvalidObjectException("Deserialization via serialization delegate");
 315     }
 316 
 317     void writeExternal(ObjectOutput out) throws IOException {
 318         out.writeObject(dateTime);
 319         out.writeObject(offset);
 320         out.writeObject(zone);
 321     }
 322 
 323     static ChronoZonedDateTime<?> readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
 324         ChronoLocalDateTime<?> dateTime = (ChronoLocalDateTime<?>) in.readObject();
 325         ZoneOffset offset = (ZoneOffset) in.readObject();
 326         ZoneId zone = (ZoneId) in.readObject();
 327         return dateTime.atZone(offset).withZoneSameLocal(zone);
 328         // TODO: ZDT uses ofLenient()
 329     }
 330 
 331     //-------------------------------------------------------------------------
 332     @Override
 333     public boolean equals(Object obj) {
 334         if (this == obj) {
 335             return true;
 336         }
 337         if (obj instanceof ChronoZonedDateTime) {
 338             return compareTo((ChronoZonedDateTime<?>) obj) == 0;
 339         }
 340         return false;
 341     }
 342 
 343     @Override
 344     public int hashCode() {
 345         return toLocalDateTime().hashCode() ^ getOffset().hashCode() ^ Integer.rotateLeft(getZone().hashCode(), 3);
 346     }
 347 
 348     @Override
 349     public String toString() {
 350         String str = toLocalDateTime().toString() + getOffset().toString();
 351         if (getOffset() != getZone()) {
 352             str += '[' + getZone().toString() + ']';
 353         }
 354         return str;
 355     }
 356 
 357 
 358 }