Skip to content

Commit 0440afc

Browse files
committed
fix extractDate and rename it to parseLocalDate
1 parent 3199316 commit 0440afc

File tree

1 file changed

+9
-4
lines changed
  • packages/cubejs-backend-shared/src

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export const utcToLocalTimeZone = (timezone: string, timestampFormat: string, ti
265265
return moment.tz(timestamp, 'UTC').tz(timezone).format(timestampFormat);
266266
};
267267

268-
export const extractDate = (data: any): string | null => {
268+
export const parseLocalDate = (data: any, timezone: string, timestampFormat: string = 'YYYY-MM-DDTHH:mm:ss.SSS'): string | null => {
269269
if (!data) {
270270
return null;
271271
}
@@ -275,6 +275,11 @@ export const extractDate = (data: any): string | null => {
275275
return null;
276276
}
277277

278+
const zone = moment.tz.zone(timezone);
279+
if (!zone) {
280+
throw new Error(`Unknown timezone: ${timezone}`);
281+
}
282+
278283
// Most common formats
279284
const formats = [
280285
moment.ISO_8601,
@@ -290,15 +295,15 @@ export const extractDate = (data: any): string | null => {
290295
// We have timezone info
291296
parsedMoment = moment(value, formats, true);
292297
} else {
293-
// If no tz info - treat as in UTC
294-
parsedMoment = moment.utc(value, formats, true);
298+
// If no tz info - use provided timezone
299+
parsedMoment = moment.tz(value, formats, true, timezone);
295300
}
296301

297302
if (!parsedMoment.isValid()) {
298303
return null;
299304
}
300305

301-
return parsedMoment.utc().format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
306+
return parsedMoment.tz(timezone).format(timestampFormat);
302307
};
303308

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

0 commit comments

Comments
 (0)