Skip to content

Commit

Permalink
fix(source): Fix #66: Zulu timestamps fails with Error: Invalid decim…
Browse files Browse the repository at this point in the history
…al 00.000Z.

This is a fix for #66.

BREAKING CHANGE: None.

66
  • Loading branch information
kadishmal committed Jul 6, 2017
1 parent 0201ebc commit bf2be70
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/IonTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
// undefined, empty or a null image it returns the timestamp NULL.

import { Decimal } from "./IonDecimal";
import { is_digit } from "./IonText";
import { isNumber } from "./IonUtilities";
import { isString } from "./IonUtilities";
import { isUndefined } from "./IonUtilities";
Expand Down Expand Up @@ -178,7 +177,7 @@ function to_4_digits(v: number) : string {
function read_unknown_digits(str: string, pos: number) : string {
let i: number = pos;
for (; i < str.length; i++) {
if (!isNumber(str.charCodeAt(i))) {
if (!isNumber(parseInt(str[i], 10))) {
break;
}
}
Expand Down Expand Up @@ -501,7 +500,10 @@ export class Timestamp {
break;
// 1234-67-89T12:45:78.dddd
case States.FRACTIONAL_SECONDS:
seconds = Decimal.parse(str.substr(17, pos - 17));
const START_POSITION_OF_SECONDS = 17;

seconds = Decimal.parse(str.substring(START_POSITION_OF_SECONDS, pos));

break;
case States.OFFSET:
break;
Expand Down
2 changes: 1 addition & 1 deletion src/IonUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* A collection of general language-level helper methods.
*/
export function isNumber(value: any) : value is number {
return typeof(value) == 'number';
return typeof(value) == 'number' && !isNaN(value);
}

export function isString(value: any) : value is string {
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/IonTimestampTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
const assert = require('intern/chai!assert');
const ion = require('dist/amd/es6/Ion');

var suite = {
let suite = {
name: 'Timestamp'
};

var parseTest = function(name, timestamp) {
const parseTest = function(name, timestamp) {
suite[name] = function() {
ion.Timestamp.parse(timestamp);
}
}
};

parseTest('Parses year', '2017T');
parseTest('Parses month', '2017-02T');
// Seconds are optional, but local offset is not.
parseTest('Parses date and time with only hour and minutes', '2007-02-23T12:14Z');
parseTest('Parses timestamp: The same instant in UTC ("zero" or "zulu")', '2017-05-01T01:00:00.000Z');

registerSuite(suite);
}
Expand Down

0 comments on commit bf2be70

Please sign in to comment.