Skip to content

Commit f0e06d5

Browse files
committed
fix correct extractDate parsing
1 parent 937aef6 commit f0e06d5

File tree

1 file changed

+26
-2
lines changed
  • packages/cubejs-backend-shared/src

1 file changed

+26
-2
lines changed

packages/cubejs-backend-shared/src/time.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,33 @@ export const extractDate = (data: any): string | null => {
266266
data = JSON.parse(JSON.stringify(data));
267267
const value = data[0] && data[0][Object.keys(data[0])[0]];
268268
if (!value) {
269-
return value;
269+
return null;
270+
}
271+
272+
// Most common formats
273+
const formats = [
274+
moment.ISO_8601,
275+
'YYYY-MM-DD HH:mm:ss',
276+
'YYYY-MM-DD HH:mm:ss.SSS',
277+
'YYYY-MM-DDTHH:mm:ss.SSS',
278+
'YYYY-MM-DDTHH:mm:ss'
279+
];
280+
281+
let parsedMoment;
282+
283+
if (value.includes('Z') || /([+-]\d{2}:?\d{2})$/.test(value.trim())) {
284+
// We have timezone info
285+
parsedMoment = moment(value, formats, true);
286+
} else {
287+
// If no tz info - treat as in UTC
288+
parsedMoment = moment.utc(value, formats, true);
270289
}
271-
return moment.tz(value, 'UTC').format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
290+
291+
if (!parsedMoment.isValid()) {
292+
return null;
293+
}
294+
295+
return parsedMoment.utc().format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
272296
};
273297

274298
export const addSecondsToLocalTimestamp = (timestamp: string, timezone: string, seconds: number): Date => {

0 commit comments

Comments
 (0)