diff --git a/docs/moment.md b/docs/moment.md index 0885910e0..b4203bcac 100644 --- a/docs/moment.md +++ b/docs/moment.md @@ -141,7 +141,7 @@ See the [formatting guide](formatting.html) for more about the string-outputting | RFC 2822 | | `toRFC2822()` | | | HTTP date string | | `toHTTP()` | | | JS Date | `toDate()` | `toJSDate()` | | -| Epoch time | `valueOf()` | `valueOf()` or `toMillis()` | | +| Epoch time | `valueOf()` | `toMillis()` or `valueOf()` | | | Object | `toObject()` | `toObject()` | | | Duration | `diff(Moment)` | `diff(DateTime)` | Moment's diff returns a count of milliseconds, but Luxon's returns a Duration. To replicate the Moment behavior, use `dt1.diff(d2).milliseconds`. | diff --git a/package.json b/package.json index 3902c47a5..8c5e0f824 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "fs-extra": "^6.0.1", "full-icu": "^1.2.1", "husky": "^0.14.3", - "jest": "^21.2.1", + "jest": "^22.4.2", "jest-cli": "^23.1.0", "lint-staged": "^4.3.0", "prettier": "1.7.4", diff --git a/src/datetime.js b/src/datetime.js index 17d2fc92c..5e5030115 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -340,7 +340,7 @@ function quickDT(obj, zone) { * * **Week calendar**: For ISO week calendar attributes, see the {@link weekYear}, {@link weekNumber}, and {@link weekday} accessors. * * **Configuration** See the {@link locale} and {@link numberingSystem} accessors. * * **Transformation**: To transform the DateTime into other DateTimes, use {@link set}, {@link reconfigure}, {@link setZone}, {@link setLocale}, {@link plus}, {@link minus}, {@link endOf}, {@link startOf}, {@link toUTC}, and {@link toLocal}. - * * **Output**: To convert the DateTime to other representations, use the {@link toJSON}, {@link toISO}, {@link toHTTP}, {@link toObject}, {@link toRFC2822}, {@link toString}, {@link toLocaleString}, {@link toFormat}, {@link valueOf} and {@link toJSDate}. + * * **Output**: To convert the DateTime to other representations, use the {@link toJSON}, {@link toISO}, {@link toHTTP}, {@link toObject}, {@link toRFC2822}, {@link toString}, {@link toLocaleString}, {@link toFormat}, {@link toMillis} and {@link toJSDate}. * * There's plenty others documented below. In addition, for more information on subtler topics like internationalization, time zones, alternative calendars, validity, and so on, see the external documentation. */ @@ -1506,19 +1506,19 @@ export default class DateTime { } /** - * Returns the epoch milliseconds of this DateTime + * Returns the epoch milliseconds of this DateTime. Alias of {@link toMillis} * @return {number} */ valueOf() { - return this.isValid ? this.ts : NaN; + return this.toMillis(); } /** - * Returns the epoch milliseconds of this DateTime. Alias of {@link valueOf} + * Returns the epoch milliseconds of this DateTime. * @return {number} */ toMillis() { - return this.valueOf(); + return this.isValid ? this.ts : NaN; } /** @@ -1640,11 +1640,13 @@ export default class DateTime { * @return {boolean} */ equals(other) { - return this.isValid && other.isValid - ? this.valueOf() === other.valueOf() && - this.zone.equals(other.zone) && - this.loc.equals(other.loc) - : false; + return ( + this.isValid && + other.isValid && + this.valueOf() === other.valueOf() && + this.zone.equals(other.zone) && + this.loc.equals(other.loc) + ); } /** diff --git a/src/settings.js b/src/settings.js index b135ae724..7f786448f 100644 --- a/src/settings.js +++ b/src/settings.js @@ -3,7 +3,7 @@ import Locale from './impl/locale'; import { normalizeZone } from './impl/zoneUtil'; -let now = () => new Date().valueOf(), +let now = () => Date.now(), defaultZone = null, // not setting this directly to LocalZone.instance bc loading order issues defaultLocale = null, defaultNumberingSystem = null, diff --git a/test/datetime/invalid.test.js b/test/datetime/invalid.test.js index 16c8060a6..c185b490f 100644 --- a/test/datetime/invalid.test.js +++ b/test/datetime/invalid.test.js @@ -39,7 +39,7 @@ test('Invalid tell you why', () => { }); test('Invalid DateTimes return invalid Dates', () => { - expect(organic1.toJSDate().valueOf()).toBeFalsy(); + expect(organic1.toJSDate().valueOf()).toBe(NaN); }); test('Diffing invalid DateTimes creates invalid Durations', () => { diff --git a/test/datetime/transform.test.js b/test/datetime/transform.test.js index 6116ef3e1..85cd35cde 100644 --- a/test/datetime/transform.test.js +++ b/test/datetime/transform.test.js @@ -18,8 +18,23 @@ const dtMaker = () => //------ // #toMillis() //------ -test('DateTime#toMillis() just does valueOf()', () => { - expect(dt.toMillis()).toBe(dt.valueOf()); +test('DateTime#toMillis() returns milliseconds for valid DateTimes', () => { + const js = dt.toJSDate(); + expect(dt.toMillis()).toBe(js.getTime()); +}); + +test('DateTime#toMillis() returns NaN for invalid DateTimes', () => { + const invalid = DateTime.invalid('reason'); + expect(invalid.toMillis()).toBe(NaN); +}); + +//------ +// #valueOf() +//------ +test('DateTime#valueOf() just does toMillis()', () => { + expect(dt.valueOf()).toBe(dt.toMillis()); + const invalid = DateTime.invalid('reason'); + expect(invalid.valueOf()).toBe(invalid.toMillis()); }); //------