Path: blob/master/src/java.base/share/classes/java/time/format/Parsed.java
41159 views
/*1* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file:30*31* Copyright (c) 2008-2013, Stephen Colebourne & Michael Nascimento Santos32*33* All rights reserved.34*35* Redistribution and use in source and binary forms, with or without36* modification, are permitted provided that the following conditions are met:37*38* * Redistributions of source code must retain the above copyright notice,39* this list of conditions and the following disclaimer.40*41* * Redistributions in binary form must reproduce the above copyright notice,42* this list of conditions and the following disclaimer in the documentation43* and/or other materials provided with the distribution.44*45* * Neither the name of JSR-310 nor the names of its contributors46* may be used to endorse or promote products derived from this software47* without specific prior written permission.48*49* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS50* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT51* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR52* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR53* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,54* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,55* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR56* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF57* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING58* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS59* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.60*/61package java.time.format;6263import static java.time.format.DateTimeFormatterBuilder.DayPeriod;64import static java.time.temporal.ChronoField.AMPM_OF_DAY;65import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;66import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_DAY;67import static java.time.temporal.ChronoField.HOUR_OF_AMPM;68import static java.time.temporal.ChronoField.HOUR_OF_DAY;69import static java.time.temporal.ChronoField.INSTANT_SECONDS;70import static java.time.temporal.ChronoField.MICRO_OF_DAY;71import static java.time.temporal.ChronoField.MICRO_OF_SECOND;72import static java.time.temporal.ChronoField.MILLI_OF_DAY;73import static java.time.temporal.ChronoField.MILLI_OF_SECOND;74import static java.time.temporal.ChronoField.MINUTE_OF_DAY;75import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;76import static java.time.temporal.ChronoField.NANO_OF_DAY;77import static java.time.temporal.ChronoField.NANO_OF_SECOND;78import static java.time.temporal.ChronoField.OFFSET_SECONDS;79import static java.time.temporal.ChronoField.SECOND_OF_DAY;80import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;8182import java.time.DateTimeException;83import java.time.Instant;84import java.time.LocalDate;85import java.time.LocalTime;86import java.time.Period;87import java.time.ZoneId;88import java.time.ZoneOffset;89import java.time.chrono.ChronoLocalDate;90import java.time.chrono.ChronoLocalDateTime;91import java.time.chrono.ChronoZonedDateTime;92import java.time.chrono.Chronology;93import java.time.temporal.ChronoField;94import java.time.temporal.TemporalAccessor;95import java.time.temporal.TemporalField;96import java.time.temporal.TemporalQueries;97import java.time.temporal.TemporalQuery;98import java.time.temporal.UnsupportedTemporalTypeException;99import java.util.HashMap;100import java.util.Iterator;101import java.util.Map;102import java.util.Map.Entry;103import java.util.Objects;104import java.util.Set;105106/**107* A store of parsed data.108* <p>109* This class is used during parsing to collect the data. Part of the parsing process110* involves handling optional blocks and multiple copies of the data get created to111* support the necessary backtracking.112* <p>113* Once parsing is completed, this class can be used as the resultant {@code TemporalAccessor}.114* In most cases, it is only exposed once the fields have been resolved.115*116* @implSpec117* This class is a mutable context intended for use from a single thread.118* Usage of the class is thread-safe within standard parsing as a new instance of this class119* is automatically created for each parse and parsing is single-threaded120*121* @since 1.8122*/123final class Parsed implements TemporalAccessor {124// some fields are accessed using package scope from DateTimeParseContext125126/**127* The parsed fields.128*/129final Map<TemporalField, Long> fieldValues = new HashMap<>();130/**131* The parsed zone.132*/133ZoneId zone;134/**135* The parsed chronology.136*/137Chronology chrono;138/**139* Whether a leap-second is parsed.140*/141boolean leapSecond;142/**143* The resolver style to use.144*/145private ResolverStyle resolverStyle;146/**147* The resolved date.148*/149private ChronoLocalDate date;150/**151* The resolved time.152*/153private LocalTime time;154/**155* The excess period from time-only parsing.156*/157Period excessDays = Period.ZERO;158/**159* The parsed day period.160*/161DayPeriod dayPeriod;162163/**164* Creates an instance.165*/166Parsed() {167}168169/**170* Creates a copy.171*/172Parsed copy() {173// only copy fields used in parsing stage174Parsed cloned = new Parsed();175cloned.fieldValues.putAll(this.fieldValues);176cloned.zone = this.zone;177cloned.chrono = this.chrono;178cloned.leapSecond = this.leapSecond;179cloned.dayPeriod = this.dayPeriod;180return cloned;181}182183//-----------------------------------------------------------------------184@Override185public boolean isSupported(TemporalField field) {186if (fieldValues.containsKey(field) ||187(date != null && date.isSupported(field)) ||188(time != null && time.isSupported(field))) {189return true;190}191return field != null && (!(field instanceof ChronoField)) && field.isSupportedBy(this);192}193194@Override195public long getLong(TemporalField field) {196Objects.requireNonNull(field, "field");197Long value = fieldValues.get(field);198if (value != null) {199return value;200}201if (date != null && date.isSupported(field)) {202return date.getLong(field);203}204if (time != null && time.isSupported(field)) {205return time.getLong(field);206}207if (field instanceof ChronoField) {208throw new UnsupportedTemporalTypeException("Unsupported field: " + field);209}210return field.getFrom(this);211}212213@SuppressWarnings("unchecked")214@Override215public <R> R query(TemporalQuery<R> query) {216if (query == TemporalQueries.zoneId()) {217return (R) zone;218} else if (query == TemporalQueries.chronology()) {219return (R) chrono;220} else if (query == TemporalQueries.localDate()) {221return (R) (date != null ? LocalDate.from(date) : null);222} else if (query == TemporalQueries.localTime()) {223return (R) time;224} else if (query == TemporalQueries.offset()) {225Long offsetSecs = fieldValues.get(OFFSET_SECONDS);226if (offsetSecs != null) {227return (R) ZoneOffset.ofTotalSeconds(offsetSecs.intValue());228}229if (zone instanceof ZoneOffset) {230return (R)zone;231}232return query.queryFrom(this);233} else if (query == TemporalQueries.zone()) {234return query.queryFrom(this);235} else if (query == TemporalQueries.precision()) {236return null; // not a complete date/time237}238// inline TemporalAccessor.super.query(query) as an optimization239// non-JDK classes are not permitted to make this optimization240return query.queryFrom(this);241}242243//-----------------------------------------------------------------------244/**245* Resolves the fields in this context.246*247* @param resolverStyle the resolver style, not null248* @param resolverFields the fields to use for resolving, null for all fields249* @return this, for method chaining250* @throws DateTimeException if resolving one field results in a value for251* another field that is in conflict252*/253TemporalAccessor resolve(ResolverStyle resolverStyle, Set<TemporalField> resolverFields) {254if (resolverFields != null) {255fieldValues.keySet().retainAll(resolverFields);256}257this.resolverStyle = resolverStyle;258resolveFields();259resolveTimeLenient();260crossCheck();261resolvePeriod();262resolveFractional();263resolveInstant();264return this;265}266267//-----------------------------------------------------------------------268private void resolveFields() {269// resolve ChronoField270resolveInstantFields();271resolveDateFields();272resolveTimeFields();273274// if any other fields, handle them275// any lenient date resolution should return epoch-day276if (fieldValues.size() > 0) {277int changedCount = 0;278outer:279while (changedCount < 50) {280for (Map.Entry<TemporalField, Long> entry : fieldValues.entrySet()) {281TemporalField targetField = entry.getKey();282TemporalAccessor resolvedObject = targetField.resolve(fieldValues, this, resolverStyle);283if (resolvedObject != null) {284if (resolvedObject instanceof ChronoZonedDateTime<?> czdt) {285if (zone == null) {286zone = czdt.getZone();287} else if (zone.equals(czdt.getZone()) == false) {288throw new DateTimeException("ChronoZonedDateTime must use the effective parsed zone: " + zone);289}290resolvedObject = czdt.toLocalDateTime();291}292if (resolvedObject instanceof ChronoLocalDateTime<?> cldt) {293updateCheckConflict(cldt.toLocalTime(), Period.ZERO);294updateCheckConflict(cldt.toLocalDate());295changedCount++;296continue outer; // have to restart to avoid concurrent modification297}298if (resolvedObject instanceof ChronoLocalDate) {299updateCheckConflict((ChronoLocalDate) resolvedObject);300changedCount++;301continue outer; // have to restart to avoid concurrent modification302}303if (resolvedObject instanceof LocalTime) {304updateCheckConflict((LocalTime) resolvedObject, Period.ZERO);305changedCount++;306continue outer; // have to restart to avoid concurrent modification307}308throw new DateTimeException("Method resolve() can only return ChronoZonedDateTime, " +309"ChronoLocalDateTime, ChronoLocalDate or LocalTime");310} else if (fieldValues.containsKey(targetField) == false) {311changedCount++;312continue outer; // have to restart to avoid concurrent modification313}314}315break;316}317if (changedCount == 50) { // catch infinite loops318throw new DateTimeException("One of the parsed fields has an incorrectly implemented resolve method");319}320// if something changed then have to redo ChronoField resolve321if (changedCount > 0) {322resolveInstantFields();323resolveDateFields();324resolveTimeFields();325}326}327}328329private void updateCheckConflict(TemporalField targetField, TemporalField changeField, Long changeValue) {330Long old = fieldValues.put(changeField, changeValue);331if (old != null && old.longValue() != changeValue.longValue()) {332throw new DateTimeException("Conflict found: " + changeField + " " + old +333" differs from " + changeField + " " + changeValue +334" while resolving " + targetField);335}336}337338339//-----------------------------------------------------------------------340private void resolveInstantFields() {341// resolve parsed instant seconds to date and time if zone available342if (fieldValues.containsKey(INSTANT_SECONDS)) {343if (zone != null) {344resolveInstantFields0(zone);345} else {346Long offsetSecs = fieldValues.get(OFFSET_SECONDS);347if (offsetSecs != null) {348ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());349resolveInstantFields0(offset);350}351}352}353}354355private void resolveInstantFields0(ZoneId selectedZone) {356Instant instant = Instant.ofEpochSecond(fieldValues.remove(INSTANT_SECONDS));357ChronoZonedDateTime<?> zdt = chrono.zonedDateTime(instant, selectedZone);358updateCheckConflict(zdt.toLocalDate());359updateCheckConflict(INSTANT_SECONDS, SECOND_OF_DAY, (long) zdt.toLocalTime().toSecondOfDay());360}361362//-----------------------------------------------------------------------363private void resolveDateFields() {364updateCheckConflict(chrono.resolveDate(fieldValues, resolverStyle));365}366367private void updateCheckConflict(ChronoLocalDate cld) {368if (date != null) {369if (cld != null && date.equals(cld) == false) {370throw new DateTimeException("Conflict found: Fields resolved to two different dates: " + date + " " + cld);371}372} else if (cld != null) {373if (chrono.equals(cld.getChronology()) == false) {374throw new DateTimeException("ChronoLocalDate must use the effective parsed chronology: " + chrono);375}376date = cld;377}378}379380//-----------------------------------------------------------------------381private void resolveTimeFields() {382// simplify fields383if (fieldValues.containsKey(CLOCK_HOUR_OF_DAY)) {384// lenient allows anything, smart allows 0-24, strict allows 1-24385long ch = fieldValues.remove(CLOCK_HOUR_OF_DAY);386if (resolverStyle == ResolverStyle.STRICT || (resolverStyle == ResolverStyle.SMART && ch != 0)) {387CLOCK_HOUR_OF_DAY.checkValidValue(ch);388}389updateCheckConflict(CLOCK_HOUR_OF_DAY, HOUR_OF_DAY, ch == 24 ? 0 : ch);390}391if (fieldValues.containsKey(CLOCK_HOUR_OF_AMPM)) {392// lenient allows anything, smart allows 0-12, strict allows 1-12393long ch = fieldValues.remove(CLOCK_HOUR_OF_AMPM);394if (resolverStyle == ResolverStyle.STRICT || (resolverStyle == ResolverStyle.SMART && ch != 0)) {395CLOCK_HOUR_OF_AMPM.checkValidValue(ch);396}397updateCheckConflict(CLOCK_HOUR_OF_AMPM, HOUR_OF_AMPM, ch == 12 ? 0 : ch);398}399if (fieldValues.containsKey(AMPM_OF_DAY) && fieldValues.containsKey(HOUR_OF_AMPM)) {400long ap = fieldValues.remove(AMPM_OF_DAY);401long hap = fieldValues.remove(HOUR_OF_AMPM);402if (resolverStyle == ResolverStyle.LENIENT) {403updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, Math.addExact(Math.multiplyExact(ap, 12), hap));404} else { // STRICT or SMART405AMPM_OF_DAY.checkValidValue(ap);406HOUR_OF_AMPM.checkValidValue(hap);407updateCheckConflict(AMPM_OF_DAY, HOUR_OF_DAY, ap * 12 + hap);408}409}410if (fieldValues.containsKey(NANO_OF_DAY)) {411long nod = fieldValues.remove(NANO_OF_DAY);412if (resolverStyle != ResolverStyle.LENIENT) {413NANO_OF_DAY.checkValidValue(nod);414}415updateCheckConflict(NANO_OF_DAY, HOUR_OF_DAY, nod / 3600_000_000_000L);416updateCheckConflict(NANO_OF_DAY, MINUTE_OF_HOUR, (nod / 60_000_000_000L) % 60);417updateCheckConflict(NANO_OF_DAY, SECOND_OF_MINUTE, (nod / 1_000_000_000L) % 60);418updateCheckConflict(NANO_OF_DAY, NANO_OF_SECOND, nod % 1_000_000_000L);419}420if (fieldValues.containsKey(MICRO_OF_DAY)) {421long cod = fieldValues.remove(MICRO_OF_DAY);422if (resolverStyle != ResolverStyle.LENIENT) {423MICRO_OF_DAY.checkValidValue(cod);424}425updateCheckConflict(MICRO_OF_DAY, SECOND_OF_DAY, cod / 1_000_000L);426updateCheckConflict(MICRO_OF_DAY, MICRO_OF_SECOND, cod % 1_000_000L);427}428if (fieldValues.containsKey(MILLI_OF_DAY)) {429long lod = fieldValues.remove(MILLI_OF_DAY);430if (resolverStyle != ResolverStyle.LENIENT) {431MILLI_OF_DAY.checkValidValue(lod);432}433updateCheckConflict(MILLI_OF_DAY, SECOND_OF_DAY, lod / 1_000);434updateCheckConflict(MILLI_OF_DAY, MILLI_OF_SECOND, lod % 1_000);435}436if (fieldValues.containsKey(SECOND_OF_DAY)) {437long sod = fieldValues.remove(SECOND_OF_DAY);438if (resolverStyle != ResolverStyle.LENIENT) {439SECOND_OF_DAY.checkValidValue(sod);440}441updateCheckConflict(SECOND_OF_DAY, HOUR_OF_DAY, sod / 3600);442updateCheckConflict(SECOND_OF_DAY, MINUTE_OF_HOUR, (sod / 60) % 60);443updateCheckConflict(SECOND_OF_DAY, SECOND_OF_MINUTE, sod % 60);444}445if (fieldValues.containsKey(MINUTE_OF_DAY)) {446long mod = fieldValues.remove(MINUTE_OF_DAY);447if (resolverStyle != ResolverStyle.LENIENT) {448MINUTE_OF_DAY.checkValidValue(mod);449}450updateCheckConflict(MINUTE_OF_DAY, HOUR_OF_DAY, mod / 60);451updateCheckConflict(MINUTE_OF_DAY, MINUTE_OF_HOUR, mod % 60);452}453454// combine partial second fields strictly, leaving lenient expansion to later455if (fieldValues.containsKey(NANO_OF_SECOND)) {456long nos = fieldValues.get(NANO_OF_SECOND);457if (resolverStyle != ResolverStyle.LENIENT) {458NANO_OF_SECOND.checkValidValue(nos);459}460if (fieldValues.containsKey(MICRO_OF_SECOND)) {461long cos = fieldValues.remove(MICRO_OF_SECOND);462if (resolverStyle != ResolverStyle.LENIENT) {463MICRO_OF_SECOND.checkValidValue(cos);464}465nos = cos * 1000 + (nos % 1000);466updateCheckConflict(MICRO_OF_SECOND, NANO_OF_SECOND, nos);467}468if (fieldValues.containsKey(MILLI_OF_SECOND)) {469long los = fieldValues.remove(MILLI_OF_SECOND);470if (resolverStyle != ResolverStyle.LENIENT) {471MILLI_OF_SECOND.checkValidValue(los);472}473updateCheckConflict(MILLI_OF_SECOND, NANO_OF_SECOND, los * 1_000_000L + (nos % 1_000_000L));474}475}476477if (dayPeriod != null && fieldValues.containsKey(HOUR_OF_AMPM)) {478long hoap = fieldValues.remove(HOUR_OF_AMPM);479if (resolverStyle != ResolverStyle.LENIENT) {480HOUR_OF_AMPM.checkValidValue(hoap);481}482Long mohObj = fieldValues.get(MINUTE_OF_HOUR);483long moh = mohObj != null ? Math.floorMod(mohObj, 60) : 0;484long excessHours = dayPeriod.includes((Math.floorMod(hoap, 12) + 12) * 60 + moh) ? 12 : 0;485long hod = Math.addExact(hoap, excessHours);486updateCheckConflict(HOUR_OF_AMPM, HOUR_OF_DAY, hod);487dayPeriod = null;488}489490// convert to time if all four fields available (optimization)491if (fieldValues.containsKey(HOUR_OF_DAY) && fieldValues.containsKey(MINUTE_OF_HOUR) &&492fieldValues.containsKey(SECOND_OF_MINUTE) && fieldValues.containsKey(NANO_OF_SECOND)) {493long hod = fieldValues.remove(HOUR_OF_DAY);494long moh = fieldValues.remove(MINUTE_OF_HOUR);495long som = fieldValues.remove(SECOND_OF_MINUTE);496long nos = fieldValues.remove(NANO_OF_SECOND);497resolveTime(hod, moh, som, nos);498}499}500501private void resolveTimeLenient() {502// leniently create a time from incomplete information503// done after everything else as it creates information from nothing504// which would break updateCheckConflict(field)505506if (time == null) {507// NANO_OF_SECOND merged with MILLI/MICRO above508if (fieldValues.containsKey(MILLI_OF_SECOND)) {509long los = fieldValues.remove(MILLI_OF_SECOND);510if (fieldValues.containsKey(MICRO_OF_SECOND)) {511// merge milli-of-second and micro-of-second for better error message512long cos = los * 1_000 + (fieldValues.get(MICRO_OF_SECOND) % 1_000);513updateCheckConflict(MILLI_OF_SECOND, MICRO_OF_SECOND, cos);514fieldValues.remove(MICRO_OF_SECOND);515fieldValues.put(NANO_OF_SECOND, cos * 1_000L);516} else {517// convert milli-of-second to nano-of-second518fieldValues.put(NANO_OF_SECOND, los * 1_000_000L);519}520} else if (fieldValues.containsKey(MICRO_OF_SECOND)) {521// convert micro-of-second to nano-of-second522long cos = fieldValues.remove(MICRO_OF_SECOND);523fieldValues.put(NANO_OF_SECOND, cos * 1_000L);524}525526// Set the hour-of-day, if not exist and not in STRICT, to the mid point of the day period or am/pm.527if (!fieldValues.containsKey(HOUR_OF_DAY) &&528!fieldValues.containsKey(MINUTE_OF_HOUR) &&529!fieldValues.containsKey(SECOND_OF_MINUTE) &&530!fieldValues.containsKey(NANO_OF_SECOND) &&531resolverStyle != ResolverStyle.STRICT) {532if (dayPeriod != null) {533long midpoint = dayPeriod.mid();534resolveTime(midpoint / 60, midpoint % 60, 0, 0);535dayPeriod = null;536} else if (fieldValues.containsKey(AMPM_OF_DAY)) {537long ap = fieldValues.remove(AMPM_OF_DAY);538if (resolverStyle == ResolverStyle.LENIENT) {539resolveTime(Math.addExact(Math.multiplyExact(ap, 12), 6), 0, 0, 0);540} else { // SMART541AMPM_OF_DAY.checkValidValue(ap);542resolveTime(ap * 12 + 6, 0, 0, 0);543}544}545}546547// merge hour/minute/second/nano leniently548Long hod = fieldValues.get(HOUR_OF_DAY);549if (hod != null) {550Long moh = fieldValues.get(MINUTE_OF_HOUR);551Long som = fieldValues.get(SECOND_OF_MINUTE);552Long nos = fieldValues.get(NANO_OF_SECOND);553554// check for invalid combinations that cannot be defaulted555if ((moh == null && (som != null || nos != null)) ||556(moh != null && som == null && nos != null)) {557return;558}559560// default as necessary and build time561long mohVal = (moh != null ? moh : 0);562long somVal = (som != null ? som : 0);563long nosVal = (nos != null ? nos : 0);564565if (dayPeriod != null && resolverStyle != ResolverStyle.LENIENT) {566// Check whether the hod/mohVal is within the day period567if (!dayPeriod.includes(hod * 60 + mohVal)) {568throw new DateTimeException("Conflict found: Resolved time %02d:%02d".formatted(hod, mohVal) +569" conflicts with " + dayPeriod);570}571}572573resolveTime(hod, mohVal, somVal, nosVal);574fieldValues.remove(HOUR_OF_DAY);575fieldValues.remove(MINUTE_OF_HOUR);576fieldValues.remove(SECOND_OF_MINUTE);577fieldValues.remove(NANO_OF_SECOND);578}579}580581// validate remaining582if (resolverStyle != ResolverStyle.LENIENT && fieldValues.size() > 0) {583for (Entry<TemporalField, Long> entry : fieldValues.entrySet()) {584TemporalField field = entry.getKey();585if (field instanceof ChronoField && field.isTimeBased()) {586((ChronoField) field).checkValidValue(entry.getValue());587}588}589}590}591592private void resolveTime(long hod, long moh, long som, long nos) {593if (resolverStyle == ResolverStyle.LENIENT) {594long totalNanos = Math.multiplyExact(hod, 3600_000_000_000L);595totalNanos = Math.addExact(totalNanos, Math.multiplyExact(moh, 60_000_000_000L));596totalNanos = Math.addExact(totalNanos, Math.multiplyExact(som, 1_000_000_000L));597totalNanos = Math.addExact(totalNanos, nos);598int excessDays = (int) Math.floorDiv(totalNanos, 86400_000_000_000L); // safe int cast599long nod = Math.floorMod(totalNanos, 86400_000_000_000L);600updateCheckConflict(LocalTime.ofNanoOfDay(nod), Period.ofDays(excessDays));601} else { // STRICT or SMART602int mohVal = MINUTE_OF_HOUR.checkValidIntValue(moh);603int nosVal = NANO_OF_SECOND.checkValidIntValue(nos);604// handle 24:00 end of day605if (resolverStyle == ResolverStyle.SMART && hod == 24 && mohVal == 0 && som == 0 && nosVal == 0) {606updateCheckConflict(LocalTime.MIDNIGHT, Period.ofDays(1));607} else {608int hodVal = HOUR_OF_DAY.checkValidIntValue(hod);609int somVal = SECOND_OF_MINUTE.checkValidIntValue(som);610updateCheckConflict(LocalTime.of(hodVal, mohVal, somVal, nosVal), Period.ZERO);611}612}613}614615private void resolvePeriod() {616// add whole days if we have both date and time617if (date != null && time != null && excessDays.isZero() == false) {618date = date.plus(excessDays);619excessDays = Period.ZERO;620}621}622623private void resolveFractional() {624// ensure fractional seconds available as ChronoField requires625// resolveTimeLenient() will have merged MICRO_OF_SECOND/MILLI_OF_SECOND to NANO_OF_SECOND626if (time == null &&627(fieldValues.containsKey(INSTANT_SECONDS) ||628fieldValues.containsKey(SECOND_OF_DAY) ||629fieldValues.containsKey(SECOND_OF_MINUTE))) {630if (fieldValues.containsKey(NANO_OF_SECOND)) {631long nos = fieldValues.get(NANO_OF_SECOND);632fieldValues.put(MICRO_OF_SECOND, nos / 1000);633fieldValues.put(MILLI_OF_SECOND, nos / 1000000);634} else {635fieldValues.put(NANO_OF_SECOND, 0L);636fieldValues.put(MICRO_OF_SECOND, 0L);637fieldValues.put(MILLI_OF_SECOND, 0L);638}639}640}641642private void resolveInstant() {643// add instant seconds if we have date, time and zone644// Offset (if present) will be given priority over the zone.645if (date != null && time != null) {646Long offsetSecs = fieldValues.get(OFFSET_SECONDS);647if (offsetSecs != null) {648ZoneOffset offset = ZoneOffset.ofTotalSeconds(offsetSecs.intValue());649long instant = date.atTime(time).atZone(offset).toEpochSecond();650fieldValues.put(INSTANT_SECONDS, instant);651} else {652if (zone != null) {653long instant = date.atTime(time).atZone(zone).toEpochSecond();654fieldValues.put(INSTANT_SECONDS, instant);655}656}657}658}659660private void updateCheckConflict(LocalTime timeToSet, Period periodToSet) {661if (time != null) {662if (time.equals(timeToSet) == false) {663throw new DateTimeException("Conflict found: Fields resolved to different times: " + time + " " + timeToSet);664}665if (excessDays.isZero() == false && periodToSet.isZero() == false && excessDays.equals(periodToSet) == false) {666throw new DateTimeException("Conflict found: Fields resolved to different excess periods: " + excessDays + " " + periodToSet);667} else {668excessDays = periodToSet;669}670} else {671time = timeToSet;672excessDays = periodToSet;673}674}675676//-----------------------------------------------------------------------677private void crossCheck() {678// only cross-check date, time and date-time679// avoid object creation if possible680if (date != null) {681crossCheck(date);682}683if (time != null) {684crossCheck(time);685if (date != null && fieldValues.size() > 0) {686crossCheck(date.atTime(time));687}688}689}690691private void crossCheck(TemporalAccessor target) {692for (Iterator<Entry<TemporalField, Long>> it = fieldValues.entrySet().iterator(); it.hasNext(); ) {693Entry<TemporalField, Long> entry = it.next();694TemporalField field = entry.getKey();695if (target.isSupported(field)) {696long val1;697try {698val1 = target.getLong(field);699} catch (RuntimeException ex) {700continue;701}702long val2 = entry.getValue();703if (val1 != val2) {704throw new DateTimeException("Conflict found: Field " + field + " " + val1 +705" differs from " + field + " " + val2 + " derived from " + target);706}707it.remove();708}709}710}711712//-----------------------------------------------------------------------713@Override714public String toString() {715StringBuilder buf = new StringBuilder(64);716buf.append(fieldValues).append(',').append(chrono);717if (zone != null) {718buf.append(',').append(zone);719}720if (date != null || time != null) {721buf.append(" resolved to ");722if (date != null) {723buf.append(date);724if (time != null) {725buf.append('T').append(time);726}727} else {728buf.append(time);729}730}731return buf.toString();732}733734}735736737