Socket
Socket
Sign inDemoInstall

js-joda

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-joda - npm Package Compare versions

Comparing version 0.2.3 to 0.2.4

src/_init.js

3

package.json
{
"name": "js-joda",
"version": "0.2.3",
"version": "0.2.4",
"description": "a date and time library for javascript",

@@ -17,2 +17,3 @@ "repository": {

"build-esdoc": " ./node_modules/.bin/esdoc -c esdoc.json",
"build-dist-es5": "./node_modules/.bin/babel src -d build/es5",
"build-dist": "./node_modules/.bin/webpack --progress --colors --bail && DIST_MIN=1 ./node_modules/.bin/webpack --progress --colors --bail",

@@ -19,0 +20,0 @@ "build-readme2html": "mkdir -p build && ./node_modules/.bin/marked -i README.md -o build/README.hml && ./node_modules/.bin/marked -i CheatSheet.md -o build/CheatSheet.hml "

@@ -64,3 +64,3 @@ Immutable data and time library for javascript

<script>
var LocalDate = jsjoda.LocalDate;
var LocalDate = JSJoda.LocalDate;
var d = LocalDate.parse('2012-12-24').atStartOfDay().plusMonths(2); // 2013-02-24T00:00:00

@@ -67,0 +67,0 @@ </script>

@@ -5,3 +5,3 @@ /**

*/
import {NullPointerException} from './errors';
import {NullPointerException, IllegalArgumentException} from './errors';

@@ -24,1 +24,8 @@ export function assert(assertion, msg, error) {

}
export function requireInstance(value, _class, parameterName) {
if (!(value instanceof _class)) {
throw new IllegalArgumentException(parameterName + ' must be an instance of ' + (_class.name ? _class.name : _class));
}
return value;
}

@@ -33,2 +33,5 @@ import {ChronoField} from '../temporal/ChronoField';

adjustInto(temporal) {
return temporal.with(ChronoField.EPOCH_DAY, this.toEpochDay());
}
}

@@ -16,7 +16,2 @@ /**

const EPOCH_DAY = ChronoField.EPOCH_DAY;
const YEAR = ChronoField.YEAR;
const MONTH_OF_YEAR = ChronoField.MONTH_OF_YEAR;
const DAY_OF_MONTH = ChronoField.DAY_OF_MONTH;
export class IsoChronology extends Enum{

@@ -47,4 +42,4 @@ /**

resolveDate(fieldValues, resolverStyle) {
if (fieldValues.containsKey(EPOCH_DAY)) {
return LocalDate.ofEpochDay(fieldValues.remove(EPOCH_DAY));
if (fieldValues.containsKey(ChronoField.EPOCH_DAY)) {
return LocalDate.ofEpochDay(fieldValues.remove(ChronoField.EPOCH_DAY));
}

@@ -59,4 +54,4 @@

}
updateResolveMap(fieldValues, MONTH_OF_YEAR, Jdk8Methods.floorMod(prolepticMonth, 12) + 1);
updateResolveMap(fieldValues, YEAR, Jdk8Methods.floorDiv(prolepticMonth, 12));
updateResolveMap(fieldValues, ChronoField.MONTH_OF_YEAR, Jdk8Methods.floorMod(prolepticMonth, 12) + 1);
updateResolveMap(fieldValues, ChronoField.YEAR, Jdk8Methods.floorDiv(prolepticMonth, 12));
}

@@ -74,7 +69,7 @@ */

if (era == null) {
Long year = fieldValues.get(YEAR);
Long year = fieldValues.get(ChronoField.YEAR);
if (resolverStyle == ResolverStyle.STRICT) {
// do not invent era if strict, but do cross-check with year
if (year != null) {
updateResolveMap(fieldValues, YEAR, (year > 0 ? yoeLong: Jdk8Methods.safeSubtract(1, yoeLong)));
updateResolveMap(fieldValues, ChronoField.YEAR, (year > 0 ? yoeLong: Jdk8Methods.safeSubtract(1, yoeLong)));
} else {

@@ -86,8 +81,8 @@ // reinstate the field removed earlier, no cross-check issues

// invent era
updateResolveMap(fieldValues, YEAR, (year == null || year > 0 ? yoeLong: Jdk8Methods.safeSubtract(1, yoeLong)));
updateResolveMap(fieldValues, ChronoField.YEAR, (year == null || year > 0 ? yoeLong: Jdk8Methods.safeSubtract(1, yoeLong)));
}
} else if (era.longValue() == 1L) {
updateResolveMap(fieldValues, YEAR, yoeLong);
updateResolveMap(fieldValues, ChronoField.YEAR, yoeLong);
} else if (era.longValue() == 0L) {
updateResolveMap(fieldValues, YEAR, Jdk8Methods.safeSubtract(1, yoeLong));
updateResolveMap(fieldValues, ChronoField.YEAR, Jdk8Methods.safeSubtract(1, yoeLong));
} else {

@@ -102,8 +97,8 @@ throw new DateTimeException("Invalid value for era: " + era);

// build date
if (fieldValues.containsKey(YEAR)) {
if (fieldValues.containsKey(MONTH_OF_YEAR)) {
if (fieldValues.containsKey(DAY_OF_MONTH)) {
var y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
var moy = fieldValues.remove(MONTH_OF_YEAR);
var dom = fieldValues.remove(DAY_OF_MONTH);
if (fieldValues.containsKey(ChronoField.YEAR)) {
if (fieldValues.containsKey(ChronoField.MONTH_OF_YEAR)) {
if (fieldValues.containsKey(ChronoField.DAY_OF_MONTH)) {
var y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
var moy = fieldValues.remove(ChronoField.MONTH_OF_YEAR);
var dom = fieldValues.remove(ChronoField.DAY_OF_MONTH);
if (resolverStyle === ResolverStyle.LENIENT) {

@@ -114,3 +109,3 @@ var months = moy - 1;

} else if (resolverStyle === ResolverStyle.SMART){
DAY_OF_MONTH.checkValidValue(dom);
ChronoField.DAY_OF_MONTH.checkValidValue(dom);
if (moy === 4 || moy === 6 || moy === 9 || moy === 11) {

@@ -129,5 +124,5 @@ dom = Math.min(dom, 30);

if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_MONTH)) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
int y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
if (resolverStyle == ResolverStyle.LENIENT) {
long months = Jdk8Methods.safeSubtract(fieldValues.remove(MONTH_OF_YEAR), 1);
long months = Jdk8Methods.safeSubtract(fieldValues.remove(ChronoField.MONTH_OF_YEAR), 1);
long weeks = Jdk8Methods.safeSubtract(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);

@@ -137,7 +132,7 @@ long days = Jdk8Methods.safeSubtract(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH), 1);

}
int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR));
int moy = ChronoField.MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(ChronoField.MONTH_OF_YEAR));
int aw = ALIGNED_WEEK_OF_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH));
int ad = ALIGNED_DAY_OF_WEEK_IN_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_DAY_OF_WEEK_IN_MONTH));
LocalDate date = LocalDate.of(y, moy, 1).plusDays((aw - 1) * 7 + (ad - 1));
if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
if (resolverStyle == ResolverStyle.STRICT && date.get(ChronoField.MONTH_OF_YEAR) != moy) {
throw new DateTimeException("Strict mode rejected date parsed to a different month");

@@ -148,5 +143,5 @@ }

if (fieldValues.containsKey(DAY_OF_WEEK)) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
int y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
if (resolverStyle == ResolverStyle.LENIENT) {
long months = Jdk8Methods.safeSubtract(fieldValues.remove(MONTH_OF_YEAR), 1);
long months = Jdk8Methods.safeSubtract(fieldValues.remove(ChronoField.MONTH_OF_YEAR), 1);
long weeks = Jdk8Methods.safeSubtract(fieldValues.remove(ALIGNED_WEEK_OF_MONTH), 1);

@@ -156,7 +151,7 @@ long days = Jdk8Methods.safeSubtract(fieldValues.remove(DAY_OF_WEEK), 1);

}
int moy = MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(MONTH_OF_YEAR));
int moy = ChronoField.MONTH_OF_YEAR.checkValidIntValue(fieldValues.remove(ChronoField.MONTH_OF_YEAR));
int aw = ALIGNED_WEEK_OF_MONTH.checkValidIntValue(fieldValues.remove(ALIGNED_WEEK_OF_MONTH));
int dow = DAY_OF_WEEK.checkValidIntValue(fieldValues.remove(DAY_OF_WEEK));
LocalDate date = LocalDate.of(y, moy, 1).plusWeeks(aw - 1).with(nextOrSame(DayOfWeek.of(dow)));
if (resolverStyle == ResolverStyle.STRICT && date.get(MONTH_OF_YEAR) != moy) {
if (resolverStyle == ResolverStyle.STRICT && date.get(ChronoField.MONTH_OF_YEAR) != moy) {
throw new DateTimeException("Strict mode rejected date parsed to a different month");

@@ -171,3 +166,3 @@ }

if (fieldValues.containsKey(DAY_OF_YEAR)) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
int y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
if (resolverStyle == ResolverStyle.LENIENT) {

@@ -184,3 +179,3 @@ long days = Jdk8Methods.safeSubtract(fieldValues.remove(DAY_OF_YEAR), 1);

if (fieldValues.containsKey(ALIGNED_DAY_OF_WEEK_IN_YEAR)) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
int y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
if (resolverStyle == ResolverStyle.LENIENT) {

@@ -194,3 +189,3 @@ long weeks = Jdk8Methods.safeSubtract(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);

LocalDate date = LocalDate.of(y, 1, 1).plusDays((aw - 1) * 7 + (ad - 1));
if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
if (resolverStyle == ResolverStyle.STRICT && date.get(ChronoField.YEAR) != y) {
throw new DateTimeException("Strict mode rejected date parsed to a different year");

@@ -201,3 +196,3 @@ }

if (fieldValues.containsKey(DAY_OF_WEEK)) {
int y = YEAR.checkValidIntValue(fieldValues.remove(YEAR));
int y = ChronoField.YEAR.checkValidIntValue(fieldValues.remove(ChronoField.YEAR));
if (resolverStyle == ResolverStyle.LENIENT) {

@@ -211,3 +206,3 @@ long weeks = Jdk8Methods.safeSubtract(fieldValues.remove(ALIGNED_WEEK_OF_YEAR), 1);

LocalDate date = LocalDate.of(y, 1, 1).plusWeeks(aw - 1).with(nextOrSame(DayOfWeek.of(dow)));
if (resolverStyle == ResolverStyle.STRICT && date.get(YEAR) != y) {
if (resolverStyle == ResolverStyle.STRICT && date.get(ChronoField.YEAR) != y) {
throw new DateTimeException("Strict mode rejected date parsed to a different month");

@@ -225,3 +220,4 @@ }

IsoChronology.INSTANCE = new IsoChronology('IsoChronology');
export function _init() {
IsoChronology.INSTANCE = new IsoChronology('IsoChronology');
}

@@ -6,8 +6,11 @@ /**

*/
import {assert, requireNonNull} from './assert';
import {requireNonNull, requireInstance} from './assert';
import {ArithmeticException, DateTimeParseException, UnsupportedTemporalTypeException} from './errors';
import {MathUtil, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER} from './MathUtil';
import {ChronoField} from './temporal/ChronoField';
import {ChronoUnit} from './temporal/ChronoUnit';
import {ArithmeticException, DateTimeParseException, UnsupportedTemporalTypeException} from './errors';
import {TemporalAmount} from './temporal/TemporalAmount';
import {LocalTime} from './LocalTime';
import {MathUtil, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER} from './MathUtil';

@@ -39,3 +42,3 @@ /**

*/
export class Duration
export class Duration extends TemporalAmount
/*implements TemporalAmount, Comparable<Duration>, Serializable */ {

@@ -51,3 +54,3 @@

constructor(seconds, nanos) {
//super();
super();
this._seconds = seconds;

@@ -976,2 +979,3 @@ this._nanos = nanos;

addTo(temporal) {
requireNonNull(temporal, 'temporal');
if (this._seconds !== 0) {

@@ -1011,2 +1015,3 @@ temporal = temporal.plus(this._seconds, ChronoUnit.SECONDS);

subtractFrom(temporal) {
requireNonNull(temporal, 'temporal');
if (this._seconds !== 0) {

@@ -1110,3 +1115,3 @@ temporal = temporal.minus(this._seconds, ChronoUnit.SECONDS);

requireNonNull(otherDuration, 'otherDuration');
assert(otherDuration instanceof Duration, 'otherDuration must be a Duration');
requireInstance(otherDuration, Duration, 'otherDuration');
var cmp = MathUtil.compareNumbers(this._seconds, otherDuration.seconds());

@@ -1208,5 +1213,8 @@ if (cmp !== 0) {

}
/**
* Constant for a duration of zero.
*/
Duration.ZERO = new Duration(0, 0);
export function _init() {
/**
* Constant for a duration of zero.
*/
Duration.ZERO = new Duration(0, 0);
}

@@ -261,14 +261,10 @@ /**

var ISO_LOCAL_DATE_INSTANCE;
DateTimeFormatter.ISO_LOCAL_DATE = () => {
if (ISO_LOCAL_DATE_INSTANCE == null) {
ISO_LOCAL_DATE_INSTANCE = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.toFormatter(ResolverStyle.STRICT).withChronology(IsoChronology.INSTANCE);
}
return ISO_LOCAL_DATE_INSTANCE;
};
export function _init() {
DateTimeFormatter.ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.toFormatter(ResolverStyle.STRICT).withChronology(IsoChronology.INSTANCE);
}

@@ -15,4 +15,4 @@ /**

// TODO verify the arbitrary values for min/ max seconds, set to 999_999 Years for now
const MIN_SECONDS = -31619087596800; // -999999-01-01
const MAX_SECONDS = 31494784694400; // 999999-12-31
const MIN_SECONDS = -31619087596800; // -999999-01-01T00:00:00
const MAX_SECONDS = 31494784780799; // +999999-12-31T23:59:59
const NANOS_PER_MILLI = 1000000;

@@ -506,4 +506,6 @@

Instant.EPOCH = new Instant(0,0);
Instant.MIN = Instant.ofEpochSecond(MIN_SECONDS, 0);
Instant.MAX = Instant.ofEpochSecond(MAX_SECONDS, 999999999);
export function _init() {
Instant.EPOCH = new Instant(0, 0);
Instant.MIN = Instant.ofEpochSecond(MIN_SECONDS, 0);
Instant.MAX = Instant.ofEpochSecond(MAX_SECONDS, 999999999);
}

@@ -13,4 +13,6 @@ /**

export { Month } from './Month';
export { Period } from './Period';
export { Year } from './Year';
export { ZoneOffset } from './ZoneOffset';
import './_init';

@@ -6,9 +6,10 @@ /**

*/
import {assert} from './assert';
import {assert, requireNonNull, requireInstance} from './assert';
import { MathUtil } from './MathUtil';
import {DateTimeException, UnsupportedTemporalTypeException, NullPointerException} from './errors';
import {MathUtil} from './MathUtil';
import {DateTimeException, UnsupportedTemporalTypeException, NullPointerException, IllegalArgumentException} from './errors';
import { IsoChronology } from './chrono/IsoChronology';
import {IsoChronology} from './chrono/IsoChronology';
import {ChronoField} from './temporal/ChronoField';
import {ChronoUnit} from './temporal/ChronoUnit';
import {ChronoLocalDate} from './chrono/ChronoLocalDate';

@@ -19,8 +20,8 @@ import {TemporalQueries, createTemporalQuery} from './temporal/TemporalQueries';

import {Clock} from './Clock';
import {DayOfWeek} from './DayOfWeek';
import {Month} from './Month';
import {Period} from './Period';
import {Year} from './Year';
import {LocalTime} from './LocalTime';
import './temporal/TemporalQueriesPattern';
/**

@@ -52,21 +53,29 @@ * The number of days in a 400 year cycle.

* such as an offset or time-zone.
*
* The ISO-8601 calendar system is the modern civil calendar system used today
* in most of the world. It is equivalent to the proleptic Gregorian calendar
* system, in which today's rules for leap years are applied for all time.
* For most applications written today, the ISO-8601 rules are entirely suitable.
* However, any application that makes use of historical dates, and requires them
* to be accurate will find the ISO-8601 approach unsuitable.
*/
export class LocalDate extends ChronoLocalDate{
/**
* Obtains the current date from the system clock in the default time-zone or
* if specified, the current date from the specified clock.
*
* @param {number} year
* @param {Month, number} month
* @param {number} dayOfMonth
* This will query the specified clock to obtain the current date - today.
* Using this method allows the use of an alternate clock for testing.
*
* @param clock the clock to use, if null, the system clock and default time-zone is used.
* @return the current date, not null
*/
constructor(year, month, dayOfMonth){
super();
if (month instanceof Month) {
month = month.value();
}
LocalDate.validate(year, month, dayOfMonth);
this._year = year;
this._month = month;
this._day = dayOfMonth;
static now(clock = Clock.systemDefaultZone()) {
assert(clock != null, 'clock', NullPointerException);
var now = clock.instant();
var offset = clock.offset(now);
var epochSec = now.epochSecond() + offset.totalSeconds();
var epochDay = MathUtil.floorDiv(epochSec, LocalTime.SECONDS_PER_DAY);
return LocalDate.ofEpochDay(epochDay);
}

@@ -92,41 +101,215 @@

/**
* Obtains an instance of {@code LocalDate} from a year and day-of-year.
* <p>
* This returns a {@code LocalDate} with the specified year and day-of-year.
* The day-of-year must be valid for the year, otherwise an exception will be thrown.
*
* @return {number} gets the year
* @param {number} year the year to represent, from MIN_YEAR to MAX_YEAR
* @param {number} dayOfYear the day-of-year to represent, from 1 to 366
* @return LocalDate the local date, not null
* @throws DateTimeException if the value of any field is out of range,
* or if the day-of-year is invalid for the year
*/
year() {
return this._year;
static ofYearDay(year, dayOfYear) {
ChronoField.YEAR.checkValidValue(year);
//TODO: ChronoField.DAY_OF_YEAR.checkValidValue(dayOfYear);
var leap = IsoChronology.isLeapYear(year);
if (dayOfYear === 366 && leap === false) {
assert(false, 'Invalid date \'DayOfYear 366\' as \'' + year + '\' is not a leap year', DateTimeException);
}
var moy = Month.of(Math.floor((dayOfYear - 1) / 31 + 1));
var monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
if (dayOfYear > monthEnd) {
moy = moy.plus(1);
}
var dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
return new LocalDate(year, moy.value(), dom);
}
/**
* Obtains an instance of LocalDate from the epoch day count.
*
* @return {number} gets the month
* This returns a LocalDate with the specified epoch-day.
* The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
* of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
*
* @param {number} epochDay - the Epoch Day to convert, based on the epoch 1970-01-01
* @return {LocalDate} the local date, not null
* @throws AssertionError if the epoch days exceeds the supported date range
*/
monthValue() {
return this._month;
static ofEpochDay(epochDay) {
var adjust, adjustCycles, dom, doyEst, marchDoy0, marchMonth0, month, year, yearEst, zeroDay;
zeroDay = epochDay + DAYS_0000_TO_1970;
zeroDay -= 60;
adjust = 0;
if (zeroDay < 0) {
adjustCycles = MathUtil.intDiv(zeroDay + 1, DAYS_PER_CYCLE) - 1;
adjust = adjustCycles * 400;
zeroDay += -adjustCycles * DAYS_PER_CYCLE;
}
yearEst = MathUtil.intDiv(400 * zeroDay + 591, DAYS_PER_CYCLE);
doyEst = zeroDay - (365 * yearEst + MathUtil.intDiv(yearEst, 4) - MathUtil.intDiv(yearEst, 100) + MathUtil.intDiv(yearEst, 400));
if (doyEst < 0) {
yearEst--;
doyEst = zeroDay - (365 * yearEst + MathUtil.intDiv(yearEst, 4) - MathUtil.intDiv(yearEst, 100) + MathUtil.intDiv(yearEst, 400));
}
yearEst += adjust;
marchDoy0 = doyEst;
marchMonth0 = MathUtil.intDiv(marchDoy0 * 5 + 2, 153);
month = (marchMonth0 + 2) % 12 + 1;
dom = marchDoy0 - MathUtil.intDiv(marchMonth0 * 306 + 5, 10) + 1;
yearEst += MathUtil.intDiv(marchMonth0, 10);
year = yearEst;
return new LocalDate(year, month, dom);
}
month() {
return Month.of(this._month);
/**
* Obtains an instance of {@code LocalDate} from a temporal object.
* <p>
* A {@code TemporalAccessor} represents some form of date and time information.
* This factory converts the arbitrary temporal object to an instance of {@code LocalDate}.
* <p>
* The conversion uses the {@link TemporalQueries#localDate()} query, which relies
* on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
* <p>
* This method matches the signature of the functional interface {@link TemporalQuery}
* allowing it to be used as a query via method reference, {@code LocalDate::from}.
*
* @param temporal the temporal object to convert, not null
* @return the local date, not null
* @throws DateTimeException if unable to convert to a {@code LocalDate}
*/
static from(temporal) {
assert(temporal != null, '', NullPointerException);
var date = temporal.query(TemporalQueries.localDate());
if (date == null) {
throw new DateTimeException(
`Unable to obtain LocalDate from TemporalAccessor: ${temporal}, type ${temporal.constructor != null ? temporal.constructor.name : ''}`);
}
return date;
}
/**
* Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
*
* @return {number} gets the day of month
* The text is parsed using the formatter, returning a date.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, default is DateTimeFormatter.ISO_LOCAL_DATE
* @return the parsed local date, not null
* @throws DateTimeParseException if the text cannot be parsed
*/
dayOfMonth() {
return this._day;
static parse(text, formatter = DateTimeFormatter.ISO_LOCAL_DATE){
assert(formatter != null, 'formatter', NullPointerException);
return formatter.parse(text, LocalDate.FROM);
}
/**
* Gets the chronology of this date, which is the ISO calendar system.
* Resolves the date, resolving days past the end of month.
*
* @param year the year to represent, validated from MIN_YEAR to MAX_YEAR
* @param month the month-of-year to represent, validated from 1 to 12
* @param day the day-of-month to represent, validated from 1 to 31
* @return LocalDate resolved date, not null
*/
static _resolvePreviousValid(year, month, day) {
switch (month) {
case 2:
day = Math.min(day, IsoChronology.isLeapYear(year) ? 29 : 28);
break;
case 4:
case 6:
case 9:
case 11:
day = Math.min(day, 30);
break;
}
return LocalDate.of(year, month, day);
}
/**
*
* @param {number} year
* @param {Month, number} month
* @param {number} dayOfMonth
*/
constructor(year, month, dayOfMonth){
super();
if (month instanceof Month) {
month = month.value();
}
LocalDate._validate(year, month, dayOfMonth);
this._year = year;
this._month = month;
this._day = dayOfMonth;
}
/**
* @private
*/
static _validate(year, month, dayOfMonth) {
var dom;
ChronoField.YEAR.checkValidValue(year);
ChronoField.MONTH_OF_YEAR.checkValidValue(month);
ChronoField.DAY_OF_MONTH.checkValidValue(dayOfMonth);
if (dayOfMonth > 28) {
dom = 31;
switch (month) {
case 2:
dom = IsoChronology.isLeapYear(year) ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
dom = 30;
}
if (dayOfMonth > dom) {
if (dayOfMonth === 29) {
assert(false, 'Invalid date \'February 29\' as \'' + year + '\' is not a leap year', DateTimeException);
} else {
assert(false, 'Invalid date \'' + year + '\' \'' + month + '\' \'' + dayOfMonth + '\'', DateTimeException);
}
}
}
}
/**
* Checks if the specified field is supported.
* <p>
* The {@code Chronology} represents the calendar system in use.
* The ISO-8601 calendar system is the modern civil calendar system used today
* in most of the world. It is equivalent to the proleptic Gregorian calendar
* system, in which todays's rules for leap years are applied for all time.
* This checks if this date can be queried for the specified field.
* If false, then calling the {@link #range(TemporalField) range} and
* {@link #get(TemporalField) get} methods will throw an exception.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this date-time.
* The supported fields are:
* <ul>
* <li>{@code DAY_OF_WEEK}
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
* <li>{@code DAY_OF_MONTH}
* <li>{@code DAY_OF_YEAR}
* <li>{@code EPOCH_DAY}
* <li>{@code ALIGNED_WEEK_OF_MONTH}
* <li>{@code ALIGNED_WEEK_OF_YEAR}
* <li>{@code MONTH_OF_YEAR}
* <li>{@code EPOCH_MONTH}
* <li>{@code YEAR_OF_ERA}
* <li>{@code YEAR}
* <li>{@code ERA}
* </ul>
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @return the ISO chronology, not null
* @param field the field to check, null returns false
* @return true if the field is supported on this date, false if not
*/
chronology() {
return IsoChronology.INSTANCE;
isSupported(field) {
return super.isSupported(field);
}

@@ -148,15 +331,15 @@

switch (field) {
// case ChronoField.DAY_OF_WEEK: return this.dayOfWeek().getValue();
// case ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((day - 1) % 7) + 1;
// case ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((this.dayOfYear() - 1) % 7) + 1;
case ChronoField.DAY_OF_WEEK: return this.dayOfWeek().value();
case ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH: return ((this._day - 1) % 7) + 1;
case ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR: return ((this.dayOfYear() - 1) % 7) + 1;
case ChronoField.DAY_OF_MONTH: return this._day;
// case ChronoField.DAY_OF_YEAR: return this.dayOfYear();
case ChronoField.DAY_OF_YEAR: return this.dayOfYear();
case ChronoField.EPOCH_DAY: return this.toEpochDay();
// case ChronoField.ALIGNED_WEEK_OF_MONTH: return ((this._day - 1) / 7) + 1;
// case ChronoField.ALIGNED_WEEK_OF_YEAR: return ((this.dayOfYear() - 1) / 7) + 1;
case ChronoField.ALIGNED_WEEK_OF_MONTH: return ((this._day - 1) / 7) + 1;
case ChronoField.ALIGNED_WEEK_OF_YEAR: return ((this.dayOfYear() - 1) / 7) + 1;
case ChronoField.MONTH_OF_YEAR: return this._month;
// case ChronoField.PROLEPTIC_MONTH: this.prolepticMonth();
// case ChronoField.YEAR_OF_ERA: return (this._year >= 1 ? this._year : 1 - this._year);
case ChronoField.PROLEPTIC_MONTH: return this._prolepticMonth();
case ChronoField.YEAR_OF_ERA: return (this._year >= 1 ? this._year : 1 - this._year);
case ChronoField.YEAR: return this._year;
// case ChronoField.ERA: return (this._year >= 1 ? 1 : 0);
case ChronoField.ERA: return (this._year >= 1 ? 1 : 0);
}

@@ -166,3 +349,542 @@ throw new UnsupportedTemporalTypeException('Unsupported field: ' + field);

_prolepticMonth() {
return (this._year * 12) + (this._month - 1);
}
/**
* Gets the chronology of this date, which is the ISO calendar system.
* <p>
* The {@code Chronology} represents the calendar system in use.
* The ISO-8601 calendar system is the modern civil calendar system used today
* in most of the world. It is equivalent to the proleptic Gregorian calendar
* system, in which todays's rules for leap years are applied for all time.
*
* @return the ISO chronology, not null
*/
chronology() {
return IsoChronology.INSTANCE;
}
/**
*
* @return {number} gets the year
*/
year() {
return this._year;
}
/**
*
* @return {number} gets the month
*/
monthValue() {
return this._month;
}
month() {
return Month.of(this._month);
}
/**
*
* @return {number} gets the day of month
*/
dayOfMonth() {
return this._day;
}
/**
* Gets the day-of-year field.
* <p>
* This method returns the primitive {@code int} value for the day-of-year.
*
* @return the day-of-year, from 1 to 365, or 366 in a leap year
*/
dayOfYear() {
return this.month().firstDayOfYear(this.isLeapYear()) + this._day - 1;
}
/**
* Gets the day-of-week field, which is an enum {@code DayOfWeek}.
* <p>
* This method returns the enum {@link DayOfWeek} for the day-of-week.
* This avoids confusion as to what {@code int} values mean.
* If you need access to the primitive {@code int} value then the enum
* provides the {@link DayOfWeek#getValue() int value}.
* <p>
* Additional information can be obtained from the {@code DayOfWeek}.
* This includes textual names of the values.
*
* @return the day-of-week, not null
*/
dayOfWeek() {
var dow0 = MathUtil.floorMod(this.toEpochDay() + 3, 7);
return DayOfWeek.of(dow0 + 1);
}
/**
* Checks if the year is a leap year, according to the ISO proleptic
* calendar system rules.
* <p>
* This method applies the current rules for leap years across the whole time-line.
* In general, a year is a leap year if it is divisible by four without
* remainder. However, years divisible by 100, are not leap years, with
* the exception of years divisible by 400 which are.
* <p>
* For example, 1904 is a leap year it is divisible by 4.
* 1900 was not a leap year as it is divisible by 100, however 2000 was a
* leap year as it is divisible by 400.
* <p>
* The calculation is proleptic - applying the same rules into the far future and far past.
* This is historically inaccurate, but is correct for the ISO-8601 standard.
*
* @return true if the year is leap, false otherwise
*/
isLeapYear() {
return IsoChronology.isLeapYear(this._year);
}
/**
* Returns the length of the month represented by this date.
* <p>
* This returns the length of the month in days.
* For example, a date in January would return 31.
*
* @return the length of the month in days
*/
lengthOfMonth() {
switch (this._month) {
case 2:
return (this.isLeapYear() ? 29 : 28);
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
/**
* Returns the length of the year represented by this date.
* <p>
* This returns the length of the year in days, either 365 or 366.
*
* @return 366 if the year is leap, 365 otherwise
*/
lengthOfYear() {
return (this.isLeapYear() ? 366 : 365);
}
/**
* function overloading for the with method.
*
* calling "with" with one (or less) argument, assumes that the argument is an TemporalAdjuster,
* otherwise a field and newValue argument is expected.
*
* @param fieldOrAdjuster
* @param newValue
*/
with(fieldOrAdjuster, newValue){
if(arguments.length < 2){
return this._withTemporalAdjuster(fieldOrAdjuster);
} else {
return this._with2(fieldOrAdjuster, newValue);
}
}
/**
* Returns an adjusted copy of this date.
* <p>
* This returns a new {@code LocalDate}, based on this one, with the date adjusted.
* The adjustment takes place using the specified adjuster strategy object.
* Read the documentation of the adjuster to understand what adjustment will be made.
* <p>
* A simple adjuster might simply set the one of the fields, such as the year field.
* A more complex adjuster might set the date to the last day of the month.
* A selection of common adjustments is provided in {@link TemporalAdjusters}.
* These include finding the "last day of the month" and "next Wednesday".
* Key date-time classes also implement the {@code TemporalAdjuster} interface,
* such as {@link Month} and {@link MonthDay}.
* The adjuster is responsible for handling special cases, such as the varying
* lengths of month and leap years.
* <p>
* For example this code returns a date on the last day of July:
* <pre>
* import static org.threeten.bp.Month.*;
* import static org.threeten.bp.temporal.Adjusters.*;
*
* result = localDate.with(JULY).with(lastDayOfMonth());
* </pre>
* <p>
* The result of this method is obtained by invoking the
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the
* specified adjuster passing {@code this} as the argument.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return a {@code LocalDate} based on {@code this} with the adjustment made, not null
* @throws DateTimeException if the adjustment cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_withTemporalAdjuster(adjuster) {
assert(adjuster != null, 'adjuster', NullPointerException);
assert(typeof adjuster.adjustInto === 'function', adjuster + 'is mot an adjuster', IllegalArgumentException);
// optimizations
if (adjuster instanceof LocalDate) {
return adjuster;
}
return adjuster.adjustInto(this);
}
/**
* Returns a copy of this date with the specified field set to a new value.
* <p>
* This returns a new {@code LocalDate}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the year, month or day-of-month.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* In some cases, changing the specified field can cause the resulting date to become invalid,
* such as changing the month from 31st January to February would make the day-of-month invalid.
* In cases like this, the field is responsible for resolving the date. Typically it will choose
* the previous valid date, which would be the last valid day of February in this example.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* The supported fields behave as follows:
* <ul>
* <li>{@code DAY_OF_WEEK} -
* Returns a {@code LocalDate} with the specified day-of-week.
* The date is adjusted up to 6 days forward or backward within the boundary
* of a Monday to Sunday week.
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH} -
* Returns a {@code LocalDate} with the specified aligned-day-of-week.
* The date is adjusted to the specified month-based aligned-day-of-week.
* Aligned weeks are counted such that the first week of a given month starts
* on the first day of that month.
* This may cause the date to be moved up to 6 days into the following month.
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR} -
* Returns a {@code LocalDate} with the specified aligned-day-of-week.
* The date is adjusted to the specified year-based aligned-day-of-week.
* Aligned weeks are counted such that the first week of a given year starts
* on the first day of that year.
* This may cause the date to be moved up to 6 days into the following year.
* <li>{@code DAY_OF_MONTH} -
* Returns a {@code LocalDate} with the specified day-of-month.
* The month and year will be unchanged. If the day-of-month is invalid for the
* year and month, then a {@code DateTimeException} is thrown.
* <li>{@code DAY_OF_YEAR} -
* Returns a {@code LocalDate} with the specified day-of-year.
* The year will be unchanged. If the day-of-year is invalid for the
* year, then a {@code DateTimeException} is thrown.
* <li>{@code EPOCH_DAY} -
* Returns a {@code LocalDate} with the specified epoch-day.
* This completely replaces the date and is equivalent to {@link #ofEpochDay(long)}.
* <li>{@code ALIGNED_WEEK_OF_MONTH} -
* Returns a {@code LocalDate} with the specified aligned-week-of-month.
* Aligned weeks are counted such that the first week of a given month starts
* on the first day of that month.
* This adjustment moves the date in whole week chunks to match the specified week.
* The result will have the same day-of-week as this date.
* This may cause the date to be moved into the following month.
* <li>{@code ALIGNED_WEEK_OF_YEAR} -
* Returns a {@code LocalDate} with the specified aligned-week-of-year.
* Aligned weeks are counted such that the first week of a given year starts
* on the first day of that year.
* This adjustment moves the date in whole week chunks to match the specified week.
* The result will have the same day-of-week as this date.
* This may cause the date to be moved into the following year.
* <li>{@code MONTH_OF_YEAR} -
* Returns a {@code LocalDate} with the specified month-of-year.
* The year will be unchanged. The day-of-month will also be unchanged,
* unless it would be invalid for the new month and year. In that case, the
* day-of-month is adjusted to the maximum valid value for the new month and year.
* <li>{@code PROLEPTIC_MONTH} -
* Returns a {@code LocalDate} with the specified proleptic-month.
* The day-of-month will be unchanged, unless it would be invalid for the new month
* and year. In that case, the day-of-month is adjusted to the maximum valid value
* for the new month and year.
* <li>{@code YEAR_OF_ERA} -
* Returns a {@code LocalDate} with the specified year-of-era.
* The era and month will be unchanged. The day-of-month will also be unchanged,
* unless it would be invalid for the new month and year. In that case, the
* day-of-month is adjusted to the maximum valid value for the new month and year.
* <li>{@code YEAR} -
* Returns a {@code LocalDate} with the specified year.
* The month will be unchanged. The day-of-month will also be unchanged,
* unless it would be invalid for the new month and year. In that case, the
* day-of-month is adjusted to the maximum valid value for the new month and year.
* <li>{@code ERA} -
* Returns a {@code LocalDate} with the specified era.
* The year-of-era and month will be unchanged. The day-of-month will also be unchanged,
* unless it would be invalid for the new month and year. In that case, the
* day-of-month is adjusted to the maximum valid value for the new month and year.
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code LocalDate} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws ArithmeticException if numeric overflow occurs
*/
_with2(field, newValue) {
assert(field != null, 'field', NullPointerException);
if (field instanceof ChronoField) {
var f = field;
f.checkValidValue(newValue);
switch (f) {
case ChronoField.DAY_OF_WEEK: return this.plusDays(newValue - this.getDayOfWeek().value());
case ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH: return this.plusDays(newValue - this.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
case ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR: return this.plusDays(newValue - this.getLong(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
case ChronoField.DAY_OF_MONTH: return this.withDayOfMonth(newValue);
case ChronoField.DAY_OF_YEAR: return this.withDayOfYear(newValue);
case ChronoField.EPOCH_DAY: return LocalDate.ofEpochDay(newValue);
case ChronoField.ALIGNED_WEEK_OF_MONTH: return this.plusWeeks(newValue - this.getLong(ChronoField.ALIGNED_WEEK_OF_MONTH));
case ChronoField.ALIGNED_WEEK_OF_YEAR: return this.plusWeeks(newValue - this.getLong(ChronoField.ALIGNED_WEEK_OF_YEAR));
case ChronoField.MONTH_OF_YEAR: return this.withMonth(newValue);
case ChronoField.PROLEPTIC_MONTH: return this.plusMonths(newValue - this.getLong(ChronoField.PROLEPTIC_MONTH));
case ChronoField.YEAR_OF_ERA: return this.withYear((this._year >= 1 ? newValue : 1 - newValue));
case ChronoField.YEAR: return this.withYear(newValue);
case ChronoField.ERA: return (this.getLong(ChronoField.ERA) === newValue ? this : this.withYear(1 - this._year));
}
throw new UnsupportedTemporalTypeException('Unsupported field: ' + field);
}
return field.adjustInto(this, newValue);
}
/**
* Returns a copy of this date with the year altered.
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param year the year to set in the result, from MIN_YEAR to MAX_YEAR
* @return a {@code LocalDate} based on this date with the requested year, not null
* @throws DateTimeException if the year value is invalid
*/
withYear(year) {
if (this._year === year) {
return this;
}
ChronoField.YEAR.checkValidValue(year);
return LocalDate._resolvePreviousValid(year, this._month, this._day);
}
/**
* Returns a copy of this date with the month-of-year altered.
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
*
* This instance is immutable and unaffected by this method call.
*
* @param month the month-of-year to set in the result, from 1 (January) to 12 (December)
* @return a {@code LocalDate} based on this date with the requested month, not null
* @throws DateTimeException if the month-of-year value is invalid
*/
withMonth(month) {
var m = (month instanceof Month) ? month.value() : month;
if (this._month === m) {
return this;
}
ChronoField.MONTH_OF_YEAR.checkValidValue(m);
return LocalDate._resolvePreviousValid(this._year, m, this._day);
}
/**
* Returns a copy of this {@code LocalDate} with the day-of-month altered.
* <p>
* If the resulting date is invalid, an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param {number} dayOfMonth the day-of-month to set in the result, from 1 to 28-31
* @return {LocalDate} based on this date with the requested day, not null
* @throws DateTimeException if the day-of-month value is invalid,
* or if the day-of-month is invalid for the month-year
*/
withDayOfMonth(dayOfMonth) {
if (this._day === dayOfMonth) {
return this;
}
return LocalDate.of(this._year, this._month, dayOfMonth);
}
/**
* Returns a copy of this date with the day-of-year altered.
* If the resulting date is invalid, an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param dayOfYear the day-of-year to set in the result, from 1 to 365-366
* @return a {@code LocalDate} based on this date with the requested day, not null
* @throws DateTimeException if the day-of-year value is invalid
* @throws DateTimeException if the day-of-year is invalid for the year
*/
withDayOfYear(dayOfYear) {
if (this.dayOfYear() === dayOfYear) {
return this;
}
return LocalDate.ofYearDay(this._year, dayOfYear);
}
/**
* function overloading for plus
*/
plus(p1, p2){
if(arguments.length < 2){
return this._plus1(p1);
} else {
return this._plus2(p1, p2);
}
}
/**
* Returns a copy of this date with the specified period added.
* <p>
* This method returns a new date based on this date with the specified period added.
* The amount is typically {@link Period} but may be any other type implementing
* the {@link TemporalAmount} interface.
* The calculation is delegated to the specified adjuster, which typically calls
* back to {@link #plus(long, TemporalUnit)}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to add, not null
* @return a {@code LocalDate} based on this date with the addition made, not null
* @throws DateTimeException if the addition cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_plus1(amount) {
requireNonNull(amount, 'amount');
return amount.addTo(this);
}
/**
* Returns a copy of this date with the specified period added.
* <p>
* This method returns a new date based on this date with the specified period added.
* This can be used to add any period that is defined by a unit, for example to add years, months or days.
* The unit is responsible for the details of the calculation, including the resolution
* of any edge cases in the calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToAdd the amount of the unit to add to the result, may be negative
* @param unit the unit of the period to add, not null
* @return a {@code LocalDate} based on this date with the specified period added, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
_plus2(amountToAdd, unit) {
requireNonNull(amountToAdd, 'amountToAdd');
requireNonNull(unit, 'unit');
if (unit instanceof ChronoUnit) {
switch (unit) {
case ChronoUnit.DAYS: return this.plusDays(amountToAdd);
case ChronoUnit.WEEKS: return this.plusWeeks(amountToAdd);
case ChronoUnit.MONTHS: return this.plusMonths(amountToAdd);
case ChronoUnit.YEARS: return this.plusYears(amountToAdd);
case ChronoUnit.DECADES: return this.plusYears(MathUtil.safeMultiply(amountToAdd, 10));
case ChronoUnit.CENTURIES: return this.plusYears(MathUtil.safeMultiply(amountToAdd, 100));
case ChronoUnit.MILLENNIA: return this.plusYears(MathUtil.safeMultiply(amountToAdd, 1000));
case ChronoUnit.ERAS: return this.with(ChronoField.ERA, MathUtil.safeAdd(this.getLong(ChronoField.ERA), amountToAdd));
}
throw new UnsupportedTemporalTypeException('Unsupported unit: ' + unit);
}
return unit.addTo(this, amountToAdd);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in years added.
* <p>
* This method adds the specified amount to the years field in three steps:
* <ol>
* <li>Add the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) plus one year would result in the
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2009-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param yearsToAdd the years to add, may be negative
* @return a {@code LocalDate} based on this date with the years added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
plusYears(yearsToAdd) {
if (yearsToAdd === 0) {
return this;
}
var newYear = ChronoField.YEAR.checkValidIntValue(this._year + yearsToAdd); // safe overflow
return LocalDate._resolvePreviousValid(newYear, this._month, this._day);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in months added.
* <p>
* This method adds the specified amount to the months field in three steps:
* <ol>
* <li>Add the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 plus one month would result in the invalid date
* 2007-04-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-04-30, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthsToAdd the months to add, may be negative
* @return a {@code LocalDate} based on this date with the months added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
plusMonths(monthsToAdd) {
if (monthsToAdd === 0) {
return this;
}
var monthCount = this._year * 12 + (this._month - 1);
var calcMonths = monthCount + monthsToAdd; // safe overflow
var newYear = ChronoField.YEAR.checkValidIntValue(MathUtil.floorDiv(calcMonths, 12));
var newMonth = MathUtil.floorMod(calcMonths, 12) + 1;
return LocalDate._resolvePreviousValid(newYear, newMonth, this._day);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in weeks added.
* <p>
* This method adds the specified amount in weeks to the days field incrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2008-12-31 plus one week would result in 2009-01-07.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeksToAdd the weeks to add, may be negative
* @return a {@code LocalDate} based on this date with the weeks added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
plusWeeks(weeksToAdd) {
return this.plusDays(MathUtil.safeMultiply(weeksToAdd, 7));
}
/**
* Returns a copy of this LocalDate with the specified number of days added.

@@ -186,6 +908,126 @@ *

}
var mjDay = this.toEpochDay() + daysToAdd;
var mjDay = MathUtil.safeAdd(this.toEpochDay(), daysToAdd);
return LocalDate.ofEpochDay(mjDay);
}
/**
* function overloading for minus
*/
minus(p1, p2){
if(arguments.length < 2){
return this._minus1(p1);
} else {
return this._minus2(p1, p2);
}
}
/**
* Returns a copy of this date with the specified period subtracted.
* <p>
* This method returns a new date based on this date with the specified period subtracted.
* The amount is typically {@link Period} but may be any other type implementing
* the {@link TemporalAmount} interface.
* The calculation is delegated to the specified adjuster, which typically calls
* back to {@link #minus(long, TemporalUnit)}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to subtract, not null
* @return a {@code LocalDate} based on this date with the subtraction made, not null
* @throws DateTimeException if the subtraction cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_minus1(amount) {
requireNonNull(amount, 'amount');
return amount.subtractFrom(this);
}
/**
* Returns a copy of this date with the specified period subtracted.
* <p>
* This method returns a new date based on this date with the specified period subtracted.
* This can be used to subtract any period that is defined by a unit, for example to subtract years, months or days.
* The unit is responsible for the details of the calculation, including the resolution
* of any edge cases in the calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
* @param unit the unit of the period to subtract, not null
* @return a {@code LocalDate} based on this date with the specified period subtracted, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
_minus2(amountToSubtract, unit) {
requireNonNull(amountToSubtract, 'amountToSubtract');
requireNonNull(unit, 'unit');
return this._plus2(-1 * amountToSubtract, unit);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in years subtracted.
* <p>
* This method subtracts the specified amount from the years field in three steps:
* <ol>
* <li>Subtract the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) minus one year would result in the
* invalid date 2007-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2007-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param yearsToSubtract the years to subtract, may be negative
* @return a {@code LocalDate} based on this date with the years subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
minusYears(yearsToSubtract) {
return this.plusYears(yearsToSubtract * -1);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in months subtracted.
* <p>
* This method subtracts the specified amount from the months field in three steps:
* <ol>
* <li>Subtract the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 minus one month would result in the invalid date
* 2007-02-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthsToSubtract the months to subtract, may be negative
* @return a {@code LocalDate} based on this date with the months subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
minusMonths(monthsToSubtract) {
return this.plusMonths(monthsToSubtract * -1);
}
/**
* Returns a copy of this {@code LocalDate} with the specified period in weeks subtracted.
* <p>
* This method subtracts the specified amount in weeks from the days field decrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2009-01-07 minus one week would result in 2008-12-31.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeksToSubtract the weeks to subtract, may be negative
* @return a {@code LocalDate} based on this date with the weeks subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
minusWeeks(weeksToSubtract) {
return this.plusWeeks(weeksToSubtract * -1);
}
/*

@@ -211,2 +1053,189 @@ * Returns a copy of this LocalDate with the specified number of days subtracted.

/**
* Queries this date using the specified query.
*
* This queries this date using the specified query strategy object.
* The {@code TemporalQuery} object defines the logic to be used to
* obtain the result. Read the documentation of the query to understand
* what the result of this method will be.
*
* The result of this method is obtained by invoking the
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
* specified query passing {@code this} as the argument.
*
* @param query the query to invoke, not null
* @return the query result, null may be returned (defined by the query)
* @throws DateTimeException if unable to query (defined by the query)
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
query(query) {
assert(query != null, '', NullPointerException);
if (query === TemporalQueries.localDate()) {
return this;
}
return super.query(query);
}
/**
* Adjusts the specified temporal object to have the same date as this object.
* <p>
* This returns a temporal object of the same observable type as the input
* with the date changed to be the same as this.
* <p>
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
* passing {@link ChronoField#EPOCH_DAY} as the field.
* <p>
* In most cases, it is clearer to reverse the calling pattern by using
* {@link Temporal#with(TemporalAdjuster)}:
* <pre>
* // these two lines are equivalent, but the second approach is recommended
* temporal = thisLocalDate.adjustInto(temporal);
* temporal = temporal.with(thisLocalDate);
* </pre>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param temporal the target object to be adjusted, not null
* @return the adjusted object, not null
* @throws DateTimeException if unable to make the adjustment
* @throws ArithmeticException if numeric overflow occurs
*/
adjustInto(temporal) {
return super.adjustInto(temporal);
}
/**
* until function overloading
*/
until(p1, p2){
if(arguments.length < 2){
return this._until1(p1);
} else {
return this._until2(p1, p2);
}
}
/**
* Calculates the period between this date and another date in
* terms of the specified unit.
* <p>
* This calculates the period between two dates in terms of a single unit.
* The start and end points are {@code this} and the specified date.
* The result will be negative if the end is before the start.
* The {@code Temporal} passed to this method must be a {@code LocalDate}.
* For example, the period in days between two dates can be calculated
* using {@code startDate.until(endDate, DAYS)}.
* <p>
* The calculation returns a whole number, representing the number of
* complete units between the two dates.
* For example, the period in months between 2012-06-15 and 2012-08-14
* will only be one month as it is one day short of two months.
* <p>
* This method operates in association with {@link TemporalUnit#between}.
* The result of this method is a {@code long} representing the amount of
* the specified unit. By contrast, the result of {@code between} is an
* object that can be used directly in addition/subtraction:
* <pre>
* long period = start.until(end, MONTHS); // this method
* dateTime.plus(MONTHS.between(start, end)); // use in plus/minus
* </pre>
* <p>
* The calculation is implemented in this method for {@link ChronoUnit}.
* The units {@code DAYS}, {@code WEEKS}, {@code MONTHS}, {@code YEARS},
* {@code DECADES}, {@code CENTURIES}, {@code MILLENNIA} and {@code ERAS}
* are supported. Other {@code ChronoUnit} values will throw an exception.
* <p>
* If the unit is not a {@code ChronoUnit}, then the result of this method
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
* passing {@code this} as the first argument and the input temporal as
* the second argument.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param endExclusive the end date, which is converted to a {@code LocalDate}, not null
* @param unit the unit to measure the period in, not null
* @return the amount of the period between this date and the end date
* @throws DateTimeException if the period cannot be calculated
* @throws ArithmeticException if numeric overflow occurs
*/
_until2(endExclusive, unit) {
var end = LocalDate.from(endExclusive);
if (unit instanceof ChronoUnit) {
switch (unit) {
case ChronoUnit.DAYS: return this.daysUntil(end);
case ChronoUnit.WEEKS: return this.daysUntil(end) / 7;
case ChronoUnit.MONTHS: return this.monthsUntil(end);
case ChronoUnit.YEARS: return this.monthsUntil(end) / 12;
case ChronoUnit.DECADES: return this.monthsUntil(end) / 120;
case ChronoUnit.CENTURIES: return this.monthsUntil(end) / 1200;
case ChronoUnit.MILLENNIA: return this.monthsUntil(end) / 12000;
case ChronoUnit.ERAS: return end.getLong(ChronoField.ERA) - this.getLong(ChronoField.ERA);
}
throw new UnsupportedTemporalTypeException('Unsupported unit: ' + unit);
}
return unit.between(this, end);
}
daysUntil(end) {
return end.toEpochDay() - this.toEpochDay(); // no overflow
}
monthsUntil(end) {
var packed1 = this._prolepticMonth() * 32 + this.dayOfMonth(); // no overflow
var packed2 = end._prolepticMonth() * 32 + end.dayOfMonth(); // no overflow
return MathUtil.floorDiv((packed2 - packed1), 32);
}
/**
* Calculates the period between this date and another date as a {@code Period}.
* <p>
* This calculates the period between two dates in terms of years, months and days.
* The start and end points are {@code this} and the specified date.
* The result will be negative if the end is before the start.
* <p>
* The calculation is performed using the ISO calendar system.
* If necessary, the input date will be converted to ISO.
* <p>
* The start date is included, but the end date is not.
* The period is calculated by removing complete months, then calculating
* the remaining number of days, adjusting to ensure that both have the same sign.
* The number of months is then normalized into years and months based on a 12 month year.
* A month is considered to be complete if the end day-of-month is greater
* than or equal to the start day-of-month.
* For example, from {@code 2010-01-15} to {@code 2011-03-18} is "1 year, 2 months and 3 days".
* <p>
* The result of this method can be a negative period if the end is before the start.
* The negative sign will be the same in each of year, month and day.
* <p>
* There are two equivalent ways of using this method.
* The first is to invoke this method.
* The second is to use {@link Period#between(LocalDate, LocalDate)}:
* <pre>
* // these two lines are equivalent
* period = start.until(end);
* period = Period.between(start, end);
* </pre>
* The choice should be made based on which makes the code more readable.
*
* @param endDate the end date, exclusive, which may be in any chronology, not null
* @return the period between this date and the end date, not null
*/
_until1(endDate) {
var end = LocalDate.from(endDate);
var totalMonths = end._prolepticMonth() - this._prolepticMonth(); // safe
var days = end._day - this._day;
if (totalMonths > 0 && days < 0) {
totalMonths--;
var calcDate = this.plusMonths(totalMonths);
days = (end.toEpochDay() - calcDate.toEpochDay()); // safe
} else if (totalMonths < 0 && days > 0) {
totalMonths++;
days -= end.lengthOfMonth();
}
var years = MathUtil.intDiv(totalMonths, 12); // safe
var months = MathUtil.intMod(totalMonths, 12); // safe
return Period.of(MathUtil.safeToInt(years), months, days);
}
/**
* Converts this date to the Epoch Day.

@@ -241,32 +1270,111 @@ *

/**
* Obtains the current date from the system clock in the default time-zone or
* if specified, the current date from the specified clock.
* Compares this date to another date.
* <p>
* The comparison is primarily based on the date, from earliest to latest.
* It is "consistent with equals", as defined by {@link Comparable}.
* <p>
* If all the dates being compared are instances of {@code LocalDate},
* then the comparison will be entirely based on the date.
* If some dates being compared are in different chronologies, then the
* chronology is also considered, see {@link ChronoLocalDate#compareTo}.
*
* This will query the specified clock to obtain the current date - today.
* Using this method allows the use of an alternate clock for testing.
* @param other the other date to compare to, not null
* @return the comparator value, negative if less, positive if greater
*/
compareTo(other) {
requireNonNull(other, 'other');
requireInstance(other, LocalDate, 'other');
if (other instanceof LocalDate) {
return this._compareTo0(other);
}
// super.compareTo(other);
}
_compareTo0(otherDate) {
var cmp = (this._year - otherDate._year);
if (cmp === 0) {
cmp = (this._month - otherDate._month);
if (cmp === 0) {
cmp = (this._day - otherDate._day);
}
}
return cmp;
}
/**
* Checks if this date is after the specified date.
* <p>
* This checks to see if this date represents a point on the
* local time-line after the other date.
* <pre>
* LocalDate a = LocalDate.of(2012, 6, 30);
* LocalDate b = LocalDate.of(2012, 7, 1);
* a.isAfter(b) == false
* a.isAfter(a) == false
* b.isAfter(a) == true
* </pre>
* <p>
* This method only considers the position of the two dates on the local time-line.
* It does not take into account the chronology, or calendar system.
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
* but is the same approach as {@link #DATE_COMPARATOR}.
*
* @param clock the clock to use, if null, the system clock and default time-zone is used.
* @return the current date, not null
* @param other the other date to compare to, not null
* @return true if this date is after the specified date
*/
static now(clock = Clock.systemDefaultZone()) {
var now = clock.instant();
var offset = clock.offset(now);
var epochSec = now.epochSecond() + offset.totalSeconds();
var epochDay = MathUtil.floorDiv(epochSec, LocalTime.SECONDS_PER_DAY);
return LocalDate.ofEpochDay(epochDay);
isAfter(other) {
return this.compareTo(other) > 0;
// return super.isAfter(other) if not instanceof LocalDate
}
/**
* Obtains an instance of {@code LocalDate} from a text string using a specific formatter.
* Checks if this date is before the specified date.
* <p>
* This checks to see if this date represents a point on the
* local time-line before the other date.
* <pre>
* LocalDate a = LocalDate.of(2012, 6, 30);
* LocalDate b = LocalDate.of(2012, 7, 1);
* a.isBefore(b) == true
* a.isBefore(a) == false
* b.isBefore(a) == false
* </pre>
* <p>
* This method only considers the position of the two dates on the local time-line.
* It does not take into account the chronology, or calendar system.
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)},
* but is the same approach as {@link #DATE_COMPARATOR}.
*
* The text is parsed using the formatter, returning a date.
* @param other the other date to compare to, not null
* @return true if this date is before the specified date
*/
isBefore(other) {
return this.compareTo(other) < 0;
// return super.isBefore(other) if not instanceof LocalDate
}
/**
* Checks if this date is equal to the specified date.
* <p>
* This checks to see if this date represents the same point on the
* local time-line as the other date.
* <pre>
* LocalDate a = LocalDate.of(2012, 6, 30);
* LocalDate b = LocalDate.of(2012, 7, 1);
* a.isEqual(b) == false
* a.isEqual(a) == true
* b.isEqual(a) == false
* </pre>
* <p>
* This method only considers the position of the two dates on the local time-line.
* It does not take into account the chronology, or calendar system.
* This is different from the comparison in {@link #compareTo(ChronoLocalDate)}
* but is the same approach as {@link #DATE_COMPARATOR}.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, default is DateTimeFormatter.ISO_LOCAL_DATE
* @return the parsed local date, not null
* @throws DateTimeParseException if the text cannot be parsed
* @param other the other date to compare to, not null
* @return true if this date is equal to the specified date
*/
static parse(text, formatter = DateTimeFormatter.ISO_LOCAL_DATE()){
assert(formatter != null, 'formatter', NullPointerException);
return formatter.parse(text, LocalDate.FROM);
isEqual(other) {
return this.compareTo(other) === 0;
// return super.isEqual(other) if not instanceof LocalDate
}

@@ -289,3 +1397,3 @@

if (otherDate instanceof LocalDate) {
return this._compareTo(otherDate) === 0;
return this._compareTo0(otherDate) === 0;
}

@@ -295,11 +1403,12 @@ return false;

_compareTo(otherDate){
var cmp = this.year() - otherDate.year();
if (cmp === 0) {
cmp = (this.monthValue() - otherDate.monthValue());
if (cmp === 0) {
cmp = (this.dayOfMonth() - otherDate.dayOfMonth());
}
}
return cmp;
/**
* A hash code for this date.
*
* @return a suitable hash code
*/
hashCode() {
var yearValue = this._year;
var monthValue = this._month;
var dayValue = this._day;
return (yearValue & 0xFFFFF800) ^ ((yearValue << 11) + (monthValue << 6) + (dayValue));
}

@@ -350,244 +1459,19 @@

}
}
export function _init() {
/**
* Obtains an instance of LocalDate from the epoch day count.
*
* This returns a LocalDate with the specified epoch-day.
* The {@link ChronoField#EPOCH_DAY EPOCH_DAY} is a simple incrementing count
* of days where day 0 is 1970-01-01. Negative numbers represent earlier days.
*
* @param {number} epochDay - the Epoch Day to convert, based on the epoch 1970-01-01
* @return {LocalDate} the local date, not null
* @throws AssertionError if the epoch days exceeds the supported date range
* The minimum supported {@code LocalDate}
* This could be used by an application as a "far past" date.
*/
static ofEpochDay(epochDay) {
var adjust, adjustCycles, dom, doyEst, marchDoy0, marchMonth0, month, year, yearEst, zeroDay;
zeroDay = epochDay + DAYS_0000_TO_1970;
zeroDay -= 60;
adjust = 0;
if (zeroDay < 0) {
adjustCycles = MathUtil.intDiv(zeroDay + 1, DAYS_PER_CYCLE) - 1;
adjust = adjustCycles * 400;
zeroDay += -adjustCycles * DAYS_PER_CYCLE;
}
yearEst = MathUtil.intDiv(400 * zeroDay + 591, DAYS_PER_CYCLE);
doyEst = zeroDay - (365 * yearEst + MathUtil.intDiv(yearEst, 4) - MathUtil.intDiv(yearEst, 100) + MathUtil.intDiv(yearEst, 400));
if (doyEst < 0) {
yearEst--;
doyEst = zeroDay - (365 * yearEst + MathUtil.intDiv(yearEst, 4) - MathUtil.intDiv(yearEst, 100) + MathUtil.intDiv(yearEst, 400));
}
yearEst += adjust;
marchDoy0 = doyEst;
marchMonth0 = MathUtil.intDiv(marchDoy0 * 5 + 2, 153);
month = (marchMonth0 + 2) % 12 + 1;
dom = marchDoy0 - MathUtil.intDiv(marchMonth0 * 306 + 5, 10) + 1;
yearEst += MathUtil.intDiv(marchMonth0, 10);
year = yearEst;
return new LocalDate(year, month, dom);
}
LocalDate.MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
/**
* Obtains an instance of {@code LocalDate} from a year and day-of-year.
* <p>
* This returns a {@code LocalDate} with the specified year and day-of-year.
* The day-of-year must be valid for the year, otherwise an exception will be thrown.
*
* @param {number} year the year to represent, from MIN_YEAR to MAX_YEAR
* @param {number} dayOfYear the day-of-year to represent, from 1 to 366
* @return LocalDate the local date, not null
* @throws DateTimeException if the value of any field is out of range,
* or if the day-of-year is invalid for the year
* The maximum supported {@code LocalDate}
* This could be used by an application as a "far future" date.
*/
static ofYearDay(year, dayOfYear) {
ChronoField.YEAR.checkValidValue(year);
//TODO: ChronoField.DAY_OF_YEAR.checkValidValue(dayOfYear);
var leap = IsoChronology.isLeapYear(year);
if (dayOfYear === 366 && leap === false) {
assert(false, 'Invalid date \'DayOfYear 366\' as \'' + year + '\' is not a leap year', DateTimeException);
}
var moy = Month.of(Math.floor((dayOfYear - 1) / 31 + 1));
var monthEnd = moy.firstDayOfYear(leap) + moy.length(leap) - 1;
if (dayOfYear > monthEnd) {
moy = moy.plus(1);
}
var dom = dayOfYear - moy.firstDayOfYear(leap) + 1;
return new LocalDate(year, moy.value(), dom);
}
LocalDate.MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
/**
* Checks if the specified field is supported.
* <p>
* This checks if this date can be queried for the specified field.
* If false, then calling the {@link #range(TemporalField) range} and
* {@link #get(TemporalField) get} methods will throw an exception.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this date-time.
* The supported fields are:
* <ul>
* <li>{@code DAY_OF_WEEK}
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_MONTH}
* <li>{@code ALIGNED_DAY_OF_WEEK_IN_YEAR}
* <li>{@code DAY_OF_MONTH}
* <li>{@code DAY_OF_YEAR}
* <li>{@code EPOCH_DAY}
* <li>{@code ALIGNED_WEEK_OF_MONTH}
* <li>{@code ALIGNED_WEEK_OF_YEAR}
* <li>{@code MONTH_OF_YEAR}
* <li>{@code EPOCH_MONTH}
* <li>{@code YEAR_OF_ERA}
* <li>{@code YEAR}
* <li>{@code ERA}
* </ul>
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @param field the field to check, null returns false
* @return true if the field is supported on this date, false if not
*/
isSupported(field) {
return super.isSupported(field);
}
/**
* Obtains an instance of {@code LocalDate} from a temporal object.
* <p>
* A {@code TemporalAccessor} represents some form of date and time information.
* This factory converts the arbitrary temporal object to an instance of {@code LocalDate}.
* <p>
* The conversion uses the {@link TemporalQueries#localDate()} query, which relies
* on extracting the {@link ChronoField#EPOCH_DAY EPOCH_DAY} field.
* <p>
* This method matches the signature of the functional interface {@link TemporalQuery}
* allowing it to be used as a query via method reference, {@code LocalDate::from}.
*
* @param temporal the temporal object to convert, not null
* @return the local date, not null
* @throws DateTimeException if unable to convert to a {@code LocalDate}
*/
static from(temporal) {
assert(temporal != null, '', NullPointerException);
var date = temporal.query(TemporalQueries.localDate());
if (date == null) {
throw new DateTimeException(
`Unable to obtain LocalDate from TemporalAccessor: ${temporal}, type ${temporal.constructor != null ? temporal.constructor.name : ''}`);
}
return date;
}
/**
* Queries this date using the specified query.
*
* This queries this date using the specified query strategy object.
* The {@code TemporalQuery} object defines the logic to be used to
* obtain the result. Read the documentation of the query to understand
* what the result of this method will be.
*
* The result of this method is obtained by invoking the
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
* specified query passing {@code this} as the argument.
*
* @param query the query to invoke, not null
* @return the query result, null may be returned (defined by the query)
* @throws DateTimeException if unable to query (defined by the query)
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
query(query) {
assert(query != null, '', NullPointerException);
if (query === TemporalQueries.localDate()) {
return this;
}
return super.query(query);
}
/**
* Returns a copy of this {@code LocalDate} with the day-of-month altered.
* <p>
* If the resulting date is invalid, an exception is thrown.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param {number} dayOfMonth the day-of-month to set in the result, from 1 to 28-31
* @return {LocalDate} based on this date with the requested day, not null
* @throws DateTimeException if the day-of-month value is invalid,
* or if the day-of-month is invalid for the month-year
*/
withDayOfMonth(dayOfMonth) {
if (this._day === dayOfMonth) {
return this;
}
return LocalDate.of(this._year, this._month, dayOfMonth);
}
/**
* Returns a copy of this {@code LocalDate} with the month-of-year altered.
* <p>
* If the day-of-month is invalid for the year, it will be changed to the last valid day of the month.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param {number} month the month-of-year to set in the result, from 1 (January) to 12 (December)
* @return {@code LocalDate} based on this date with the requested month, not null
* @throws DateTimeException if the month-of-year value is invalid
*/
withMonth(month) {
if (this._month === month) {
return this;
}
return LocalDate.of(this._year, month, this._day);
}
/**
* @private
*/
static validate(year, month, dayOfMonth) {
var dom;
ChronoField.YEAR.checkValidValue(year);
ChronoField.MONTH_OF_YEAR.checkValidValue(month);
ChronoField.DAY_OF_MONTH.checkValidValue(dayOfMonth);
if (dayOfMonth > 28) {
dom = 31;
switch (month) {
case 2:
dom = IsoChronology.isLeapYear(year) ? 29 : 28;
break;
case 4:
case 6:
case 9:
case 11:
dom = 30;
}
if (dayOfMonth > dom) {
if (dayOfMonth === 29) {
assert(false, 'Invalid date \'February 29\' as \'' + year + '\' is not a leap year', DateTimeException);
} else {
assert(false, 'Invalid date \'' + year + '\' \'' + month + '\' \'' + dayOfMonth + '\'', DateTimeException);
}
}
}
}
LocalDate.FROM = createTemporalQuery('LocalDate.FROM', (temporal) => {
return LocalDate.from(temporal);
});
}
/**
* The minimum supported {@code LocalDate}
* This could be used by an application as a "far past" date.
*/
LocalDate.MIN = LocalDate.of(Year.MIN_VALUE, 1, 1);
/**
* The maximum supported {@code LocalDate}
* This could be used by an application as a "far future" date.
*/
LocalDate.MAX = LocalDate.of(Year.MAX_VALUE, 12, 31);
LocalDate.FROM = createTemporalQuery('LocalDate.FROM', (temporal) => {
return LocalDate.from(temporal);
});

@@ -6,14 +6,1319 @@ /**

*/
export class LocalTime {
import {MathUtil} from './MathUtil';
import {assert, requireNonNull, requireInstance} from './assert';
import {DateTimeException, UnsupportedTemporalTypeException, IllegalArgumentException} from './errors';
import {Clock} from './Clock';
import {DateTimeFormatter} from './format/DateTimeFormatter';
import {ChronoField} from './temporal/ChronoField';
import {ChronoUnit} from './temporal/ChronoUnit';
import {TemporalAccessor} from './temporal/TemporalAccessor';
import {TemporalQueries, createTemporalQuery} from './temporal/TemporalQueries';
/**
* A time without time-zone in the ISO-8601 calendar system,
* such as {@code 10:15:30}.
* <p>
* {@code LocalTime} is an immutable date-time object that represents a time,
* often viewed as hour-minute-second.
* Time is represented to nanosecond precision.
* For example, the value '13:45.30.123456789' can be stored in a {@code LocalTime}.
* <p>
* It does not store or represent a date or time-zone.
* Instead, it is a description of the local time as seen on a wall clock.
* It cannot represent an instant on the time-line without additional information
* such as an offset or time-zone.
* <p>
* The ISO-8601 calendar system is the modern civil calendar system used today
* in most of the world. This API assumes that all calendar systems use the same
* representation, this class, for time-of-day.
*
* <h3>Specification for implementors</h3>
* This class is immutable and thread-safe.
*/
export class LocalTime extends TemporalAccessor /** implements Temporal, TemporalAdjuster */ {
/**
* Obtains the current time from the specified clock.
* <p>
* This will query the specified clock to obtain the current time.
* Using this method allows the use of an alternate clock for testing.
* The alternate clock may be introduced using {@link Clock dependency injection}.
*
* @param clock the clock to use, not null
* @return the current time, not null
*/
static now(clock = Clock.systemDefaultZone()) {
requireNonNull(clock, 'clock');
// inline OffsetTime factory to avoid creating object and InstantProvider checks
var now = clock.instant(); // called once
var offset = clock.offset(now);
var secsOfDay = MathUtil.intMod(now.epochSecond(), LocalTime.SECONDS_PER_DAY);
secsOfDay = MathUtil.intMod((secsOfDay + offset.totalSeconds()), LocalTime.SECONDS_PER_DAY);
if (secsOfDay < 0) {
secsOfDay += LocalTime.SECONDS_PER_DAY;
}
return LocalTime.ofSecondOfDay(secsOfDay, now.nano());
}
/**
* Obtains an instance of {@code LocalTime} from an hour, minute, second and nanosecond.
* <p>
* This factory may return a cached value, but applications must not rely on this.
*
* @param hour the hour-of-day to represent, from 0 to 23
* @param minute the minute-of-hour to represent, from 0 to 59
* @param second the second-of-minute to represent, from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, from 0 to 999,999,999
* @return the local time, not null
* @throws DateTimeException if the value of any field is out of range
*/
static of(hour, minute, second, nanoOfSecond) {
return new LocalTime(hour, minute, second, nanoOfSecond);
}
/**
* Obtains an instance of {@code LocalTime} from a second-of-day value, with
* associated nanos of second.
* <p>
* This factory may return a cached value, but applications must not rely on this.
*
* @param secondOfDay the second-of-day, from {@code 0} to {@code 24 * 60 * 60 - 1}
* @param nanoOfSecond the nano-of-second, from 0 to 999,999,999
* @return the local time, not null
* @throws DateTimeException if the either input value is invalid
*/
static ofSecondOfDay(secondOfDay=0, nanoOfSecond=0) {
ChronoField.SECOND_OF_DAY.checkValidValue(secondOfDay);
ChronoField.NANO_OF_SECOND.checkValidValue(nanoOfSecond);
var hours = MathUtil.intDiv(secondOfDay, LocalTime.SECONDS_PER_HOUR);
secondOfDay -= hours * LocalTime.SECONDS_PER_HOUR;
var minutes = MathUtil.intDiv(secondOfDay, LocalTime.SECONDS_PER_MINUTE);
secondOfDay -= minutes * LocalTime.SECONDS_PER_MINUTE;
return new LocalTime(hours, minutes, secondOfDay, nanoOfSecond);
}
/**
* Obtains an instance of {@code LocalTime} from a nanos-of-day value.
* <p>
* This factory may return a cached value, but applications must not rely on this.
*
* @param nanoOfDay the nano of day, from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}
* @return the local time, not null
* @throws DateTimeException if the nanos of day value is invalid
*/
static ofNanoOfDay(nanoOfDay=0) {
ChronoField.NANO_OF_DAY.checkValidValue(nanoOfDay);
var hours = MathUtil.intDiv(nanoOfDay, LocalTime.NANOS_PER_HOUR);
nanoOfDay -= hours * LocalTime.NANOS_PER_HOUR;
var minutes = MathUtil.intDiv(nanoOfDay, LocalTime.NANOS_PER_MINUTE);
nanoOfDay -= minutes * LocalTime.NANOS_PER_MINUTE;
var seconds = MathUtil.intDiv(nanoOfDay, LocalTime.NANOS_PER_SECOND);
nanoOfDay -= seconds * LocalTime.NANOS_PER_SECOND;
return new LocalTime(hours, minutes, seconds, nanoOfDay);
}
//-----------------------------------------------------------------------
/**
* Obtains an instance of {@code LocalTime} from a temporal object.
* <p>
* A {@code TemporalAccessor} represents some form of date and time information.
* This factory converts the arbitrary temporal object to an instance of {@code LocalTime}.
* <p>
* The conversion uses the {@link TemporalQueries#localTime()} query, which relies
* on extracting the {@link ChronoField#NANO_OF_DAY NANO_OF_DAY} field.
* <p>
* This method matches the signature of the functional interface {@link TemporalQuery}
* allowing it to be used in queries via method reference, {@code LocalTime::from}.
*
* @param temporal the temporal object to convert, not null
* @return the local time, not null
* @throws DateTimeException if unable to convert to a {@code LocalTime}
*/
static from(temporal) {
requireNonNull(temporal, 'temporal');
var time = temporal.query(TemporalQueries.localTime());
if (time == null) {
throw new DateTimeException('Unable to obtain LocalTime from TemporalAccessor: ' +
temporal + ', type ' + temporal);
}
return time;
}
/**
* Obtains an instance of {@code LocalTime} from a text string using a specific formatter.
* <p>
* The text is parsed using the formatter, returning a time.
*
* @param text the text to parse, not null
* @param formatter the formatter to use, not null
* @return the parsed local time, not null
* @throws DateTimeParseException if the text cannot be parsed
*/
static parse(text, formatter=DateTimeFormatter.ISO_LOCAL_TIME) {
requireNonNull(formatter, 'formatter');
return formatter.parse(text, LocalTime.FROM);
}
/**
* Constructor, previously validated.
*
* @param hour the hour-of-day to represent, validated from 0 to 23
* @param minute the minute-of-hour to represent, validated from 0 to 59
* @param second the second-of-minute to represent, validated from 0 to 59
* @param nanoOfSecond the nano-of-second to represent, validated from 0 to 999,999,999
*/
constructor(hour=0, minute=0, second=0, nanoOfSecond=0) {
super();
LocalTime._validate(hour, minute, second, nanoOfSecond);
if ((minute | second | nanoOfSecond) === 0) {
return LocalTime.HOURS[hour];
}
this._hour = hour;
this._minute = minute;
this._second = second;
this._nano = nanoOfSecond;
}
static _validate(hour, minute, second, nanoOfSecond){
ChronoField.HOUR_OF_DAY.checkValidValue(hour);
ChronoField.MINUTE_OF_HOUR.checkValidValue(minute);
ChronoField.SECOND_OF_MINUTE.checkValidValue(second);
ChronoField.NANO_OF_SECOND.checkValidValue(nanoOfSecond);
}
//-----------------------------------------------------------------------
/**
* Checks if the specified field is supported.
* <p>
* This checks if this time can be queried for the specified field.
* If false, then calling the {@link #range(TemporalField) range} and
* {@link #get(TemporalField) get} methods will throw an exception.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The supported fields are:
* <ul>
* <li>{@code NANO_OF_SECOND}
* <li>{@code NANO_OF_DAY}
* <li>{@code MICRO_OF_SECOND}
* <li>{@code MICRO_OF_DAY}
* <li>{@code MILLI_OF_SECOND}
* <li>{@code MILLI_OF_DAY}
* <li>{@code SECOND_OF_MINUTE}
* <li>{@code SECOND_OF_DAY}
* <li>{@code MINUTE_OF_HOUR}
* <li>{@code MINUTE_OF_DAY}
* <li>{@code HOUR_OF_AMPM}
* <li>{@code CLOCK_HOUR_OF_AMPM}
* <li>{@code HOUR_OF_DAY}
* <li>{@code CLOCK_HOUR_OF_DAY}
* <li>{@code AMPM_OF_DAY}
* </ul>
* All other {@code ChronoField} instances will return false.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.isSupportedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the field is supported is determined by the field.
*
* @param fieldOrUnit the field to check, null returns false
* @return true if the field is supported on this time, false if not
*/
isSupported(fieldOrUnit) {
if (fieldOrUnit instanceof ChronoField) {
return fieldOrUnit.isTimeBased();
} else if (fieldOrUnit instanceof ChronoUnit) {
return fieldOrUnit.isTimeBased();
}
return fieldOrUnit != null && fieldOrUnit.isSupportedBy(this);
}
/**
* Gets the range of valid values for the specified field.
* <p>
* The range object expresses the minimum and maximum valid values for a field.
* This time is used to enhance the accuracy of the returned range.
* If it is not possible to return the range, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return
* appropriate range instances.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
* passing {@code this} as the argument.
* Whether the range can be obtained is determined by the field.
*
* @param field the field to query the range for, not null
* @return the range of valid values for the field, not null
* @throws DateTimeException if the range for the field cannot be obtained
*/
range(field) {
requireNonNull(field);
return super.range(field);
}
/**
* Gets the value of the specified field from this time as an {@code int}.
* <p>
* This queries this time for the value for the specified field.
* The returned value will always be within the valid range of values for the field.
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this time, except {@code NANO_OF_DAY} and {@code MICRO_OF_DAY}
* which are too large to fit in an {@code int} and throw a {@code DateTimeException}.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
* @throws DateTimeException if a value for the field cannot be obtained
* @throws ArithmeticException if numeric overflow occurs
*/
get(field) {
return this.getLong(field);
}
/**
* Gets the value of the specified field from this time as a {@code long}.
* <p>
* This queries this time for the value for the specified field.
* If it is not possible to return the value, because the field is not supported
* or for some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the query is implemented here.
* The {@link #isSupported(TemporalField) supported fields} will return valid
* values based on this time.
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.getFrom(TemporalAccessor)}
* passing {@code this} as the argument. Whether the value can be obtained,
* and what the value represents, is determined by the field.
*
* @param field the field to get, not null
* @return the value for the field
* @throws DateTimeException if a value for the field cannot be obtained
* @throws ArithmeticException if numeric overflow occurs
*/
getLong(field) {
requireNonNull(field, 'field');
if (field instanceof ChronoField) {
return this._get0(field);
}
return field.getFrom(this);
}
_get0(field) {
switch (field) {
case ChronoField.NANO_OF_SECOND: return this._nano;
case ChronoField.NANO_OF_DAY: return this.toNanoOfDay();
case ChronoField.MICRO_OF_SECOND: return MathUtil.intDiv(this._nano, 1000);
case ChronoField.MICRO_OF_DAY: return MathUtil.intDiv(this.toNanoOfDay(), 1000);
case ChronoField.MILLI_OF_SECOND: return MathUtil.intDiv(this._nano, 1000000);
case ChronoField.MILLI_OF_DAY: return MathUtil.intDiv(this.toNanoOfDay(), 1000000);
case ChronoField.SECOND_OF_MINUTE: return this._second;
case ChronoField.SECOND_OF_DAY: return this.toSecondOfDay();
case ChronoField.MINUTE_OF_HOUR: return this._minute;
case ChronoField.MINUTE_OF_DAY: return this._hour * 60 + this._minute;
case ChronoField.HOUR_OF_AMPM: return this._hour % 12;
case ChronoField.CLOCK_HOUR_OF_AMPM: var ham = MathUtil.intMod(this._hour, 12); return (ham % 12 === 0 ? 12 : ham);
case ChronoField.HOUR_OF_DAY: return this._hour;
case ChronoField.CLOCK_HOUR_OF_DAY: return (this._hour === 0 ? 24 : this._hour);
case ChronoField.AMPM_OF_DAY: return this._hour / 12;
}
throw new UnsupportedTemporalTypeException('Unsupported field: ' + field);
}
//-----------------------------------------------------------------------
/**
* Gets the hour-of-day field.
*
* @return the hour-of-day, from 0 to 23
*/
hour() {
return this._hour;
}
/**
* Gets the minute-of-hour field.
*
* @return the minute-of-hour, from 0 to 59
*/
minute() {
return this._minute;
}
/**
* Gets the second-of-minute field.
*
* @return the second-of-minute, from 0 to 59
*/
second() {
return this._second;
}
/**
* Gets the nano-of-second field.
*
* @return the nano-of-second, from 0 to 999,999,999
*/
nano() {
return this._nano;
}
/**
* functional overloading for with
*/
with(){
if(arguments.length < 2){
return this._with1.apply(this, arguments);
} else {
return this._with2.apply(this, arguments);
}
}
/**
* Returns an adjusted copy of this time.
* <p>
* This returns a new {@code LocalTime}, based on this one, with the time adjusted.
* The adjustment takes place using the specified adjuster strategy object.
* Read the documentation of the adjuster to understand what adjustment will be made.
* <p>
* A simple adjuster might simply set the one of the fields, such as the hour field.
* A more complex adjuster might set the time to the last hour of the day.
* <p>
* The result of this method is obtained by invoking the
* {@link TemporalAdjuster#adjustInto(Temporal)} method on the
* specified adjuster passing {@code this} as the argument.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param adjuster the adjuster to use, not null
* @return a {@code LocalTime} based on {@code this} with the adjustment made, not null
* @throws DateTimeException if the adjustment cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_with1(adjuster) {
requireNonNull(adjuster, 'adjuster');
// optimizations
if (adjuster instanceof LocalTime) {
return adjuster;
}
assert(typeof adjuster.adjustInto === 'function', 'adjuster', IllegalArgumentException);
return adjuster.adjustInto(this);
}
/**
* Returns a copy of this time with the specified field set to a new value.
* <p>
* This returns a new {@code LocalTime}, based on this one, with the value
* for the specified field changed.
* This can be used to change any supported field, such as the hour, minute or second.
* If it is not possible to set the value, because the field is not supported or for
* some other reason, an exception is thrown.
* <p>
* If the field is a {@link ChronoField} then the adjustment is implemented here.
* The supported fields behave as follows:
* <ul>
* <li>{@code NANO_OF_SECOND} -
* Returns a {@code LocalTime} with the specified nano-of-second.
* The hour, minute and second will be unchanged.
* <li>{@code NANO_OF_DAY} -
* Returns a {@code LocalTime} with the specified nano-of-day.
* This completely replaces the time and is equivalent to {@link #ofNanoOfDay(long)}.
* <li>{@code MICRO_OF_SECOND} -
* Returns a {@code LocalTime} with the nano-of-second replaced by the specified
* micro-of-second multiplied by 1,000.
* The hour, minute and second will be unchanged.
* <li>{@code MICRO_OF_DAY} -
* Returns a {@code LocalTime} with the specified micro-of-day.
* This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
* with the micro-of-day multiplied by 1,000.
* <li>{@code MILLI_OF_SECOND} -
* Returns a {@code LocalTime} with the nano-of-second replaced by the specified
* milli-of-second multiplied by 1,000,000.
* The hour, minute and second will be unchanged.
* <li>{@code MILLI_OF_DAY} -
* Returns a {@code LocalTime} with the specified milli-of-day.
* This completely replaces the time and is equivalent to using {@link #ofNanoOfDay(long)}
* with the milli-of-day multiplied by 1,000,000.
* <li>{@code SECOND_OF_MINUTE} -
* Returns a {@code LocalTime} with the specified second-of-minute.
* The hour, minute and nano-of-second will be unchanged.
* <li>{@code SECOND_OF_DAY} -
* Returns a {@code LocalTime} with the specified second-of-day.
* The nano-of-second will be unchanged.
* <li>{@code MINUTE_OF_HOUR} -
* Returns a {@code LocalTime} with the specified minute-of-hour.
* The hour, second-of-minute and nano-of-second will be unchanged.
* <li>{@code MINUTE_OF_DAY} -
* Returns a {@code LocalTime} with the specified minute-of-day.
* The second-of-minute and nano-of-second will be unchanged.
* <li>{@code HOUR_OF_AMPM} -
* Returns a {@code LocalTime} with the specified hour-of-am-pm.
* The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
* <li>{@code CLOCK_HOUR_OF_AMPM} -
* Returns a {@code LocalTime} with the specified clock-hour-of-am-pm.
* The AM/PM, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
* <li>{@code HOUR_OF_DAY} -
* Returns a {@code LocalTime} with the specified hour-of-day.
* The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
* <li>{@code CLOCK_HOUR_OF_DAY} -
* Returns a {@code LocalTime} with the specified clock-hour-of-day.
* The minute-of-hour, second-of-minute and nano-of-second will be unchanged.
* <li>{@code AMPM_OF_DAY} -
* Returns a {@code LocalTime} with the specified AM/PM.
* The hour-of-am-pm, minute-of-hour, second-of-minute and nano-of-second will be unchanged.
* </ul>
* <p>
* In all cases, if the new value is outside the valid range of values for the field
* then a {@code DateTimeException} will be thrown.
* <p>
* All other {@code ChronoField} instances will throw a {@code DateTimeException}.
* <p>
* If the field is not a {@code ChronoField}, then the result of this method
* is obtained by invoking {@code TemporalField.adjustInto(Temporal, long)}
* passing {@code this} as the argument. In this case, the field determines
* whether and how to adjust the instant.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param field the field to set in the result, not null
* @param newValue the new value of the field in the result
* @return a {@code LocalTime} based on {@code this} with the specified field set, not null
* @throws DateTimeException if the field cannot be set
* @throws ArithmeticException if numeric overflow occurs
*/
_with2(field, newValue) {
requireNonNull(field, 'field');
if (field instanceof ChronoField) {
field.checkValidValue(newValue);
switch (field) {
case ChronoField.NANO_OF_SECOND: return this.withNano(newValue);
case ChronoField.NANO_OF_DAY: return LocalTime.ofNanoOfDay(newValue);
case ChronoField.MICRO_OF_SECOND: return this.withNano(newValue * 1000);
case ChronoField.MICRO_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000);
case ChronoField.MILLI_OF_SECOND: return this.withNano( newValue * 1000000);
case ChronoField.MILLI_OF_DAY: return LocalTime.ofNanoOfDay(newValue * 1000000);
case ChronoField.SECOND_OF_MINUTE: return this.withSecond(newValue);
case ChronoField.SECOND_OF_DAY: return this.plusSeconds(newValue - this.toSecondOfDay());
case ChronoField.MINUTE_OF_HOUR: return this.withMinute(newValue);
case ChronoField.MINUTE_OF_DAY: return this.plusMinutes(newValue - (this._hour * 60 + this._minute));
case ChronoField.HOUR_OF_AMPM: return this.plusHours(newValue - MathUtil.intMod(this._hour, 12));
case ChronoField.CLOCK_HOUR_OF_AMPM: return this.plusHours((newValue === 12 ? 0 : newValue) - MathUtil.intMod(this._hour, 12));
case ChronoField.HOUR_OF_DAY: return this.withHour(newValue);
case ChronoField.CLOCK_HOUR_OF_DAY: return this.withHour((newValue === 24 ? 0 : newValue));
case ChronoField.AMPM_OF_DAY: return this.plusHours((newValue - MathUtil.intDiv(this._hour, 12)) * 12);
}
throw new UnsupportedTemporalTypeException('Unsupported field: ' + field);
}
return field.adjustInto(this, newValue);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalTime} with the hour-of-day value altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param hour the hour-of-day to set in the result, from 0 to 23
* @return a {@code LocalTime} based on this time with the requested hour, not null
* @throws DateTimeException if the hour value is invalid
*/
withHour(hour=0) {
if (this._hour === hour) {
return this;
}
return new LocalTime(hour, this._minute, this._second, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the minute-of-hour value altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param minute the minute-of-hour to set in the result, from 0 to 59
* @return a {@code LocalTime} based on this time with the requested minute, not null
* @throws DateTimeException if the minute value is invalid
*/
withMinute(minute=0) {
if (this._minute === minute) {
return this;
}
return new LocalTime(this._hour, minute, this._second, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the second-of-minute value altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param second the second-of-minute to set in the result, from 0 to 59
* @return a {@code LocalTime} based on this time with the requested second, not null
* @throws DateTimeException if the second value is invalid
*/
withSecond(second=0) {
if (this._second === second) {
return this;
}
return new LocalTime(this._hour, this._minute, second, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the nano-of-second value altered.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param nanoOfSecond the nano-of-second to set in the result, from 0 to 999,999,999
* @return a {@code LocalTime} based on this time with the requested nanosecond, not null
* @throws DateTimeException if the nanos value is invalid
*/
withNano(nanoOfSecond) {
if (this._nano === nanoOfSecond) {
return this;
}
return new LocalTime(this._hour, this._minute, this._second, nanoOfSecond);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalTime} with the time truncated.
* <p>
* Truncating the time returns a copy of the original time with fields
* smaller than the specified unit set to zero.
* For example, truncating with the {@link ChronoUnit#MINUTES minutes} unit
* will set the second-of-minute and nano-of-second field to zero.
* <p>
* The unit must have a {@linkplain TemporalUnit#getDuration() duration}
* that divides into the length of a standard day without remainder.
* This includes all supplied time units on {@link ChronoUnit} and
* {@link ChronoUnit#DAYS DAYS}. Other units throw an exception.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param unit the unit to truncate to, not null
* @return a {@code LocalTime} based on this time with the time truncated, not null
* @throws DateTimeException if unable to truncate
*/
truncatedTo(unit) {
requireNonNull(unit, 'unit');
if (unit === ChronoUnit.NANOS) {
return this;
}
var unitDur = unit.duration();
if (unitDur.seconds() > LocalTime.SECONDS_PER_DAY) {
throw new DateTimeException('Unit is too large to be used for truncation');
}
var dur = unitDur.toNanos();
if (MathUtil.intMod(LocalTime.NANOS_PER_DAY, dur) !== 0) {
throw new DateTimeException('Unit must divide into a standard day without remainder');
}
var nod = this.toNanoOfDay();
return LocalTime.ofNanoOfDay(MathUtil.intDiv(nod, dur) * dur);
}
//-----------------------------------------------------------------------
plus(){
if(arguments.length < 2){
return this._plus1.apply(this, arguments);
} else {
return this._plus2.apply(this, arguments);
}
}
/**
* Returns a copy of this date with the specified period added.
* <p>
* This method returns a new time based on this time with the specified period added.
* The amount is typically {@link Period} but may be any other type implementing
* the {@link TemporalAmount} interface.
* The calculation is delegated to the specified adjuster, which typically calls
* back to {@link #plus(long, TemporalUnit)}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to add, not null
* @return a {@code LocalTime} based on this time with the addition made, not null
* @throws DateTimeException if the addition cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_plus1(amount) {
requireNonNull(amount, 'amount');
return amount.addTo(this);
}
/**
* Returns a copy of this time with the specified period added.
* <p>
* This method returns a new time based on this time with the specified period added.
* This can be used to add any period that is defined by a unit, for example to add hours, minutes or seconds.
* The unit is responsible for the details of the calculation, including the resolution
* of any edge cases in the calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToAdd the amount of the unit to add to the result, may be negative
* @param unit the unit of the period to add, not null
* @return a {@code LocalTime} based on this time with the specified period added, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
_plus2(amountToAdd, unit) {
requireNonNull(unit, 'unit');
if (unit instanceof ChronoUnit) {
switch (unit) {
case ChronoUnit.NANOS: return this.plusNanos(amountToAdd);
case ChronoUnit.MICROS: return this.plusNanos(MathUtil.intMod(amountToAdd, LocalTime.MICROS_PER_DAY) * 1000);
case ChronoUnit.MILLIS: return this.plusNanos(MathUtil.intMod(amountToAdd, LocalTime.MILLIS_PER_DAY) * 1000000);
case ChronoUnit.SECONDS: return this.plusSeconds(amountToAdd);
case ChronoUnit.MINUTES: return this.plusMinutes(amountToAdd);
case ChronoUnit.HOURS: return this.plusHours(amountToAdd);
case ChronoUnit.HALF_DAYS: return this.plusHours(MathUtil.intMod(amountToAdd, 2) * 12);
}
throw new UnsupportedTemporalTypeException('Unsupported unit: ' + unit);
}
return unit.addTo(this, amountToAdd);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalTime} with the specified period in hours added.
* <p>
* This adds the specified number of hours to this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param hoursToAdd the hours to add, may be negative
* @return a {@code LocalTime} based on this time with the hours added, not null
*/
plusHours(hoursToAdd) {
if (hoursToAdd === 0) {
return this;
}
var newHour = MathUtil.intMod(MathUtil.intMod(hoursToAdd, LocalTime.HOURS_PER_DAY) + this._hour + LocalTime.HOURS_PER_DAY, LocalTime.HOURS_PER_DAY);
return new LocalTime(newHour, this._minute, this._second, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in minutes added.
* <p>
* This adds the specified number of minutes to this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param minutesToAdd the minutes to add, may be negative
* @return a {@code LocalTime} based on this time with the minutes added, not null
*/
plusMinutes(minutesToAdd) {
if (minutesToAdd === 0) {
return this;
}
var mofd = this._hour * LocalTime.MINUTES_PER_HOUR + this._minute;
var newMofd = MathUtil.intMod(MathUtil.intMod(minutesToAdd, LocalTime.MINUTES_PER_DAY) + mofd + LocalTime.MINUTES_PER_DAY, LocalTime.MINUTES_PER_DAY);
if (mofd === newMofd) {
return this;
}
var newHour = MathUtil.intDiv(newMofd, LocalTime.MINUTES_PER_HOUR);
var newMinute = MathUtil.intMod(newMofd, LocalTime.MINUTES_PER_HOUR);
return new LocalTime(newHour, newMinute, this._second, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in seconds added.
* <p>
* This adds the specified number of seconds to this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param secondstoAdd the seconds to add, may be negative
* @return a {@code LocalTime} based on this time with the seconds added, not null
*/
plusSeconds(secondstoAdd) {
if (secondstoAdd === 0) {
return this;
}
var sofd = this._hour * LocalTime.SECONDS_PER_HOUR +
this._minute * LocalTime.SECONDS_PER_MINUTE + this._second;
var newSofd = MathUtil.intMod((MathUtil.intMod(secondstoAdd, LocalTime.SECONDS_PER_DAY) + sofd + LocalTime.SECONDS_PER_DAY), LocalTime.SECONDS_PER_DAY);
if (sofd === newSofd) {
return this;
}
var newHour = MathUtil.intDiv(newSofd, LocalTime.SECONDS_PER_HOUR);
var newMinute = MathUtil.intMod(MathUtil.intDiv(newSofd, LocalTime.SECONDS_PER_MINUTE), LocalTime.MINUTES_PER_HOUR);
var newSecond = MathUtil.intMod(newSofd, LocalTime.SECONDS_PER_MINUTE);
return new LocalTime(newHour, newMinute, newSecond, this._nano);
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in nanoseconds added.
* <p>
* This adds the specified number of nanoseconds to this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param nanosToAdd the nanos to add, may be negative
* @return a {@code LocalTime} based on this time with the nanoseconds added, not null
*/
plusNanos(nanosToAdd) {
if (nanosToAdd === 0) {
return this;
}
var nofd = this.toNanoOfDay();
var newNofd = MathUtil.intMod((MathUtil.intMod(nanosToAdd, LocalTime.NANOS_PER_DAY) + nofd + LocalTime.NANOS_PER_DAY), LocalTime.NANOS_PER_DAY);
if (nofd === newNofd) {
return this;
}
var newHour = MathUtil.intDiv(newNofd, LocalTime.NANOS_PER_HOUR);
var newMinute = MathUtil.intMod(MathUtil.intDiv(newNofd, LocalTime.NANOS_PER_MINUTE), LocalTime.MINUTES_PER_HOUR);
var newSecond = MathUtil.intMod(MathUtil.intDiv(newNofd, LocalTime.NANOS_PER_SECOND), LocalTime.SECONDS_PER_MINUTE);
var newNano = MathUtil.intMod(newNofd, LocalTime.NANOS_PER_SECOND);
return new LocalTime(newHour, newMinute, newSecond, newNano);
}
//-----------------------------------------------------------------------
minus(){
if(arguments.length < 2){
return this._minus1.apply(this, arguments);
} else {
return this._minus2.apply(this, arguments);
}
}
/**
* Returns a copy of this time with the specified period subtracted.
* <p>
* This method returns a new time based on this time with the specified period subtracted.
* The amount is typically {@link Period} but may be any other type implementing
* the {@link TemporalAmount} interface.
* The calculation is delegated to the specified adjuster, which typically calls
* back to {@link #minus(long, TemporalUnit)}.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amount the amount to subtract, not null
* @return a {@code LocalTime} based on this time with the subtraction made, not null
* @throws DateTimeException if the subtraction cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
_minus1(amount) {
requireNonNull(amount, 'amount');
return amount.subtractFrom(this);
}
/**
* Returns a copy of this time with the specified period subtracted.
* <p>
* This method returns a new time based on this time with the specified period subtracted.
* This can be used to subtract any period that is defined by a unit, for example to subtract hours, minutes or seconds.
* The unit is responsible for the details of the calculation, including the resolution
* of any edge cases in the calculation.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
* @param unit the unit of the period to subtract, not null
* @return a {@code LocalTime} based on this time with the specified period subtracted, not null
* @throws DateTimeException if the unit cannot be added to this type
*/
_minus2(amountToSubtract, unit) {
requireNonNull(unit, 'unit');
return this._plus2(-1 * amountToSubtract, unit);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalTime} with the specified period in hours subtracted.
* <p>
* This subtracts the specified number of hours from this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param hoursToSubtract the hours to subtract, may be negative
* @return a {@code LocalTime} based on this time with the hours subtracted, not null
*/
minusHours(hoursToSubtract) {
return this.plusHours(-1 * MathUtil.intMod(hoursToSubtract, LocalTime.HOURS_PER_DAY));
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in minutes subtracted.
* <p>
* This subtracts the specified number of minutes from this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param minutesToSubtract the minutes to subtract, may be negative
* @return a {@code LocalTime} based on this time with the minutes subtracted, not null
*/
minusMinutes(minutesToSubtract) {
return this.plusMinutes(-1 * MathUtil.intMod(minutesToSubtract, LocalTime.MINUTES_PER_DAY));
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in seconds subtracted.
* <p>
* This subtracts the specified number of seconds from this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param secondsToSubtract the seconds to subtract, may be negative
* @return a {@code LocalTime} based on this time with the seconds subtracted, not null
*/
minusSeconds(secondsToSubtract) {
return this.plusSeconds(-1 * MathUtil.intMod(secondsToSubtract, LocalTime.SECONDS_PER_DAY));
}
/**
* Returns a copy of this {@code LocalTime} with the specified period in nanoseconds subtracted.
* <p>
* This subtracts the specified number of nanoseconds from this time, returning a new time.
* The calculation wraps around midnight.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param nanosToSubtract the nanos to subtract, may be negative
* @return a {@code LocalTime} based on this time with the nanoseconds subtracted, not null
*/
minusNanos(nanosToSubtract) {
return this.plusNanos(-1 * MathUtil.intMod(nanosToSubtract, LocalTime.NANOS_PER_DAY));
}
//-----------------------------------------------------------------------
/**
* Queries this time using the specified query.
* <p>
* This queries this time using the specified query strategy object.
* The {@code TemporalQuery} object defines the logic to be used to
* obtain the result. Read the documentation of the query to understand
* what the result of this method will be.
* <p>
* The result of this method is obtained by invoking the
* {@link TemporalQuery#queryFrom(TemporalAccessor)} method on the
* specified query passing {@code this} as the argument.
*
* @param query the query to invoke, not null
* @return the query result, null may be returned (defined by the query)
* @throws DateTimeException if unable to query (defined by the query)
* @throws ArithmeticException if numeric overflow occurs (defined by the query)
*/
query(query) {
requireNonNull(query, 'query');
if (query === TemporalQueries.precision()) {
return ChronoUnit.NANOS;
} else if (query === TemporalQueries.localTime()) {
return this;
}
// inline TemporalAccessor.super.query(query) as an optimization
if (query === TemporalQueries.chronology() || query === TemporalQueries.zoneId() ||
query === TemporalQueries.zone() || query === TemporalQueries.offset() ||
query === TemporalQueries.localDate()) {
return null;
}
return query.queryFrom(this);
}
/**
* Adjusts the specified temporal object to have the same time as this object.
* <p>
* This returns a temporal object of the same observable type as the input
* with the time changed to be the same as this.
* <p>
* The adjustment is equivalent to using {@link Temporal#with(TemporalField, long)}
* passing {@link ChronoField#NANO_OF_DAY} as the field.
* <p>
* In most cases, it is clearer to reverse the calling pattern by using
* {@link Temporal#with(TemporalAdjuster)}:
* <pre>
* // these two lines are equivalent, but the second approach is recommended
* temporal = thisLocalTime.adjustInto(temporal);
* temporal = temporal.with(thisLocalTime);
* </pre>
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param temporal the target object to be adjusted, not null
* @return the adjusted object, not null
* @throws DateTimeException if unable to make the adjustment
* @throws ArithmeticException if numeric overflow occurs
*/
adjustInto(temporal) {
return temporal.with(LocalTime.NANO_OF_DAY, this.toNanoOfDay());
}
/**
* Calculates the period between this time and another time in
* terms of the specified unit.
* <p>
* This calculates the period between two times in terms of a single unit.
* The start and end points are {@code this} and the specified time.
* The result will be negative if the end is before the start.
* The {@code Temporal} passed to this method must be a {@code LocalTime}.
* For example, the period in hours between two times can be calculated
* using {@code startTime.until(endTime, HOURS)}.
* <p>
* The calculation returns a whole number, representing the number of
* complete units between the two times.
* For example, the period in hours between 11:30 and 13:29 will only
* be one hour as it is one minute short of two hours.
* <p>
* This method operates in association with {@link TemporalUnit#between}.
* The result of this method is a {@code long} representing the amount of
* the specified unit. By contrast, the result of {@code between} is an
* object that can be used directly in addition/subtraction:
* <pre>
* long period = start.until(end, HOURS); // this method
* dateTime.plus(HOURS.between(start, end)); // use in plus/minus
* </pre>
* <p>
* The calculation is implemented in this method for {@link ChronoUnit}.
* The units {@code NANOS}, {@code MICROS}, {@code MILLIS}, {@code SECONDS},
* {@code MINUTES}, {@code HOURS} and {@code HALF_DAYS} are supported.
* Other {@code ChronoUnit} values will throw an exception.
* <p>
* If the unit is not a {@code ChronoUnit}, then the result of this method
* is obtained by invoking {@code TemporalUnit.between(Temporal, Temporal)}
* passing {@code this} as the first argument and the input temporal as
* the second argument.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param endExclusive the end time, which is converted to a {@code LocalTime}, not null
* @param unit the unit to measure the period in, not null
* @return the amount of the period between this time and the end time
* @throws DateTimeException if the period cannot be calculated
* @throws ArithmeticException if numeric overflow occurs
*/
until(endExclusive, unit) {
var end = LocalTime.from(endExclusive);
if (unit instanceof ChronoUnit) {
var nanosUntil = end.toNanoOfDay() - this.toNanoOfDay(); // no overflow
switch (unit) {
case ChronoUnit.NANOS: return nanosUntil;
case ChronoUnit.MICROS: return MathUtil.intDiv(nanosUntil, 1000);
case ChronoUnit.MILLIS: return MathUtil.intDiv(nanosUntil, 1000000);
case ChronoUnit.SECONDS: return MathUtil.intDiv(nanosUntil, LocalTime.NANOS_PER_SECOND);
case ChronoUnit.MINUTES: return MathUtil.intDiv(nanosUntil, LocalTime.NANOS_PER_MINUTE);
case ChronoUnit.HOURS: return MathUtil.intDiv(nanosUntil, LocalTime.NANOS_PER_HOUR);
case ChronoUnit.HALF_DAYS: return MathUtil.intDiv(nanosUntil, (12 * LocalTime.NANOS_PER_HOUR));
}
throw new UnsupportedTemporalTypeException('Unsupported unit: ' + unit);
}
return unit.between(this, end);
}
//-----------------------------------------------------------------------
/**
* Combines this time with a date to create a {@code LocalDateTime}.
* <p>
* This returns a {@code LocalDateTime} formed from this time at the specified date.
* All possible combinations of date and time are valid.
*
* @param date the date to combine with, not null
* @return the local date-time formed from this time and the specified date, not null
*/
atDate(date) {
return LocalDateTime.of(date, this);
}
/**
* Combines this time with an offset to create an {@code OffsetTime}.
* <p>
* This returns an {@code OffsetTime} formed from this time at the specified offset.
* All possible combinations of time and offset are valid.
*
* @param offset the offset to combine with, not null
* @return the offset time formed from this time and the specified offset, not null
*/
atOffset(offset) {
return OffsetTime.of(this, offset);
}
//-----------------------------------------------------------------------
/**
* Extracts the time as seconds of day,
* from {@code 0} to {@code 24 * 60 * 60 - 1}.
*
* @return the second-of-day equivalent to this time
*/
toSecondOfDay() {
var total = this._hour * LocalTime.SECONDS_PER_HOUR;
total += this._minute * LocalTime.SECONDS_PER_MINUTE;
total += this._second;
return total;
}
/**
* Extracts the time as nanos of day,
* from {@code 0} to {@code 24 * 60 * 60 * 1,000,000,000 - 1}.
*
* @return the nano of day equivalent to this time
*/
toNanoOfDay() {
var total = this._hour * LocalTime.NANOS_PER_HOUR;
total += this._minute * LocalTime.NANOS_PER_MINUTE;
total += this._second * LocalTime.NANOS_PER_SECOND;
total += this._nano;
return total;
}
//-----------------------------------------------------------------------
/**
* Compares this {@code LocalTime} to another time.
* <p>
* The comparison is based on the time-line position of the local times within a day.
* It is 'consistent with equals', as defined by {@link Comparable}.
*
* @param other the other time to compare to, not null
* @return the comparator value, negative if less, positive if greater
* @throws NullPointerException if {@code other} is null
*/
compareTo(other) {
requireNonNull(other, 'other');
requireInstance(other, LocalTime, 'other');
var cmp = MathUtil.compareNumbers(this._hour, other._hour);
if (cmp === 0) {
cmp = MathUtil.compareNumbers(this._minute, other._minute);
if (cmp === 0) {
cmp = MathUtil.compareNumbers(this._second, other._second);
if (cmp === 0) {
cmp = MathUtil.compareNumbers(this._nano, other._nano);
}
}
}
return cmp;
}
/**
* Checks if this {@code LocalTime} is after the specified time.
* <p>
* The comparison is based on the time-line position of the time within a day.
*
* @param other the other time to compare to, not null
* @return true if this is after the specified time
* @throws NullPointerException if {@code other} is null
*/
isAfter(other) {
return this.compareTo(other) > 0;
}
/**
* Checks if this {@code LocalTime} is before the specified time.
* <p>
* The comparison is based on the time-line position of the time within a day.
*
* @param other the other time to compare to, not null
* @return true if this point is before the specified time
* @throws NullPointerException if {@code other} is null
*/
isBefore(other) {
return this.compareTo(other) < 0;
}
//-----------------------------------------------------------------------
/**
* Checks if this time is equal to another time.
* <p>
* The comparison is based on the time-line position of the time within a day.
* <p>
* Only objects of type {@code LocalTime} are compared, other types return false.
* To compare the date of two {@code TemporalAccessor} instances, use
* {@link ChronoField#NANO_OF_DAY} as a comparator.
*
* @param other the object to check, null returns false
* @return true if this is equal to the other time
*/
equals(other) {
if (this === other) {
return true;
}
if (other instanceof LocalTime) {
return this._hour === other._hour && this._minute === other._minute &&
this._second === other._second && this._nano === other._nano;
}
return false;
}
/**
* A hash code for this time.
*
* @return a suitable hash code
*/
hashCode() {
var nod = this.toNanoOfDay();
return (nod ^ (nod >>> 24));
}
//-----------------------------------------------------------------------
/**
* Outputs this time as a {@code String}, such as {@code 10:15}.
* <p>
* The output will be one of the following ISO-8601 formats:
* <p><ul>
* <li>{@code HH:mm}</li>
* <li>{@code HH:mm:ss}</li>
* <li>{@code HH:mm:ss.SSS}</li>
* <li>{@code HH:mm:ss.SSSSSS}</li>
* <li>{@code HH:mm:ss.SSSSSSSSS}</li>
* </ul><p>
* The format used will be the shortest that outputs the full value of
* the time where the omitted parts are implied to be zero.
*
* @return a string representation of this time, not null
*/
toString() {
var buf = '';
var hourValue = this._hour;
var minuteValue = this._minute;
var secondValue = this._second;
var nanoValue = this._nano;
buf += hourValue < 10 ? '0' : '';
buf += hourValue;
buf += minuteValue < 10 ? ':0' : ':';
buf += minuteValue;
if (secondValue > 0 || nanoValue > 0) {
buf += secondValue < 10 ? ':0' : ':';
buf += secondValue;
if (nanoValue > 0) {
buf += '.';
if(MathUtil.intMod(nanoValue, 1000000) === 0) {
buf += ('' + (MathUtil.intDiv(nanoValue, 1000000) + 1000)).substring(1);
} else if (MathUtil.intMod(nanoValue, 1000) === 0) {
buf += ('' + (MathUtil.intDiv(nanoValue, 1000) + 1000000)).substring(1);
} else {
buf += ('' + (nanoValue + 1000000000)).substring(1);
}
}
}
return buf;
}
/**
* Outputs this time as a {@code String} using the formatter.
* <p>
* This time will be passed to the formatter
* {@link DateTimeFormatter#format(TemporalAccessor) print method}.
*
* @param formatter the formatter to use, not null
* @return the formatted time string, not null
* @throws DateTimeException if an error occurs during printing
*/
format(formatter) {
requireNonNull(formatter, 'formatter');
return formatter.format(this);
}
}
LocalTime.HOURS_PER_DAY = 24;
LocalTime.MINUTES_PER_HOUR = 60;
LocalTime.MINUTES_PER_DAY = LocalTime.MINUTES_PER_HOUR * LocalTime.HOURS_PER_DAY;
export function _init() {
/**
* Constants for the local time of each hour.
*/
LocalTime.HOURS = [];
for (let i = 0; i < 24; i++) {
LocalTime.HOURS[i] = makeLocalTimeConst(i);
}
LocalTime.SECONDS_PER_MINUTE = 60;
LocalTime.SECONDS_PER_HOUR = LocalTime.SECONDS_PER_MINUTE * LocalTime.MINUTES_PER_HOUR;
LocalTime.SECONDS_PER_DAY = LocalTime.SECONDS_PER_HOUR * LocalTime.HOURS_PER_DAY;
function makeLocalTimeConst(hour = 0, minute = 0, second = 0, nano = 0) {
var localTime = Object.create(LocalTime.prototype);
TemporalAccessor.call(localTime);
localTime._hour = hour;
localTime._minute = minute;
localTime._second = second;
localTime._nano = nano;
return localTime;
}
LocalTime.NANOS_PER_SECOND = 1000000000;
/**
* The minimum supported {@code LocalTime}, '00:00'.
* This is the time of midnight at the start of the day.
*/
LocalTime.MIN = LocalTime.HOURS[0];
/**
* The maximum supported {@code LocalTime}, '23:59:59.999999999'.
* This is the time just before midnight at the end of the day.
*/
LocalTime.MAX = makeLocalTimeConst(23, 59, 59, 999999999);
/**
* The time of midnight at the start of the day, '00:00'.
*/
LocalTime.MIDNIGHT = LocalTime.HOURS[0];
/**
* The time of noon in the middle of the day, '12:00'.
*/
LocalTime.NOON = LocalTime.HOURS[12];
var FROM;
LocalTime.FROM = () => {
return FROM || (FROM = createTemporalQuery('LocalTime.FROM', (temporal) => {
return LocalTime.from(temporal);
}));
};
/**
* Hours per day.
*/
LocalTime.HOURS_PER_DAY = 24;
/**
* Minutes per hour.
*/
LocalTime.MINUTES_PER_HOUR = 60;
/**
* Minutes per day.
*/
LocalTime.MINUTES_PER_DAY = LocalTime.MINUTES_PER_HOUR * LocalTime.HOURS_PER_DAY;
/**
* Seconds per minute.
*/
LocalTime.SECONDS_PER_MINUTE = 60;
/**
* Seconds per hour.
*/
LocalTime.SECONDS_PER_HOUR = LocalTime.SECONDS_PER_MINUTE * LocalTime.MINUTES_PER_HOUR;
/**
* Seconds per day.
*/
LocalTime.SECONDS_PER_DAY = LocalTime.SECONDS_PER_HOUR * LocalTime.HOURS_PER_DAY;
/**
* Milliseconds per day.
*/
LocalTime.MILLIS_PER_DAY = LocalTime.SECONDS_PER_DAY * 1000;
/**
* Microseconds per day.
*/
LocalTime.MICROS_PER_DAY = LocalTime.SECONDS_PER_DAY * 1000000;
/**
* Nanos per second.
*/
LocalTime.NANOS_PER_SECOND = 1000000000;
/**
* Nanos per minute.
*/
LocalTime.NANOS_PER_MINUTE = LocalTime.NANOS_PER_SECOND * LocalTime.SECONDS_PER_MINUTE;
/**
* Nanos per hour.
*/
LocalTime.NANOS_PER_HOUR = LocalTime.NANOS_PER_MINUTE * LocalTime.MINUTES_PER_HOUR;
/**
* Nanos per day.
*/
LocalTime.NANOS_PER_DAY = LocalTime.NANOS_PER_HOUR * LocalTime.HOURS_PER_DAY;
}

@@ -17,3 +17,5 @@ /**

var r = x/y;
if(r < 0){
if(r === 0){
return 0;
} else if(r < 0){
return Math.ceil(r);

@@ -27,3 +29,5 @@ } else {

var r = x - MathUtil.intDiv(x, y) * y;
if(r < 0){
if(r === 0){
return 0;
} else if(r < 0){
return Math.ceil(r);

@@ -81,3 +85,3 @@ } else {

}
return r;
return -1 * r;
}

@@ -100,4 +104,4 @@ if (y === 0) {

// TODO: is this correct and complete?
if (r < MIN_SAFE_INTEGER || r > MAX_SAFE_INTEGER || r === x || r === y) {
throw new ArithmeticException('Invalid subtraction beyond MIN_SAFE_INTEGER!');
if (r < MIN_SAFE_INTEGER || r > MAX_SAFE_INTEGER) {
throw new ArithmeticException('Invalid subtraction beyond MIN_SAFE_INTEGER! ' + x + '-' + y);
}

@@ -124,2 +128,26 @@ return r;

static safeToInt(value) {
if(value === 0){
return 0;
}
if (isNaN(value)) {
throw new ArithmeticException('Invalid int value, using NaN as argument');
}
if (value > MAX_SAFE_INTEGER || value < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Calculation overflows an int: ' + value);
}
return value;
}
static parseInt(value) {
var int = parseInt(value);
if (isNaN(int)) {
throw new ArithmeticException('Invalid int value parse to NaN: ' + value);
}
if (int > MAX_SAFE_INTEGER || int < MIN_SAFE_INTEGER) {
throw new ArithmeticException('Calculation overflows an int: ' + value);
}
return int;
}
/**

@@ -126,0 +154,0 @@ * Compares two Numbers.

@@ -13,3 +13,2 @@ /**

import {TemporalQueries} from './temporal/TemporalQueries';
import './temporal/TemporalQueriesPattern';

@@ -390,2 +389,9 @@ /**

/**
* replacement for enum values
*/
static values(){
return MONTHS.slice();
}
/**
*

@@ -403,19 +409,22 @@ * @param {number} month

Month.JANUARY = new Month(1);
Month.FEBRUARY = new Month(2);
Month.MARCH = new Month(3);
Month.APRIL = new Month(4);
Month.MAY = new Month(5);
Month.JUNE = new Month(6);
Month.JULY = new Month(7);
Month.AUGUST = new Month(8);
Month.SEPTEMBER = new Month(9);
Month.OCTOBER = new Month(10);
Month.NOVEMBER = new Month(11);
Month.DECEMBER = new Month(12);
var MONTHS;
var MONTHS = [
Month.JANUARY, Month.FEBRUARY, Month.MARCH, Month.APRIL, Month.MAY, Month.JUNE,
Month.JULY, Month.AUGUST, Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER, Month.DECEMBER
];
export function _init() {
Month.JANUARY = new Month(1);
Month.FEBRUARY = new Month(2);
Month.MARCH = new Month(3);
Month.APRIL = new Month(4);
Month.MAY = new Month(5);
Month.JUNE = new Month(6);
Month.JULY = new Month(7);
Month.AUGUST = new Month(8);
Month.SEPTEMBER = new Month(9);
Month.OCTOBER = new Month(10);
Month.NOVEMBER = new Month(11);
Month.DECEMBER = new Month(12);
MONTHS = [
Month.JANUARY, Month.FEBRUARY, Month.MARCH, Month.APRIL, Month.MAY, Month.JUNE,
Month.JULY, Month.AUGUST, Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER, Month.DECEMBER
];
}

@@ -135,31 +135,65 @@ /**

ChronoField.NANO_OF_SECOND = new ChronoField('NanoOfSecond', ChronoUnit.NANOS, ChronoUnit.SECONDS, ValueRange.of(0, 999999999));
export function _init() {
ChronoField.NANO_OF_DAY = new ChronoField('NanoOfDay', ChronoUnit.NANOS, ChronoUnit.DAYS, ValueRange.of(0, 86400 * 1000000000 - 1));
ChronoField.NANO_OF_SECOND = new ChronoField('NanoOfSecond', ChronoUnit.NANOS, ChronoUnit.SECONDS, ValueRange.of(0, 999999999));
ChronoField.MICRO_OF_SECOND = new ChronoField('MicroOfSecond', ChronoUnit.MICROS, ChronoUnit.SECONDS, ValueRange.of(0, 999999));
ChronoField.NANO_OF_DAY = new ChronoField('NanoOfDay', ChronoUnit.NANOS, ChronoUnit.DAYS, ValueRange.of(0, 86400 * 1000000000 - 1));
ChronoField.MILLI_OF_SECOND = new ChronoField('MilliOfSecond', ChronoUnit.MILLIS, ChronoUnit.SECONDS, ValueRange.of(0, 999));
ChronoField.MICRO_OF_SECOND = new ChronoField('MicroOfSecond', ChronoUnit.MICROS, ChronoUnit.SECONDS, ValueRange.of(0, 999999));
ChronoField.OFFSET_SECONDS = new ChronoField('OffsetSeconds', ChronoUnit.SECONDS, ChronoUnit.FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
ChronoField.MICRO_OF_DAY = new ChronoField('MicroOfDay', ChronoUnit.MICROS, ChronoUnit.DAYS, ValueRange.of(0, 86400 * 1000000 - 1));
ChronoField.HOUR_OF_DAY = new ChronoField('HourOfDay', ChronoUnit.HOURS, ChronoUnit.DAYS, ValueRange.of(0, 23));
ChronoField.MILLI_OF_SECOND = new ChronoField('MilliOfSecond', ChronoUnit.MILLIS, ChronoUnit.SECONDS, ValueRange.of(0, 999));
ChronoField.DAY_OF_WEEK = new ChronoField('DayOfWeek', ChronoUnit.DAYS, ChronoUnit.WEEKS, ValueRange.of(1, 7));
ChronoField.MILLI_OF_DAY = new ChronoField('MilliOfDay', ChronoUnit.MILLIS, ChronoUnit.DAYS, ValueRange.of(0, 86400 * 1000 - 1));
ChronoField.DAY_OF_MONTH = new ChronoField('DayOfMonth', ChronoUnit.DAYS, ChronoUnit.MONTHS, ValueRange.of(1, 28, 31), 'day');
ChronoField.SECOND_OF_MINUTE = new ChronoField('SecondOfMinute', ChronoUnit.SECONDS, ChronoUnit.MINUTES, ValueRange.of(0, 59));
ChronoField.MONTH_OF_YEAR = new ChronoField('MonthOfYear', ChronoUnit.MONTHS, ChronoUnit.YEARS, ValueRange.of(1, 12), 'month');
ChronoField.SECOND_OF_DAY = new ChronoField('SecondOfDay', ChronoUnit.SECONDS, ChronoUnit.DAYS, ValueRange.of(0, 86400 - 1));
ChronoField.DAY_OF_YEAR = new ChronoField('DayOfYear', ChronoUnit.DAYS, ChronoUnit.YEARS, ValueRange.of(1, 365, 366));
ChronoField.MINUTE_OF_HOUR = new ChronoField('MinuteOfHour', ChronoUnit.MINUTES, ChronoUnit.HOURS, ValueRange.of(0, 59));
ChronoField.EPOCH_DAY = new ChronoField('EpochDay', ChronoUnit.DAYS, ChronoUnit.FOREVER, ValueRange.of(Math.floor(Year.MIN_VALUE * 365.25), Math.floor(Year.MAX_VALUE * 365.25)));
ChronoField.MINUTE_OF_DAY = new ChronoField('MinuteOfDay', ChronoUnit.MINUTES, ChronoUnit.DAYS, ValueRange.of(0, (24 * 60) - 1));
ChronoField.YEAR = new ChronoField('Year', ChronoUnit.YEARS, ChronoUnit.FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), 'year');
ChronoField.HOUR_OF_AMPM = new ChronoField('HourOfAmPm', ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ValueRange.of(0, 11));
ChronoField.ERA = new ChronoField('Era', ChronoUnit.ERAS, ChronoUnit.FOREVER, ValueRange.of(0, 1));
ChronoField.CLOCK_HOUR_OF_AMPM = new ChronoField('ClockHourOfAmPm', ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ValueRange.of(1, 12));
ChronoField.INSTANT_SECONDS = new ChronoField('InstantSeconds', ChronoUnit.SECONDS, ChronoUnit.FOREVER, ValueRange.of(MIN_SAFE_INTEGER, MAX_SAFE_INTEGER));
ChronoField.HOUR_OF_DAY = new ChronoField('HourOfDay', ChronoUnit.HOURS, ChronoUnit.DAYS, ValueRange.of(0, 23));
ChronoField.CLOCK_HOUR_OF_DAY = new ChronoField('ClockHourOfDay', ChronoUnit.HOURS, ChronoUnit.DAYS, ValueRange.of(1, 24));
ChronoField.AMPM_OF_DAY = new ChronoField('AmPmOfDay', ChronoUnit.HALF_DAYS, ChronoUnit.DAYS, ValueRange.of(0, 1));
ChronoField.DAY_OF_WEEK = new ChronoField('DayOfWeek', ChronoUnit.DAYS, ChronoUnit.WEEKS, ValueRange.of(1, 7));
ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH = new ChronoField('AlignedDayOfWeekInMonth', ChronoUnit.DAYS, ChronoUnit.WEEKS, ValueRange.of(1, 7));
ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR = new ChronoField('AlignedDayOfWeekInYear', ChronoUnit.DAYS, ChronoUnit.WEEKS, ValueRange.of(1, 7));
ChronoField.DAY_OF_MONTH = new ChronoField('DayOfMonth', ChronoUnit.DAYS, ChronoUnit.MONTHS, ValueRange.of(1, 28, 31), 'day');
ChronoField.DAY_OF_YEAR = new ChronoField('DayOfYear', ChronoUnit.DAYS, ChronoUnit.YEARS, ValueRange.of(1, 365, 366));
ChronoField.EPOCH_DAY = new ChronoField('EpochDay', ChronoUnit.DAYS, ChronoUnit.FOREVER, ValueRange.of(Math.floor(Year.MIN_VALUE * 365.25), Math.floor(Year.MAX_VALUE * 365.25)));
ChronoField.ALIGNED_WEEK_OF_MONTH = new ChronoField('AlignedWeekOfMonth', ChronoUnit.WEEKS, ChronoUnit.MONTHS, ValueRange.of(1, 4, 5));
ChronoField.ALIGNED_WEEK_OF_YEAR = new ChronoField('AlignedWeekOfYear', ChronoUnit.WEEKS, ChronoUnit.YEARS, ValueRange.of(1, 53));
ChronoField.MONTH_OF_YEAR = new ChronoField('MonthOfYear', ChronoUnit.MONTHS, ChronoUnit.YEARS, ValueRange.of(1, 12), 'month');
ChronoField.PROLEPTIC_MONTH = new ChronoField('ProlepticMonth', ChronoUnit.MONTHS, ChronoUnit.FOREVER, ValueRange.of(Year.MIN_VALUE * 12, Year.MAX_VALUE * 12 + 11));
ChronoField.YEAR_OF_ERA = new ChronoField('YearOfEra', ChronoUnit.YEARS, ChronoUnit.FOREVER, ValueRange.of(1, Year.MAX_VALUE, Year.MAX_VALUE + 1));
ChronoField.YEAR = new ChronoField('Year', ChronoUnit.YEARS, ChronoUnit.FOREVER, ValueRange.of(Year.MIN_VALUE, Year.MAX_VALUE), 'year');
ChronoField.ERA = new ChronoField('Era', ChronoUnit.ERAS, ChronoUnit.FOREVER, ValueRange.of(0, 1));
ChronoField.INSTANT_SECONDS = new ChronoField('InstantSeconds', ChronoUnit.SECONDS, ChronoUnit.FOREVER, ValueRange.of(MIN_SAFE_INTEGER, MAX_SAFE_INTEGER));
ChronoField.OFFSET_SECONDS = new ChronoField('OffsetSeconds', ChronoUnit.SECONDS, ChronoUnit.FOREVER, ValueRange.of(-18 * 3600, 18 * 3600));
}

@@ -233,113 +233,116 @@ /**

}
/**
* Unit that represents the concept of a nanosecond, the smallest supported unit of time.
* For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
*/
ChronoUnit.NANOS = new ChronoUnit('Nanos', Duration.ofNanos(1));
/**
* Unit that represents the concept of a microsecond.
* For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
*/
ChronoUnit.MICROS = new ChronoUnit('Micros', Duration.ofNanos(1000));
/**
* Unit that represents the concept of a millisecond.
* For the ISO calendar system, it is equal to the 1000th part of the second unit.
*/
ChronoUnit.MILLIS = new ChronoUnit('Millis', Duration.ofNanos(1000000));
/**
* Unit that represents the concept of a second.
* For the ISO calendar system, it is equal to the second in the SI system
* of units, except around a leap-second.
*/
ChronoUnit.SECONDS = new ChronoUnit('Seconds', Duration.ofSeconds(1));
/**
* Unit that represents the concept of a minute.
* For the ISO calendar system, it is equal to 60 seconds.
*/
ChronoUnit.MINUTES = new ChronoUnit('Minutes', Duration.ofSeconds(60));
/**
* Unit that represents the concept of an hour.
* For the ISO calendar system, it is equal to 60 minutes.
*/
ChronoUnit.HOURS = new ChronoUnit('Hours', Duration.ofSeconds(3600));
/**
* Unit that represents the concept of half a day, as used in AM/PM.
* For the ISO calendar system, it is equal to 12 hours.
*/
ChronoUnit.HALF_DAYS = new ChronoUnit('HalfDays', Duration.ofSeconds(43200));
/**
* Unit that represents the concept of a day.
* For the ISO calendar system, it is the standard day from midnight to midnight.
* The estimated duration of a day is {@code 24 Hours}.
* <p>
* When used with other calendar systems it must correspond to the day defined by
* the rising and setting of the Sun on Earth. It is not required that days begin
* at midnight - when converting between calendar systems, the date should be
* equivalent at midday.
*/
ChronoUnit.DAYS = new ChronoUnit('Days', Duration.ofSeconds(86400));
/**
* Unit that represents the concept of a week.
* For the ISO calendar system, it is equal to 7 days.
* <p>
* When used with other calendar systems it must correspond to an integral number of days.
*/
ChronoUnit.WEEKS = new ChronoUnit('Weeks', Duration.ofSeconds(7 * 86400));
/**
* Unit that represents the concept of a month.
* For the ISO calendar system, the length of the month varies by month-of-year.
* The estimated duration of a month is one twelfth of {@code 365.2425 Days}.
* <p>
* When used with other calendar systems it must correspond to an integral number of days.
*/
ChronoUnit.MONTHS = new ChronoUnit('Months', Duration.ofSeconds(31556952 / 12));
/**
* Unit that represents the concept of a year.
* For the ISO calendar system, it is equal to 12 months.
* The estimated duration of a year is {@code 365.2425 Days}.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* or months roughly equal to a year defined by the passage of the Earth around the Sun.
*/
ChronoUnit.YEARS = new ChronoUnit('Years', Duration.ofSeconds(31556952));
/**
* Unit that represents the concept of a decade.
* For the ISO calendar system, it is equal to 10 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.DECADES = new ChronoUnit('Decades', Duration.ofSeconds(31556952 * 10));
/**
* Unit that represents the concept of a century.
* For the ISO calendar system, it is equal to 100 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.CENTURIES = new ChronoUnit('Centuries', Duration.ofSeconds(31556952 * 100));
/**
* Unit that represents the concept of a millennium.
* For the ISO calendar system, it is equal to 1000 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.MILLENNIA = new ChronoUnit('Millennia', Duration.ofSeconds(31556952 * 1000));
/**
* Unit that represents the concept of an era.
* The ISO calendar system doesn't have eras thus it is impossible to add
* an era to a date or date-time.
* The estimated duration of the era is artificially defined as {Year.MAX_VALUE} + 1.
* <p>
* When used with other calendar systems there are no restrictions on the unit.
*/
ChronoUnit.ERAS = new ChronoUnit('Eras', Duration.ofSeconds(31556952 * (Year.MAX_VALUE + 1)));
/**
* Artificial unit that represents the concept of forever.
* This is primarily used with {@link TemporalField} to represent unbounded fields
* such as the year or era.
* The estimated duration of the era is artificially defined as the largest duration
* supported by {@code Duration}.
*/
ChronoUnit.FOREVER = new ChronoUnit('Forever', Duration.ofSeconds(Number.MAX_SAFE_INTEGER, 999999999));
export function _init() {
/**
* Unit that represents the concept of a nanosecond, the smallest supported unit of time.
* For the ISO calendar system, it is equal to the 1,000,000,000th part of the second unit.
*/
ChronoUnit.NANOS = new ChronoUnit('Nanos', Duration.ofNanos(1));
/**
* Unit that represents the concept of a microsecond.
* For the ISO calendar system, it is equal to the 1,000,000th part of the second unit.
*/
ChronoUnit.MICROS = new ChronoUnit('Micros', Duration.ofNanos(1000));
/**
* Unit that represents the concept of a millisecond.
* For the ISO calendar system, it is equal to the 1000th part of the second unit.
*/
ChronoUnit.MILLIS = new ChronoUnit('Millis', Duration.ofNanos(1000000));
/**
* Unit that represents the concept of a second.
* For the ISO calendar system, it is equal to the second in the SI system
* of units, except around a leap-second.
*/
ChronoUnit.SECONDS = new ChronoUnit('Seconds', Duration.ofSeconds(1));
/**
* Unit that represents the concept of a minute.
* For the ISO calendar system, it is equal to 60 seconds.
*/
ChronoUnit.MINUTES = new ChronoUnit('Minutes', Duration.ofSeconds(60));
/**
* Unit that represents the concept of an hour.
* For the ISO calendar system, it is equal to 60 minutes.
*/
ChronoUnit.HOURS = new ChronoUnit('Hours', Duration.ofSeconds(3600));
/**
* Unit that represents the concept of half a day, as used in AM/PM.
* For the ISO calendar system, it is equal to 12 hours.
*/
ChronoUnit.HALF_DAYS = new ChronoUnit('HalfDays', Duration.ofSeconds(43200));
/**
* Unit that represents the concept of a day.
* For the ISO calendar system, it is the standard day from midnight to midnight.
* The estimated duration of a day is {@code 24 Hours}.
* <p>
* When used with other calendar systems it must correspond to the day defined by
* the rising and setting of the Sun on Earth. It is not required that days begin
* at midnight - when converting between calendar systems, the date should be
* equivalent at midday.
*/
ChronoUnit.DAYS = new ChronoUnit('Days', Duration.ofSeconds(86400));
/**
* Unit that represents the concept of a week.
* For the ISO calendar system, it is equal to 7 days.
* <p>
* When used with other calendar systems it must correspond to an integral number of days.
*/
ChronoUnit.WEEKS = new ChronoUnit('Weeks', Duration.ofSeconds(7 * 86400));
/**
* Unit that represents the concept of a month.
* For the ISO calendar system, the length of the month varies by month-of-year.
* The estimated duration of a month is one twelfth of {@code 365.2425 Days}.
* <p>
* When used with other calendar systems it must correspond to an integral number of days.
*/
ChronoUnit.MONTHS = new ChronoUnit('Months', Duration.ofSeconds(31556952 / 12));
/**
* Unit that represents the concept of a year.
* For the ISO calendar system, it is equal to 12 months.
* The estimated duration of a year is {@code 365.2425 Days}.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* or months roughly equal to a year defined by the passage of the Earth around the Sun.
*/
ChronoUnit.YEARS = new ChronoUnit('Years', Duration.ofSeconds(31556952));
/**
* Unit that represents the concept of a decade.
* For the ISO calendar system, it is equal to 10 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.DECADES = new ChronoUnit('Decades', Duration.ofSeconds(31556952 * 10));
/**
* Unit that represents the concept of a century.
* For the ISO calendar system, it is equal to 100 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.CENTURIES = new ChronoUnit('Centuries', Duration.ofSeconds(31556952 * 100));
/**
* Unit that represents the concept of a millennium.
* For the ISO calendar system, it is equal to 1000 years.
* <p>
* When used with other calendar systems it must correspond to an integral number of days
* and is normally an integral number of years.
*/
ChronoUnit.MILLENNIA = new ChronoUnit('Millennia', Duration.ofSeconds(31556952 * 1000));
/**
* Unit that represents the concept of an era.
* The ISO calendar system doesn't have eras thus it is impossible to add
* an era to a date or date-time.
* The estimated duration of the era is artificially defined as {Year.MAX_VALUE} + 1.
* <p>
* When used with other calendar systems there are no restrictions on the unit.
*/
ChronoUnit.ERAS = new ChronoUnit('Eras', Duration.ofSeconds(31556952 * (Year.MAX_VALUE + 1)));
/**
* Artificial unit that represents the concept of forever.
* This is primarily used with {@link TemporalField} to represent unbounded fields
* such as the year or era.
* The estimated duration of the era is artificially defined as the largest duration
* supported by {@code Duration}.
*/
ChronoUnit.FOREVER = new ChronoUnit('Forever', Duration.ofSeconds(Number.MAX_SAFE_INTEGER, 999999999));
}

@@ -9,2 +9,8 @@ /**

import {ChronoField} from './ChronoField';
import {LocalDate} from '../LocalDate';
import {LocalTime} from '../LocalTime';
import {ZoneOffset} from '../ZoneOffset';
/**

@@ -239,1 +245,64 @@ * Common implementations of {@code TemporalQuery}.

}
export function _init() {
//-----------------------------------------------------------------------
/**
* A strict query for the {@code ZoneId}.
*/
TemporalQueries.ZONE_ID = createTemporalQuery('ZONE_ID', (temporal) => {
return temporal.query(TemporalQueries.ZONE_ID);
});
/**
* A query for the {@code Chronology}.
*/
TemporalQueries.CHRONO = createTemporalQuery('CHRONO', (temporal) => {
return temporal.query(TemporalQueries.CHRONO);
});
/**
* A query for the smallest supported unit.
*/
TemporalQueries.PRECISION = createTemporalQuery('PRECISION', (temporal) => {
return temporal.query(TemporalQueries.PRECISION);
});
//-----------------------------------------------------------------------
/**
* A query for {@code ZoneOffset} returning null if not found.
*/
TemporalQueries.OFFSET = createTemporalQuery('OFFSET', (temporal) => {
if (temporal.isSupported(ChronoField.OFFSET_SECONDS)) {
return ZoneOffset.ofTotalSeconds(temporal.get(TemporalQueries.OFFSET_SECONDS));
}
return null;
});
/**
* A lenient query for the {@code ZoneId}, falling back to the {@code ZoneOffset}.
*/
TemporalQueries.ZONE = createTemporalQuery('ZONE', (temporal) => {
var zone = temporal.query(TemporalQueries.ZONE_ID);
return (zone != null ? zone : temporal.query(TemporalQueries.OFFSET));
});
/**
* A query for {@code LocalDate} returning null if not found.
*/
TemporalQueries.LOCAL_DATE = createTemporalQuery('LOCAL_DATE', (temporal) => {
if (temporal.isSupported(ChronoField.EPOCH_DAY)) {
return LocalDate.ofEpochDay(temporal.getLong(TemporalQueries.EPOCH_DAY));
}
return null;
});
/**
* A query for {@code LocalTime} returning null if not found.
*/
TemporalQueries.LOCAL_TIME = createTemporalQuery('LOCAL_TIME', (temporal) => {
if (temporal.isSupported(ChronoField.NANO_OF_DAY)) {
return LocalTime.ofNanoOfDay(temporal.getLong(TemporalQueries.NANO_OF_DAY));
}
return null;
});
}

@@ -35,9 +35,11 @@ /**

/**
* The minimum supported year
*/
Year.MIN_VALUE = -999999;
/**
* The maximum supported year
*/
Year.MAX_VALUE = 999999;
export function _init() {
/**
* The minimum supported year
*/
Year.MIN_VALUE = -999999;
/**
* The maximum supported year
*/
Year.MAX_VALUE = 999999;
}

@@ -9,5 +9,2 @@ /**

const MAX_SECONDS = 18 * LocalTime.SECONDS_PER_HOUR;
var SECONDS_CACHE = {};
export class ZoneOffset {

@@ -43,3 +40,3 @@ constructor(totalSeconds){

static validateTotalSeconds(totalSeconds){
if (Math.abs(totalSeconds) > MAX_SECONDS) {
if (Math.abs(totalSeconds) > ZoneOffset.MAX_SECONDS) {
throw new DateTimeException('Zone offset not in valid range: -18:00 to +18:00');

@@ -100,6 +97,6 @@ }

var totalSecs = totalSeconds;
var result = SECONDS_CACHE[totalSecs];
var result = ZoneOffset.SECONDS_CACHE[totalSecs];
if (result == null) {
result = new ZoneOffset(totalSeconds);
SECONDS_CACHE[totalSecs] = result;
ZoneOffset.SECONDS_CACHE[totalSecs] = result;
}

@@ -114,4 +111,8 @@ return result;

ZoneOffset.UTC = ZoneOffset.ofTotalSeconds(0);
ZoneOffset.MIN = ZoneOffset.ofTotalSeconds(-MAX_SECONDS);
ZoneOffset.MAX = ZoneOffset.ofTotalSeconds(MAX_SECONDS);
export function _init() {
ZoneOffset.MAX_SECONDS = 18 * LocalTime.SECONDS_PER_HOUR;
ZoneOffset.SECONDS_CACHE = {};
ZoneOffset.UTC = ZoneOffset.ofTotalSeconds(0);
ZoneOffset.MIN = ZoneOffset.ofTotalSeconds(-ZoneOffset.MAX_SECONDS);
ZoneOffset.MAX = ZoneOffset.ofTotalSeconds(ZoneOffset.MAX_SECONDS);
}

@@ -9,2 +9,3 @@ /*

this._map = {};
return this;
}

@@ -16,10 +17,46 @@

/*
var i;
var a = {};
for(i=0; i<10000000; i++){
for(i=0; i<10000; i++){
var f = new Foo();
f.add(a, i);
}
*/
var g = Object.create(Foo.prototype);
g._map = {};
g.add('a',1);
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _possibleConstructorReturn(self, call) {
if (!self) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return call && (typeof call === "object" || typeof call === "function") ? call : self;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
enumerable: false,
writable: true,
configurable: true
}
});
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc