Path: blob/master/src/java.base/share/classes/java/time/temporal/JulianFields.java
41159 views
/*1* Copyright (c) 2012, 2019, 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) 2012, 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.temporal;6263import static java.time.temporal.ChronoField.EPOCH_DAY;64import static java.time.temporal.ChronoUnit.DAYS;65import static java.time.temporal.ChronoUnit.FOREVER;6667import java.time.DateTimeException;68import java.time.chrono.ChronoLocalDate;69import java.time.chrono.Chronology;70import java.time.format.ResolverStyle;71import java.util.Map;7273/**74* A set of date fields that provide access to Julian Days.75* <p>76* The Julian Day is a standard way of expressing date and time commonly used in the scientific community.77* It is expressed as a decimal number of whole days where days start at midday.78* This class represents variations on Julian Days that count whole days from midnight.79* <p>80* The fields are implemented relative to {@link ChronoField#EPOCH_DAY EPOCH_DAY}.81* The fields are supported, and can be queried and set if {@code EPOCH_DAY} is available.82* The fields work with all chronologies.83*84* @implSpec85* This is an immutable and thread-safe class.86*87* @since 1.888*/89public final class JulianFields {9091/**92* The offset from Julian to EPOCH DAY.93*/94private static final long JULIAN_DAY_OFFSET = 2440588L;9596/**97* Julian Day field.98* <p>99* This is an integer-based version of the Julian Day Number.100* Julian Day is a well-known system that represents the count of whole days since day 0,101* which is defined to be January 1, 4713 BCE in the Julian calendar, and -4713-11-24 Gregorian.102* The field has "JulianDay" as 'name', and 'DAYS' as 'baseUnit'.103* The field always refers to the local date-time, ignoring the offset or zone.104* <p>105* For date-times, 'JULIAN_DAY.getFrom()' assumes the same value from106* midnight until just before the next midnight.107* When 'JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.108* 'JULIAN_DAY.adjustInto()' and 'JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects that109* can be converted into {@link ChronoField#EPOCH_DAY}.110* An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.111* <p>112* In the resolving phase of parsing, a date can be created from a Julian Day field.113* In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}114* the Julian Day value is validated against the range of valid values.115* In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.116*117* <h4>Astronomical and Scientific Notes</h4>118* The standard astronomical definition uses a fraction to indicate the time-of-day,119* where each day is counted from midday to midday. For example,120* a fraction of 0 represents midday, a fraction of 0.25121* represents 18:00, a fraction of 0.5 represents midnight and a fraction122* of 0.75 represents 06:00.123* <p>124* By contrast, this implementation has no fractional part, and counts125* days from midnight to midnight.126* This implementation uses an integer and days starting at midnight.127* The integer value for the Julian Day Number is the astronomical Julian Day value at midday128* of the date in question.129* This amounts to the astronomical Julian Day, rounded to an integer {@code JDN = floor(JD + 0.5)}.130*131* <pre>132* | ISO date | Julian Day Number | Astronomical Julian Day |133* | 1970-01-01T00:00 | 2,440,588 | 2,440,587.5 |134* | 1970-01-01T06:00 | 2,440,588 | 2,440,587.75 |135* | 1970-01-01T12:00 | 2,440,588 | 2,440,588.0 |136* | 1970-01-01T18:00 | 2,440,588 | 2,440,588.25 |137* | 1970-01-02T00:00 | 2,440,589 | 2,440,588.5 |138* | 1970-01-02T06:00 | 2,440,589 | 2,440,588.75 |139* | 1970-01-02T12:00 | 2,440,589 | 2,440,589.0 |140* </pre>141* <p>142* Julian Days are sometimes taken to imply Universal Time or UTC, but this143* implementation always uses the Julian Day number for the local date,144* regardless of the offset or time-zone.145*/146public static final TemporalField JULIAN_DAY = Field.JULIAN_DAY;147148/**149* Modified Julian Day field.150* <p>151* This is an integer-based version of the Modified Julian Day Number.152* Modified Julian Day (MJD) is a well-known system that counts days continuously.153* It is defined relative to astronomical Julian Day as {@code MJD = JD - 2400000.5}.154* Each Modified Julian Day runs from midnight to midnight.155* The field always refers to the local date-time, ignoring the offset or zone.156* <p>157* For date-times, 'MODIFIED_JULIAN_DAY.getFrom()' assumes the same value from158* midnight until just before the next midnight.159* When 'MODIFIED_JULIAN_DAY.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.160* 'MODIFIED_JULIAN_DAY.adjustInto()' and 'MODIFIED_JULIAN_DAY.getFrom()' only apply to {@code Temporal} objects161* that can be converted into {@link ChronoField#EPOCH_DAY}.162* An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.163* <p>164* This implementation is an integer version of MJD with the decimal part rounded to floor.165* <p>166* In the resolving phase of parsing, a date can be created from a Modified Julian Day field.167* In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}168* the Modified Julian Day value is validated against the range of valid values.169* In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.170*171* <h4>Astronomical and Scientific Notes</h4>172* <pre>173* | ISO date | Modified Julian Day | Decimal MJD |174* | 1970-01-01T00:00 | 40,587 | 40,587.0 |175* | 1970-01-01T06:00 | 40,587 | 40,587.25 |176* | 1970-01-01T12:00 | 40,587 | 40,587.5 |177* | 1970-01-01T18:00 | 40,587 | 40,587.75 |178* | 1970-01-02T00:00 | 40,588 | 40,588.0 |179* | 1970-01-02T06:00 | 40,588 | 40,588.25 |180* | 1970-01-02T12:00 | 40,588 | 40,588.5 |181* </pre>182*183* Modified Julian Days are sometimes taken to imply Universal Time or UTC, but this184* implementation always uses the Modified Julian Day for the local date,185* regardless of the offset or time-zone.186*/187public static final TemporalField MODIFIED_JULIAN_DAY = Field.MODIFIED_JULIAN_DAY;188189/**190* Rata Die field.191* <p>192* Rata Die counts whole days continuously starting day 1 at midnight at the beginning of 0001-01-01 (ISO).193* The field always refers to the local date-time, ignoring the offset or zone.194* <p>195* For date-times, 'RATA_DIE.getFrom()' assumes the same value from196* midnight until just before the next midnight.197* When 'RATA_DIE.adjustInto()' is applied to a date-time, the time of day portion remains unaltered.198* 'RATA_DIE.adjustInto()' and 'RATA_DIE.getFrom()' only apply to {@code Temporal} objects199* that can be converted into {@link ChronoField#EPOCH_DAY}.200* An {@link UnsupportedTemporalTypeException} is thrown for any other type of object.201* <p>202* In the resolving phase of parsing, a date can be created from a Rata Die field.203* In {@linkplain ResolverStyle#STRICT strict mode} and {@linkplain ResolverStyle#SMART smart mode}204* the Rata Die value is validated against the range of valid values.205* In {@linkplain ResolverStyle#LENIENT lenient mode} no validation occurs.206*/207public static final TemporalField RATA_DIE = Field.RATA_DIE;208209/**210* Restricted constructor.211*/212private JulianFields() {213throw new AssertionError("Not instantiable");214}215216/**217* Implementation of JulianFields. Each instance is a singleton.218*/219private static enum Field implements TemporalField {220JULIAN_DAY("JulianDay", DAYS, FOREVER, JULIAN_DAY_OFFSET),221MODIFIED_JULIAN_DAY("ModifiedJulianDay", DAYS, FOREVER, 40587L),222RATA_DIE("RataDie", DAYS, FOREVER, 719163L);223224private final transient String name;225private final transient TemporalUnit baseUnit;226private final transient TemporalUnit rangeUnit;227private final transient ValueRange range;228private final transient long offset;229230private Field(String name, TemporalUnit baseUnit, TemporalUnit rangeUnit, long offset) {231this.name = name;232this.baseUnit = baseUnit;233this.rangeUnit = rangeUnit;234this.range = ValueRange.of(-365243219162L + offset, 365241780471L + offset);235this.offset = offset;236}237238//-----------------------------------------------------------------------239@Override240public TemporalUnit getBaseUnit() {241return baseUnit;242}243244@Override245public TemporalUnit getRangeUnit() {246return rangeUnit;247}248249@Override250public boolean isDateBased() {251return true;252}253254@Override255public boolean isTimeBased() {256return false;257}258259@Override260public ValueRange range() {261return range;262}263264//-----------------------------------------------------------------------265@Override266public boolean isSupportedBy(TemporalAccessor temporal) {267return temporal.isSupported(EPOCH_DAY);268}269270@Override271public ValueRange rangeRefinedBy(TemporalAccessor temporal) {272if (isSupportedBy(temporal) == false) {273throw new DateTimeException("Unsupported field: " + this);274}275return range();276}277278@Override279public long getFrom(TemporalAccessor temporal) {280return temporal.getLong(EPOCH_DAY) + offset;281}282283@SuppressWarnings("unchecked")284@Override285public <R extends Temporal> R adjustInto(R temporal, long newValue) {286if (range().isValidValue(newValue) == false) {287throw new DateTimeException("Invalid value: " + name + " " + newValue);288}289return (R) temporal.with(EPOCH_DAY, Math.subtractExact(newValue, offset));290}291292//-----------------------------------------------------------------------293@Override294public ChronoLocalDate resolve(295Map<TemporalField, Long> fieldValues, TemporalAccessor partialTemporal, ResolverStyle resolverStyle) {296long value = fieldValues.remove(this);297Chronology chrono = Chronology.from(partialTemporal);298if (resolverStyle == ResolverStyle.LENIENT) {299return chrono.dateEpochDay(Math.subtractExact(value, offset));300}301range().checkValidValue(value, this);302return chrono.dateEpochDay(value - offset);303}304305//-----------------------------------------------------------------------306@Override307public String toString() {308return name;309}310}311}312313314