Skip to content

Commit

Permalink
toString() gives [object Type ISO-String] asString() gives ISO-String
Browse files Browse the repository at this point in the history
  • Loading branch information
pipobscure committed Aug 2, 2018
1 parent 6ad0cd6 commit 7c46ffa
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 52 deletions.
5 changes: 4 additions & 1 deletion polyfill/lib/civildate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export class CivilDate {
withTime(time) {
return new CivilDateTime.from(this, time);
}
toString() {
asString() {
const { year, month, day } = this;
return `${spad(year, 4)}-${pad(month, 2)}-${pad(day, 2)}`;
}
toString() {
return `[object CivilDate ${this.asString()}]`;
}

static fromString(string) {
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(string);
Expand Down
5 changes: 4 additions & 1 deletion polyfill/lib/civildatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ export class CivilDateTime {
instant[VALUE] = { milliseconds, nanoseconds };
return new ZonedInstant(instant, zone);
}
toString() {
asString() {
const { year, month, day, hour, minute, second, millisecond, nanosecond } = this;
return `${spad(year, 4)}-${pad(month, 2)}-${pad(day, 2)}T${pad(hour, 2)}:${pad(minute, 2)}:${pad(second, 2)}.${pad(millisecond,3)}${pad(nanosecond, 6)}`;
}
toString() {
return `[object CivilDateTime ${this.asString()}]`;
}

static fromString(string) {
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})(\d{6})$/.exec(string);
Expand Down
5 changes: 4 additions & 1 deletion polyfill/lib/civiltime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ export class CivilTime {
withDate(date) {
return new CivilDateTime.from(date, this);
}
toString() {
asString() {
const { hour, minute, second, millisecond, nanosecond } = this;
return `${pad(hour, 2)}:${pad(minute, 2)}:${pad(second, 2)}.${pad(millisecond, 3)}${pad(nanosecond, 6)}`;
}
toString() {
return `[object CivilTime ${this.asString()}]`;
}

static fromString(string) {
const match = /^(\d{2}):(\d{2}):(\d{2})\.(\d{3})(\d{6})$/.exec(string);
Expand Down
5 changes: 4 additions & 1 deletion polyfill/lib/instant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ export class Instant {
format(locale, options) {
return this.withZone().format(locale, options);
}
toString() {
asString() {
const { year, month, day, hour, minute, second, millisecond } = fromEpoch(this.milliseconds, 'UTC');
const nanosecond = this.nanoseconds;
return `${spad(year,4)}-${pad(month,2)}-${pad(day,2)}T${pad(hour,2)}:${pad(minute,2)}:${pad(second,2)}.${pad(millisecond,3)}${pad(nanosecond,6)}Z`;
}
toString() {
return `[object Instant ${this.asString()}]`;
}

static fromString(string) {
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})(\d{6})Z$/.exec(string);
Expand Down
5 changes: 4 additions & 1 deletion polyfill/lib/zonedinstant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ export class ZonedInstant{
);
return fmt.format(new Date(this.toInstant().milliseconds));
}
toString() {
asString() {
const ts = this[INSTANT].milliseconds;
const { year, month, day, hour, minute, second, millisecond } = fromEpoch(ts, this[ZONE]);
const nanosecond = this[INSTANT].nanoseconds;
const offset = ([ 'UTC', '+00:00', '-00:00' ].indexOf(this[ZONE]) > -1) ? 'Z' : zoneOffset(ts, this[ZONE]);
return `${spad(year,4)}-${pad(month,2)}-${pad(day,2)}T${pad(hour,2)}:${pad(minute,2)}:${pad(second,2)}.${pad(millisecond,3)}${pad(nanosecond,6)}${offset}`;
}
toString() {
return `[object ZonedInstant ${this.asString()}]`;
}

static fromString(string) {
const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})(\d{6})(Z|[+-]\d{2}:\d{2})$/.exec(string);
Expand Down
3 changes: 2 additions & 1 deletion polyfill/test/compat/civildate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ test('CivilDate', ({ test, end })=>{
equal(instance.second, undefined);
equal(instance.millisecond, undefined);
equal(instance.nanosecond, undefined);
equal(instance.toString(), '1976-11-18');
equal(instance.asString(), '1976-11-18');
equal(instance.toString(), '[object CivilDate 1976-11-18]');

end();
});
Expand Down
35 changes: 18 additions & 17 deletions polyfill/test/compat/civildatetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@ const { CivilDateTime } = require('../../');
test('CivilDateTime', ({ test, end })=>{
test('simple', ({ equal, end })=>{
const instance = new CivilDateTime(1976, 11, 18, 15, 23, 30);
equal('[object CivilDateTime 1976-11-18T15:23:30.000000000]', instance.toString());

equal('1976-11-18T15:23:30.000000000', instance.toString());
equal('1976-11-18T15:23:30.000000000+01:00', instance.withZone('Europe/Berlin').toString());
equal('1976-11-18T15:23:30.000000000', instance.asString());
equal('1976-11-18T15:23:30.000000000+01:00', instance.withZone('Europe/Berlin').asString());

const anniversary = instance.plus({ years: 3 });
equal('1979-11-18T15:23:30.000000000', anniversary.toString());
equal('1979-11-18T15:23:30.000000000+01:00', anniversary.withZone('Europe/Berlin').toString());
equal('1979-11-18T15:23:30.000000000', anniversary.asString());
equal('1979-11-18T15:23:30.000000000+01:00', anniversary.withZone('Europe/Berlin').asString());

const runup = anniversary.plus({ months: -1 });
equal('1979-10-18T15:23:30.000000000', runup.toString());
equal('1979-10-18T15:23:30.000000000+01:00', runup.withZone('Europe/Berlin').toString());
equal('1979-10-18T15:23:30.000000000', runup.asString());
equal('1979-10-18T15:23:30.000000000+01:00', runup.withZone('Europe/Berlin').asString());

const thirty = runup.plus({ days: 30 });
equal('1979-11-17T15:23:30.000000000', thirty.toString());
equal('1979-11-17T15:23:30.000000000+01:00', thirty.withZone('Europe/Berlin').toString());
equal('1979-11-17T15:23:30.000000000', thirty.asString());
equal('1979-11-17T15:23:30.000000000+01:00', thirty.withZone('Europe/Berlin').asString());

const preptime = anniversary.plus({ hours: -5, minutes: 37, seconds: 30, milliseconds: 4, nanoseconds: -3 });
equal('1979-11-18T11:01:00.003999997', preptime.toString());
equal('1979-11-18T11:01:00.003999997+01:00', preptime.withZone('Europe/Berlin').toString());
equal('1979-11-18T11:01:00.003999997', preptime.asString());
equal('1979-11-18T11:01:00.003999997+01:00', preptime.withZone('Europe/Berlin').asString());

const daybreak = anniversary.with({ hour: 0, minute: 0, second: 0 });
equal('1979-11-18T00:00:00.000000000', daybreak.toString());
equal('1979-11-18T00:00:00.000000000+01:00', daybreak.withZone('Europe/Berlin').toString());
equal('1979-11-18T00:00:00.000000000', daybreak.asString());
equal('1979-11-18T00:00:00.000000000+01:00', daybreak.withZone('Europe/Berlin').asString());

const nightfall = anniversary.with({ hour: 23, minute: 59, second: 59, millisecond: 1000, nanosecond: -1 });
equal('1979-11-18T23:59:59.999999999', nightfall.toString());
equal('1979-11-18T23:59:59.999999999+01:00', nightfall.withZone('Europe/Berlin').toString());
equal('1979-11-18T23:59:59.999999999', nightfall.asString());
equal('1979-11-18T23:59:59.999999999+01:00', nightfall.withZone('Europe/Berlin').asString());

const year = (new Date()).getFullYear()
const birthday = instance.with({ year });
equal(`${year}-11-18T15:23:30.000000000`, birthday.toString());
equal(`${year}-11-18T15:23:30.000000000+01:00`, birthday.withZone('Europe/Berlin').toString());
equal(`${year}-11-18T15:23:30.000000000`, birthday.asString());
equal(`${year}-11-18T15:23:30.000000000+01:00`, birthday.withZone('Europe/Berlin').asString());

end();
});
Expand All @@ -53,7 +54,7 @@ test('CivilDateTime', ({ test, end })=>{
equal(instance.second, 30);
equal(instance.millisecond, 450);
equal(instance.nanosecond, 12345);
equal(instance.toString(), '1976-11-18T15:23:30.450012345');
equal(instance.asString(), '1976-11-18T15:23:30.450012345');

end();
});
Expand Down
3 changes: 2 additions & 1 deletion polyfill/test/compat/civiltime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ test('CivilTime', ({ test, end })=>{
equal(instance.second, 30);
equal(instance.millisecond, 450);
equal(instance.nanosecond, 12345);
equal(instance.toString(), '15:23:30.450012345');
equal(instance.asString(), '15:23:30.450012345');
equal(instance.toString(), '[object CivilTime 15:23:30.450012345]');

end();
});
Expand Down
7 changes: 4 additions & 3 deletions polyfill/test/compat/instant.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ test('Instant', ({ test, end})=>{
test('simple', ({ equal, end })=>{
const instant = Instant.fromMilliseconds(0);

equal('1970-01-01T00:00:00.000000000Z', instant.toString());
equal('[object Instant 1970-01-01T00:00:00.000000000Z]', instant.toString());
equal('1970-01-01T00:00:00.000000000Z', instant.asString());

const now = new Date();
const nowi = Instant.fromMilliseconds(now);
equal(now.toISOString().replace('Z','000000Z'), nowi.toString());
equal(now.toISOString().replace('Z','000000Z'), nowi.asString());
end();
});

Expand All @@ -20,7 +21,7 @@ test('Instant', ({ test, end})=>{
equal(instance instanceof Instant, true);
equal(instance.milliseconds, 217178610450);
equal(instance.nanoseconds, 0);
equal(instance.toString(), '1976-11-18T15:23:30.450000000Z');
equal(instance.asString(), '1976-11-18T15:23:30.450000000Z');

end();
});
Expand Down
3 changes: 2 additions & 1 deletion polyfill/test/compat/zonedinstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ test('ZonedInstant', ({ test, end})=>{
equal(instance instanceof ZonedInstant, true);
equal(instance.milliseconds, 217175010450);
equal(instance.nanoseconds, 0);
equal(instance.toString(), '1976-11-18T15:23:30.450000000+01:00');
equal(instance.asString(), '1976-11-18T15:23:30.450000000+01:00');
equal(instance.toString(), '[object ZonedInstant 1976-11-18T15:23:30.450000000+01:00]');
end();
});

Expand Down
3 changes: 2 additions & 1 deletion polyfill/test/temporal/CivilDate/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ assert.sameValue(instance.minute, undefined);
assert.sameValue(instance.second, undefined);
assert.sameValue(instance.millisecond, undefined);
assert.sameValue(instance.nanosecond, undefined);
assert.sameValue(instance.toString(), '1976-11-18');
assert.sameValue(instance.asString(), '1976-11-18');
assert.sameValue(instance.toString(), '[object CivilDate 1976-11-18]');
34 changes: 18 additions & 16 deletions polyfill/test/temporal/CivilDateTime/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,36 @@ esid: pending

const instance = new temporal.CivilDateTime(1976, 11, 18, 15, 23, 30);

assert.sameValue('1976-11-18T15:23:30.000000000', instance.toString());
assert.sameValue('1976-11-18T15:23:30.000000000+01:00', instance.withZone('Europe/Berlin').toString());
assert.sameValue('[object CivilDateTime 1976-11-18T15:23:30.000000000]', instance.toString());

assert.sameValue('1976-11-18T15:23:30.000000000', instance.asString());
assert.sameValue('1976-11-18T15:23:30.000000000+01:00', instance.withZone('Europe/Berlin').asString());

const anniversary = instance.plus({ years: 3 });
assert.sameValue('1979-11-18T15:23:30.000000000', anniversary.toString());
assert.sameValue('1979-11-18T15:23:30.000000000+01:00', anniversary.withZone('Europe/Berlin').toString());
assert.sameValue('1979-11-18T15:23:30.000000000', anniversary.asString());
assert.sameValue('1979-11-18T15:23:30.000000000+01:00', anniversary.withZone('Europe/Berlin').asString());

const runup = anniversary.plus({ months: -1 });
assert.sameValue('1979-10-18T15:23:30.000000000', runup.toString());
assert.sameValue('1979-10-18T15:23:30.000000000+01:00', runup.withZone('Europe/Berlin').toString());
assert.sameValue('1979-10-18T15:23:30.000000000', runup.asString());
assert.sameValue('1979-10-18T15:23:30.000000000+01:00', runup.withZone('Europe/Berlin').asString());

const thirty = runup.plus({ days: 30 });
assert.sameValue('1979-11-17T15:23:30.000000000', thirty.toString());
assert.sameValue('1979-11-17T15:23:30.000000000+01:00', thirty.withZone('Europe/Berlin').toString());
assert.sameValue('1979-11-17T15:23:30.000000000', thirty.asString());
assert.sameValue('1979-11-17T15:23:30.000000000+01:00', thirty.withZone('Europe/Berlin').asString());

const preptime = anniversary.plus({ hours: -5, minutes: 37, seconds: 30, milliseconds: 4, nanoseconds: -3 });
assert.sameValue('1979-11-18T11:01:00.003999997', preptime.toString());
assert.sameValue('1979-11-18T11:01:00.003999997+01:00', preptime.withZone('Europe/Berlin').toString());
assert.sameValue('1979-11-18T11:01:00.003999997', preptime.asString());
assert.sameValue('1979-11-18T11:01:00.003999997+01:00', preptime.withZone('Europe/Berlin').asString());

const daybreak = anniversary.with({ hour: 0, minute: 0, second: 0 });
assert.sameValue('1979-11-18T00:00:00.000000000', daybreak.toString());
assert.sameValue('1979-11-18T00:00:00.000000000+01:00', daybreak.withZone('Europe/Berlin').toString());
assert.sameValue('1979-11-18T00:00:00.000000000', daybreak.asString());
assert.sameValue('1979-11-18T00:00:00.000000000+01:00', daybreak.withZone('Europe/Berlin').asString());

const nightfall = anniversary.with({ hour: 23, minute: 59, second: 59, millisecond: 1000, nanosecond: -1 });
assert.sameValue('1979-11-18T23:59:59.999999999', nightfall.toString());
assert.sameValue('1979-11-18T23:59:59.999999999+01:00', nightfall.withZone('Europe/Berlin').toString());
assert.sameValue('1979-11-18T23:59:59.999999999', nightfall.asString());
assert.sameValue('1979-11-18T23:59:59.999999999+01:00', nightfall.withZone('Europe/Berlin').asString());

const year = (new Date()).getFullYear()
const birthday = instance.with({ year });
assert.sameValue(`${year}-11-18T15:23:30.000000000`, birthday.toString());
assert.sameValue(`${year}-11-18T15:23:30.000000000+01:00`, birthday.withZone('Europe/Berlin').toString());
assert.sameValue(`${year}-11-18T15:23:30.000000000`, birthday.asString());
assert.sameValue(`${year}-11-18T15:23:30.000000000+01:00`, birthday.withZone('Europe/Berlin').asString());
3 changes: 2 additions & 1 deletion polyfill/test/temporal/CivilDateTime/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ assert.sameValue(instance.minute, 23);
assert.sameValue(instance.second, 30);
assert.sameValue(instance.millisecond, 450);
assert.sameValue(instance.nanosecond, 12345);
assert.sameValue(instance.toString(), '1976-11-18T15:23:30.450012345');
assert.sameValue(instance.asString(), '1976-11-18T15:23:30.450012345');
assert.sameValue(instance.toString(), '[object CivilDateTime 1976-11-18T15:23:30.450012345]');
3 changes: 2 additions & 1 deletion polyfill/test/temporal/CivilTime/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ assert.sameValue(instance.minute, 23);
assert.sameValue(instance.second, 30);
assert.sameValue(instance.millisecond, 450);
assert.sameValue(instance.nanosecond, 12345);
assert.sameValue(instance.toString(), '15:23:30.450012345');
assert.sameValue(instance.asString(), '15:23:30.450012345');
assert.sameValue(instance.toString(), '[object CivilTime 15:23:30.450012345]');
5 changes: 3 additions & 2 deletions polyfill/test/temporal/Instant/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ esid: pending
---*/

const instant = new temporal.Instant(0n);
assert.sameValue('[object Instant 1970-01-01T00:00:00.000000000Z]', instant.toString());

assert.sameValue('1970-01-01T00:00:00.000000000Z', instant.toString());
assert.sameValue('1970-01-01T00:00:00.000000000Z', instant.asString());

const now = new Date();
const nowi = new temporal.Instant(BigInt(now.valueOf()) * BigInt(1e6));
assert.sameValue(now.toISOString().replace('Z','000000Z'), nowi.toString());
assert.sameValue(now.toISOString().replace('Z','000000Z'), nowi.asString());
3 changes: 2 additions & 1 deletion polyfill/test/temporal/Instant/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ assert.sameValue(instance instanceof temporal.Instant, true);
assert.sameValue(instance.milliseconds, 217178610450);
assert.sameValue(instance.nanoseconds, 100);
assert.sameValue(instance.value, 217178610450000100n);
assert.sameValue(instance.toString(), '1976-11-18T15:23:30.450000100Z');
assert.sameValue(instance.asString(), '1976-11-18T15:23:30.450000100Z');
assert.sameValue(instance.toString(), '[object Instant 1976-11-18T15:23:30.450000100Z]');
6 changes: 4 additions & 2 deletions polyfill/test/temporal/ZonedInstant/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ assert.sameValue(one instanceof temporal.ZonedInstant, true);
assert.sameValue(one.milliseconds, 217175010450);
assert.sameValue(one.nanoseconds, 100);
assert.sameValue(one.value, 217175010450000100n);
assert.sameValue(one.toString(), '1976-11-18T15:23:30.450000100+01:00');
assert.sameValue(one.asString(), '1976-11-18T15:23:30.450000100+01:00');
assert.sameValue(one.toString(), '[object ZonedInstant 1976-11-18T15:23:30.450000100+01:00]');

const two = new temporal.ZonedInstant(instant, 'America/New_York');
assert.sameValue(typeof two, 'object');
assert.sameValue(two instanceof temporal.ZonedInstant, true);
assert.sameValue(two.milliseconds, 217175010450);
assert.sameValue(two.nanoseconds, 100);
assert.sameValue(two.value, 217175010450000100n);
assert.sameValue(two.toString(), '1976-11-18T09:23:30.450000100-05:00');
assert.sameValue(two.asString(), '1976-11-18T09:23:30.450000100-05:00');
assert.sameValue(two.toString(), '[object ZonedInstant 1976-11-18T09:23:30.450000100-05:00]');

0 comments on commit 7c46ffa

Please sign in to comment.