Skip to content

Commit

Permalink
make loadBuildRange() timezone-aware
Browse files Browse the repository at this point in the history
  • Loading branch information
KSDaemon committed Feb 26, 2025
1 parent 90f6f27 commit 8f1fa77
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
8 changes: 7 additions & 1 deletion packages/cubejs-backend-shared/src/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ export const inDbTimeZone = (timezone: string, timestampFormat: string, timestam
return moment.tz(timestamp, timezone).utc().format(timestampFormat);
};

export const utcToLocalTimeZone = (timezone: string, timestampFormat: string, timestamp: string): string => {
/**
* Takes timestamp in UTC, treat it as local time in provided timezone and returns the corresponding timestamp in UTC
*/
export const utcToLocalTimeZoneInUtc = (timezone: string, timestampFormat: string, timestamp: string): string | null => {
if (!timestamp) {
return null;
}
if (timestamp.length === 23) {
const zone = moment.tz.zone(timezone);
if (!zone) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TO_PARTITION_RANGE,
MAX_SOURCE_ROW_LIMIT,
reformatInIsoLocal,
utcToLocalTimeZone,
utcToLocalTimeZoneInUtc,
timeSeries,
inDbTimeZone,
extractDate
Expand Down Expand Up @@ -136,9 +136,9 @@ export class PreAggregationPartitionRangeLoader {
return queryValues?.map(
param => {
if (param === BUILD_RANGE_START_LOCAL) {
return utcToLocalTimeZone(this.preAggregation.timezone, this.preAggregation.timestampFormat, buildRangeStart);
return utcToLocalTimeZoneInUtc(this.preAggregation.timezone, this.preAggregation.timestampFormat, buildRangeStart);
} else if (param === BUILD_RANGE_END_LOCAL) {
return utcToLocalTimeZone(this.preAggregation.timezone, this.preAggregation.timestampFormat, buildRangeEnd);
return utcToLocalTimeZoneInUtc(this.preAggregation.timezone, this.preAggregation.timestampFormat, buildRangeEnd);
} else {
return param;
}
Expand Down Expand Up @@ -396,31 +396,42 @@ export class PreAggregationPartitionRangeLoader {
const { preAggregationStartEndQueries } = this.preAggregation;
const [startDate, endDate] = await Promise.all(
preAggregationStartEndQueries.map(
async rangeQuery => PreAggregationPartitionRangeLoader.extractDate(await this.loadRangeQuery(rangeQuery)),
async rangeQuery => utcToLocalTimeZoneInUtc(
this.preAggregation.timezone,
'YYYY-MM-DDTHH:mm:ss.SSS',
PreAggregationPartitionRangeLoader.extractDate(await this.loadRangeQuery(rangeQuery)),
)
),
);

if (!this.preAggregation.partitionGranularity) {
return this.orNowIfEmpty([startDate, endDate]);
}

// startDate & endDate are `localized` here
const wholeSeriesRanges = PreAggregationPartitionRangeLoader.timeSeries(
this.preAggregation.partitionGranularity,
this.orNowIfEmpty([startDate, endDate]),
this.preAggregation.timestampPrecision,
);
const [rangeStart, rangeEnd] = await Promise.all(
preAggregationStartEndQueries.map(
async (rangeQuery, i) => PreAggregationPartitionRangeLoader.extractDate(
await this.loadRangeQuery(
rangeQuery, i === 0 ? wholeSeriesRanges[0] : wholeSeriesRanges[wholeSeriesRanges.length - 1],
async (rangeQuery, i) => utcToLocalTimeZoneInUtc(
this.preAggregation.timezone,
'YYYY-MM-DDTHH:mm:ss.SSS',
PreAggregationPartitionRangeLoader.extractDate(
await this.loadRangeQuery(
rangeQuery, i === 0 ? wholeSeriesRanges[0] : wholeSeriesRanges[wholeSeriesRanges.length - 1],
),
),
),
)
),
);
return this.orNowIfEmpty([rangeStart, rangeEnd]);
}

private now() {
return utcToLocalTimeZone(this.preAggregation.timezone, 'YYYY-MM-DDTHH:mm:ss.SSS', new Date().toJSON().substring(0, 23));
return utcToLocalTimeZoneInUtc(this.preAggregation.timezone, 'YYYY-MM-DDTHH:mm:ss.SSS', new Date().toJSON().substring(0, 23));
}

private orNowIfEmpty(dateRange: QueryDateRange): QueryDateRange {
Expand Down

0 comments on commit 8f1fa77

Please sign in to comment.