Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/tzdb/LocalDateTime.java
32287 views
/*1* Copyright (c) 2012, 2013, 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) 2007-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 build.tools.tzdb;6263import static build.tools.tzdb.Utils.*;64import static build.tools.tzdb.LocalTime.HOURS_PER_DAY;65import static build.tools.tzdb.LocalTime.MICROS_PER_DAY;66import static build.tools.tzdb.LocalTime.MILLIS_PER_DAY;67import static build.tools.tzdb.LocalTime.MINUTES_PER_DAY;68import static build.tools.tzdb.LocalTime.SECONDS_PER_DAY;69import static build.tools.tzdb.LocalTime.SECONDS_PER_MINUTE;70import static build.tools.tzdb.LocalTime.SECONDS_PER_HOUR;7172import java.util.Objects;7374/**75* A date-time without a time-zone in the ISO-8601 calendar system,76* such as {@code 2007-12-03T10:15:30}.77*78* @since 1.879*/80final class LocalDateTime {8182/**83* The minimum supported {@code LocalDateTime}, '-999999999-01-01T00:00:00'.84* This is the local date-time of midnight at the start of the minimum date.85* This combines {@link LocalDate#MIN} and {@link LocalTime#MIN}.86* This could be used by an application as a "far past" date-time.87*/88public static final LocalDateTime MIN = LocalDateTime.of(LocalDate.MIN, LocalTime.MIN);89/**90* The maximum supported {@code LocalDateTime}, '+999999999-12-31T23:59:59.999999999'.91* This is the local date-time just before midnight at the end of the maximum date.92* This combines {@link LocalDate#MAX} and {@link LocalTime#MAX}.93* This could be used by an application as a "far future" date-time.94*/95public static final LocalDateTime MAX = LocalDateTime.of(LocalDate.MAX, LocalTime.MAX);9697/**98* The date part.99*/100private final LocalDate date;101/**102* The time part.103*/104private final LocalTime time;105106/**107* Obtains an instance of {@code LocalDateTime} from year, month,108* day, hour and minute, setting the second and nanosecond to zero.109* <p>110* The day must be valid for the year and month, otherwise an exception will be thrown.111* The second and nanosecond fields will be set to zero.112*113* @param year the year to represent, from MIN_YEAR to MAX_YEAR114* @param month the month-of-year to represent, from 1 (January) to 12 (December)115* @param dayOfMonth the day-of-month to represent, from 1 to 31116* @param hour the hour-of-day to represent, from 0 to 23117* @param minute the minute-of-hour to represent, from 0 to 59118* @return the local date-time, not null119* @throws DateTimeException if the value of any field is out of range120* @throws DateTimeException if the day-of-month is invalid for the month-year121*/122public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) {123LocalDate date = LocalDate.of(year, month, dayOfMonth);124LocalTime time = LocalTime.of(hour, minute);125return new LocalDateTime(date, time);126}127128/**129* Obtains an instance of {@code LocalDateTime} from a date and time.130*131* @param date the local date, not null132* @param time the local time, not null133* @return the local date-time, not null134*/135public static LocalDateTime of(LocalDate date, LocalTime time) {136Objects.requireNonNull(date, "date");137Objects.requireNonNull(time, "time");138return new LocalDateTime(date, time);139}140141/**142* Obtains an instance of {@code LocalDateTime} using seconds from the143* epoch of 1970-01-01T00:00:00Z.144* <p>145* This allows the {@link ChronoField#INSTANT_SECONDS epoch-second} field146* to be converted to a local date-time. This is primarily intended for147* low-level conversions rather than general application usage.148*149* @param epochSecond the number of seconds from the epoch of 1970-01-01T00:00:00Z150* @param nanoOfSecond the nanosecond within the second, from 0 to 999,999,999151* @param offset the zone offset, not null152* @return the local date-time, not null153* @throws DateTimeException if the result exceeds the supported range154*/155public static LocalDateTime ofEpochSecond(long epochSecond, int nanoOfSecond, ZoneOffset offset) {156Objects.requireNonNull(offset, "offset");157long localSecond = epochSecond + offset.getTotalSeconds(); // overflow caught later158long localEpochDay = floorDiv(localSecond, SECONDS_PER_DAY);159int secsOfDay = (int)floorMod(localSecond, SECONDS_PER_DAY);160LocalDate date = LocalDate.ofEpochDay(localEpochDay);161LocalTime time = LocalTime.ofSecondOfDay(secsOfDay); // ignore nano162return new LocalDateTime(date, time);163}164165/**166* Constructor.167*168* @param date the date part of the date-time, validated not null169* @param time the time part of the date-time, validated not null170*/171private LocalDateTime(LocalDate date, LocalTime time) {172this.date = date;173this.time = time;174}175176/**177* Returns a copy of this date-time with the new date and time, checking178* to see if a new object is in fact required.179*180* @param newDate the date of the new date-time, not null181* @param newTime the time of the new date-time, not null182* @return the date-time, not null183*/184private LocalDateTime with(LocalDate newDate, LocalTime newTime) {185if (date == newDate && time == newTime) {186return this;187}188return new LocalDateTime(newDate, newTime);189}190191/**192* Gets the {@code LocalDate} part of this date-time.193* <p>194* This returns a {@code LocalDate} with the same year, month and day195* as this date-time.196*197* @return the date part of this date-time, not null198*/199public LocalDate getDate() {200return date;201}202203/**204* Gets the year field.205* <p>206* This method returns the primitive {@code int} value for the year.207* <p>208* The year returned by this method is proleptic as per {@code get(YEAR)}.209* To obtain the year-of-era, use {@code get(YEAR_OF_ERA}.210*211* @return the year, from MIN_YEAR to MAX_YEAR212*/213public int getYear() {214return date.getYear();215}216217/**218* Gets the month-of-year field as an int from 1 to 12.219*220* @return the month-of-year221*/222public int getMonth() {223return date.getMonth();224}225226/**227* Gets the day-of-month field.228* <p>229* This method returns the primitive {@code int} value for the day-of-month.230*231* @return the day-of-month, from 1 to 31232*/233public int getDayOfMonth() {234return date.getDayOfMonth();235}236237/**238* Gets the day-of-week field, which is an integer from 1 to 7.239*240* @return the day-of-week, from 1 to 7241*/242public int getDayOfWeek() {243return date.getDayOfWeek();244}245246/**247* Gets the {@code LocalTime} part of this date-time.248* <p>249* This returns a {@code LocalTime} with the same hour, minute, second and250* nanosecond as this date-time.251*252* @return the time part of this date-time, not null253*/254public LocalTime getTime() {255return time;256}257258/**259* Gets the hour-of-day field.260*261* @return the hour-of-day, from 0 to 23262*/263public int getHour() {264return time.getHour();265}266267/**268* Gets the minute-of-hour field.269*270* @return the minute-of-hour, from 0 to 59271*/272public int getMinute() {273return time.getMinute();274}275276/**277* Gets the second-of-minute field.278*279* @return the second-of-minute, from 0 to 59280*/281public int getSecond() {282return time.getSecond();283}284285/**286* Converts this date-time to the number of seconds from the epoch287* of 1970-01-01T00:00:00Z.288* <p>289* This combines this local date-time and the specified offset to calculate the290* epoch-second value, which is the number of elapsed seconds from 1970-01-01T00:00:00Z.291* Instants on the time-line after the epoch are positive, earlier are negative.292* <p>293* This default implementation calculates from the epoch-day of the date and the294* second-of-day of the time.295*296* @param offset the offset to use for the conversion, not null297* @return the number of seconds from the epoch of 1970-01-01T00:00:00Z298*/299public long toEpochSecond(ZoneOffset offset) {300Objects.requireNonNull(offset, "offset");301long epochDay = getDate().toEpochDay();302long secs = epochDay * 86400 + getTime().toSecondOfDay();303secs -= offset.getTotalSeconds();304return secs;305}306307/**308* Returns a copy of this {@code LocalDateTime} with the specified period in days added.309* <p>310* This method adds the specified amount to the days field incrementing the311* month and year fields as necessary to ensure the result remains valid.312* The result is only invalid if the maximum/minimum year is exceeded.313* <p>314* For example, 2008-12-31 plus one day would result in 2009-01-01.315* <p>316* This instance is immutable and unaffected by this method call.317*318* @param days the days to add, may be negative319* @return a {@code LocalDateTime} based on this date-time with the days added, not null320* @throws DateTimeException if the result exceeds the supported date range321*/322public LocalDateTime plusDays(long days) {323LocalDate newDate = date.plusDays(days);324return with(newDate, time);325}326327/**328* Returns a copy of this {@code LocalDateTime} with the specified period in seconds added.329* <p>330* This instance is immutable and unaffected by this method call.331*332* @param seconds the seconds to add, may be negative333* @return a {@code LocalDateTime} based on this date-time with the seconds added, not null334* @throws DateTimeException if the result exceeds the supported date range335*/336public LocalDateTime plusSeconds(long seconds) {337return plusWithOverflow(date, 0, 0, seconds, 1);338}339340/**341* Returns a copy of this {@code LocalDateTime} with the specified period added.342* <p>343* This instance is immutable and unaffected by this method call.344*345* @param newDate the new date to base the calculation on, not null346* @param hours the hours to add, may be negative347* @param minutes the minutes to add, may be negative348* @param seconds the seconds to add, may be negative349* @param nanos the nanos to add, may be negative350* @param sign the sign to determine add or subtract351* @return the combined result, not null352*/353private LocalDateTime plusWithOverflow(LocalDate newDate, long hours, long minutes, long seconds, int sign) {354if ((hours | minutes | seconds) == 0) {355return with(newDate, time);356}357long totDays = seconds / SECONDS_PER_DAY + // max/24*60*60358minutes / MINUTES_PER_DAY + // max/24*60359hours / HOURS_PER_DAY; // max/24360totDays *= sign; // total max*0.4237...361long totSecs = (seconds % SECONDS_PER_DAY) +362(minutes % MINUTES_PER_DAY) * SECONDS_PER_MINUTE +363(hours % HOURS_PER_DAY) * SECONDS_PER_HOUR;364long curSoD = time.toSecondOfDay();365totSecs = totSecs * sign + curSoD; // total 432000000000000366totDays += floorDiv(totSecs, SECONDS_PER_DAY);367368int newSoD = (int)floorMod(totSecs, SECONDS_PER_DAY);369LocalTime newTime = (newSoD == curSoD ? time : LocalTime.ofSecondOfDay(newSoD));370return with(newDate.plusDays(totDays), newTime);371}372373/**374* Compares this date-time to another date-time.375* <p>376* The comparison is primarily based on the date-time, from earliest to latest.377* It is "consistent with equals", as defined by {@link Comparable}.378* <p>379* If all the date-times being compared are instances of {@code LocalDateTime},380* then the comparison will be entirely based on the date-time.381* If some dates being compared are in different chronologies, then the382* chronology is also considered, see {@link ChronoLocalDateTime#compareTo}.383*384* @param other the other date-time to compare to, not null385* @return the comparator value, negative if less, positive if greater386*/387public int compareTo(LocalDateTime other) {388int cmp = date.compareTo(other.getDate());389if (cmp == 0) {390cmp = time.compareTo(other.getTime());391}392return cmp;393}394395/**396* Checks if this date-time is equal to another date-time.397* <p>398* Compares this {@code LocalDateTime} with another ensuring that the date-time is the same.399* Only objects of type {@code LocalDateTime} are compared, other types return false.400*401* @param obj the object to check, null returns false402* @return true if this is equal to the other date-time403*/404@Override405public boolean equals(Object obj) {406if (this == obj) {407return true;408}409if (obj instanceof LocalDateTime) {410LocalDateTime other = (LocalDateTime) obj;411return date.equals(other.date) && time.equals(other.time);412}413return false;414}415416/**417* A hash code for this date-time.418*419* @return a suitable hash code420*/421@Override422public int hashCode() {423return date.hashCode() ^ time.hashCode();424}425426}427428429