Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions packages/cubejs-backend-shared/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1055,33 +1055,29 @@ const variables: Record<string, (...args: any) => any> = {
* @see https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_convert-tz
* @see https://dev.mysql.com/doc/refman/8.4/en/time-zone-support.html
*/
mysqlUseNamedTimezones: ({ dataSource }: { dataSource: string }) => {
const val = process.env[
keyByDataSource(
'CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES',
dataSource,
)
];
mysqlUseNamedTimezones: ({ dataSource }: { dataSource: string }) => (
get(keyByDataSource('CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES', dataSource))
// It's true in schema-compiler integration tests
.default('false')
.asBool()
),

if (val) {
if (val.toLocaleLowerCase() === 'true') {
return true;
} else if (val.toLowerCase() === 'false') {
return false;
} else {
throw new TypeError(
`The ${
keyByDataSource(
'CUBEJS_DB_MYSQL_USE_NAMED_TIMEZONES',
dataSource,
)
} must be either 'true' or 'false'.`
);
}
} else {
return false;
}
},
/** ****************************************************************
* MSSQL Driver *
***************************************************************** */

/**
* Use named timezones for date/time conversions via AT TIME ZONE.
* Defaults to FALSE, meaning that numeric offsets for timezone will be used.
*
* @see https://learn.microsoft.com/en-us/sql/t-sql/queries/at-time-zone-transact-sql
*/
mssqlUseNamedTimezones: ({ dataSource }: { dataSource: string }) => (
get(keyByDataSource('CUBEJS_DB_MSSQL_USE_NAMED_TIMEZONES', dataSource))
// It's true in schema-compiler integration tests
.default('false')
.asBool()
),

/** ****************************************************************
* Databricks Driver *
Expand Down
16 changes: 15 additions & 1 deletion packages/cubejs-schema-compiler/src/adapter/MssqlQuery.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import R from 'ramda';
import moment from 'moment-timezone';

import { QueryAlias, parseSqlInterval } from '@cubejs-backend/shared';
import { getEnv, QueryAlias, parseSqlInterval } from '@cubejs-backend/shared';
import { BaseQuery } from './BaseQuery';
import { BaseFilter } from './BaseFilter';
import { BaseSegment } from './BaseSegment';
import { ParamAllocator } from './ParamAllocator';
import { resolveWindowsTimezone } from './windows-iana';

const abbrs = {
EST: 'Eastern Standard Time',
Expand Down Expand Up @@ -73,6 +74,14 @@ class MssqlSegment extends BaseSegment {
}

export class MssqlQuery extends BaseQuery {
private readonly useNamedTimezones: boolean;

public constructor(compilers: any, options: any) {
super(compilers, options);

this.useNamedTimezones = getEnv('mssqlUseNamedTimezones', { dataSource: this.dataSource });
}

public newFilter(filter) {
return new MssqlFilter(this, filter);
}
Expand All @@ -90,6 +99,11 @@ export class MssqlQuery extends BaseQuery {
}

public convertTz(field) {
if (this.useNamedTimezones) {
const windowsTz = resolveWindowsTimezone(this.timezone);
return `CAST(${field} AT TIME ZONE 'UTC' AT TIME ZONE '${windowsTz}' AS DATETIME2)`;
}

const offset = moment().tz(this.timezone).format('Z');

// 1. Treating the field as UTC (add '+00:00' offset)
Expand Down
1 change: 1 addition & 0 deletions packages/cubejs-schema-compiler/src/adapter/MysqlQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class MysqlQuery extends BaseQuery {
if (this.useNamedTimezones) {
return `CONVERT_TZ(${field}, @@session.time_zone, '${this.timezone}')`;
}

return `CONVERT_TZ(${field}, @@session.time_zone, '${moment().tz(this.timezone).format('Z')}')`;
}

Expand Down
Loading
Loading