Skip to content

Commit

Permalink
added miliseconds to the dat time converter
Browse files Browse the repository at this point in the history
  • Loading branch information
ppaska committed Sep 15, 2020
1 parent fc81f49 commit a3398a0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "datapipe-js",
"version": "0.3.1",
"version": "0.3.2",
"description": "dataPipe is a JavaScript library for data manipulations, data transformations and data wrangling library inspired by LINQ (C#) and Pandas (Python)",
"main": "dist/cjs/data-pipe.js",
"module": "dist/esm/data-pipe.mjs",
Expand Down
14 changes: 11 additions & 3 deletions src/tests/utils-pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseDatetimeOrNull, parseNumberOrNull, getFieldDescriptions } from "../utils";
import { parseDatetimeOrNull, parseNumberOrNull, getFieldDescriptions, dateToString } from "../utils";
import { FieldDescription, DataTypeName } from "../types";


Expand All @@ -15,7 +15,15 @@ describe('Test dataUtils', () => {
if (date) {
expect(date.getMonth()).toBe(9);
}
})
});

it('parseDateTime with miliseconds', () => {
const dt = parseDatetimeOrNull('2020-06-08T13:49:15.16');
expect(dt).toBeInstanceOf(Date);
expect(dateToString(dt as Date)).toBe('2020-06-08T13:49:15.016Z');
const strDate = '2020-02-21T13:49:15.967Z';
expect(dateToString(parseDatetimeOrNull(strDate) as Date)).toBe(strDate);
});

it('parseNumber', () => {
expect(parseNumberOrNull('')).toBe(null);
Expand Down Expand Up @@ -69,7 +77,7 @@ describe('Test dataUtils', () => {
expect(fdFn(['2019-01-01', null, '2019-01-02']).isNullable).toBe(true);
expect(fdFn([new Date(2001, 1, 1), new Date()]).dataTypeName).toBe(DataTypeName.DateTime);

expect(fdFn(['2019-01-01','NOT A DATE', '2019-01-02']).dataTypeName).toBe(DataTypeName.String);
expect(fdFn(['2019-01-01', 'NOT A DATE', '2019-01-02']).dataTypeName).toBe(DataTypeName.String);
expect(fdFn(['2019-01-01', 76, '2019-01-02']).dataTypeName).toBe(DataTypeName.String);
expect(fdFn(['2019-01-01', 76, false, '2019-01-02']).dataTypeName).toBe(DataTypeName.String);
expect(fdFn(['2019-01-01', 76, false, null, '2019-01-02']).dataTypeName).toBe(DataTypeName.String);
Expand Down
12 changes: 6 additions & 6 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,26 @@ export function parseDatetimeOrNull(value: string | Date): Date | null {
};

const validDateOrNull =
(yyyy: number, month: number, day: number, hours: number, mins: number, ss: number): Date | null => {
(yyyy: number, month: number, day: number, hours: number, mins: number, ss: number, ms: number): Date | null => {
if (month > 11 || day > 31 || hours >= 60 || mins >= 60 || ss >= 60) { return null; }

const dd = new Date(yyyy, month, day, hours, mins, ss, 0);
const dd = new Date(yyyy, month, day, hours, mins, ss, ms);
return !isNaN(dd.valueOf()) ? dd : null;
};

const strTokens = strValue.replace('T', ' ').toLowerCase().split(/[: /-]/);
const strTokens = strValue.replace('T', ' ').replace('.', ' ').toLowerCase().split(/[: /-]/);
const dt = strTokens.map(parseFloat);

// try ISO first
let d = validDateOrNull(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0);
let d = validDateOrNull(dt[0], dt[1] - 1, dt[2], dt[3] || 0, dt[4] || 0, dt[5] || 0, dt[6] || 0);
if (d) { return d; }

// then UK
d = validDateOrNull(correctYear(dt[2]), parseMonth(strTokens[1]), dt[0], dt[3] || 0, dt[4] || 0, dt[5] || 0);
d = validDateOrNull(correctYear(dt[2]), parseMonth(strTokens[1]), dt[0], dt[3] || 0, dt[4] || 0, dt[5] || 0, dt[6] || 0);
if (d) { return d; }

// then US
d = validDateOrNull(correctYear(dt[2]), parseMonth(strTokens[0]), correctYear(dt[1]), dt[3] || 0, dt[4] || 0, dt[5] || 0);
d = validDateOrNull(correctYear(dt[2]), parseMonth(strTokens[0]), correctYear(dt[1]), dt[3] || 0, dt[4] || 0, dt[5] || 0, dt[6] || 0);
if (d) { return d; }

return null;
Expand Down

0 comments on commit a3398a0

Please sign in to comment.