Skip to content

Commit

Permalink
fix: year zero was not accepted for SpannerDate (#783)
Browse files Browse the repository at this point in the history
* fix: year zero was not accepted for SpannerDate

* fix: fix typo in documentation

* fix: process review comments
  • Loading branch information
olavloite authored Jan 4, 2020
1 parent 7a1f40d commit 0ceb862
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ type DateFields = [number, number, number];
* @extends Date
*
* @param {string|number} [date] String representing the date or number
* representing the year.
* @param {number} [month] Number representing the month.
* representing the year. If year is a number between 0 and 99, then year is
* assumed to be 1900 + year.
* @param {number} [month] Number representing the month (0 = January).
* @param {number} [date] Number representing the date.
*
* @example
Expand All @@ -71,7 +72,8 @@ export class SpannerDate extends Date {
constructor(...dateFields: Array<string | number | undefined>) {
const yearOrDateString = dateFields[0];

if (!yearOrDateString) {
// yearOrDateString could be 0 (number).
if (yearOrDateString == null) {
dateFields[0] = new Date().toDateString();
}

Expand Down
9 changes: 9 additions & 0 deletions test/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ describe('codec', () => {
assert.strictEqual(json, '1986-03-22');
});

it('should accept year zero in y/m/d number values', () => {
const d = new codec.SpannerDate(null!);
const date = new codec.SpannerDate(0, 2, 22);
const json = date.toJSON();

assert.ok(d);
assert.strictEqual(json, '1900-03-22');
});

it('should truncate additional date fields', () => {
const truncated = new codec.SpannerDate(1986, 2, 22, 4, 8, 10);
const expected = new codec.SpannerDate(1986, 2, 22);
Expand Down

0 comments on commit 0ceb862

Please sign in to comment.