]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Prevent toISOString converting to UTC (issue #1751) (#4341)
authorAsh <ash@hexmen.com>
Sun, 17 Dec 2017 00:41:10 +0000 (00:41 +0000)
committerKunal Marwaha <marwahaha@berkeley.edu>
Sun, 17 Dec 2017 00:41:10 +0000 (19:41 -0500)
* [feature] Prevent toISOString converting to UTC (issue #1751)

* Improve readability (maybe)

* Tweak following code review

* jshint fix

src/lib/moment/format.js
src/test/moment/format.js

index d95042f20bf5bf634b1aedb8a1b4b149ccb7c7ac..d3080aa0360d044e5d3df8238fccf6ea97d1ace8 100644 (file)
@@ -9,19 +9,24 @@ export function toString () {
     return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
 }
 
-export function toISOString() {
+export function toISOString(keepOffset) {
     if (!this.isValid()) {
         return null;
     }
-    var m = this.clone().utc();
+    var utc = keepOffset !== true;
+    var m = utc ? this.clone().utc() : this;
     if (m.year() < 0 || m.year() > 9999) {
-        return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
+        return formatMoment(m, utc ? 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]' : 'YYYYYY-MM-DD[T]HH:mm:ss.SSSZ');
     }
     if (isFunction(Date.prototype.toISOString)) {
         // native implementation is ~50x faster, use it when we can
-        return this.toDate().toISOString();
+        if (utc) {
+            return this.toDate().toISOString();
+        } else {
+            return new Date(this._d.valueOf()).toISOString().replace('Z', formatMoment(m, 'Z'));
+        }
     }
-    return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
+    return formatMoment(m, utc ? 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]' : 'YYYY-MM-DD[T]HH:mm:ss.SSSZ');
 }
 
 /**
index a637d51b28f2866df19bbaa8437517f755df8646..07ed3794430897ed13675a568e35a0468532084c 100644 (file)
@@ -167,6 +167,26 @@ test('toISOString', function (assert) {
     assert.equal(date.toISOString(), null, 'An invalid date to iso string is null');
 });
 
+test('toISOString without UTC conversion', function (assert) {
+    var date = moment.utc('2016-12-31T19:53:45.678').utcOffset('+05:30');
+
+    assert.equal(date.toISOString(true), '2017-01-01T01:23:45.678+05:30', 'should output ISO8601 on moment.fn.toISOString');
+
+    // big years
+    date = moment.utc('+020122-12-31T19:53:45.678').utcOffset('+05:30');
+    assert.equal(date.toISOString(true), '+020123-01-01T01:23:45.678+05:30', 'ISO8601 format on big positive year');
+    // negative years
+    date = moment.utc('-000002-12-31T19:53:45.678').utcOffset('+05:30');
+    assert.equal(date.toISOString(true), '-000001-01-01T01:23:45.678+05:30', 'ISO8601 format on negative year');
+    // big negative years
+    date = moment.utc('-020124-12-31T19:53:45.678').utcOffset('+05:30');
+    assert.equal(date.toISOString(true), '-020123-01-01T01:23:45.678+05:30', 'ISO8601 format on big negative year');
+
+    //invalid dates
+    date = moment.utc('2017-12-32').utcOffset('+05:30');
+    assert.equal(date.toISOString(true), null, 'An invalid date to iso string is null');
+});
+
 // See https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects
 test('inspect', function (assert) {
     function roundtrip(m) {