devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
86 lines (85 loc) • 3.37 kB
JavaScript
/**
* DevExtreme (esm/__internal/scheduler/r1/timezone_calculator/calculator.js)
* Version: 25.2.3
* Build date: Fri Dec 12 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import dateUtils from "../../../../core/utils/date";
import {
isDefined
} from "../../../../core/utils/type";
import {
dateUtilsTs
} from "../../../core/utils/date";
const MS_IN_MINUTE = 6e4;
const MS_IN_HOUR = 36e5;
const toMs = dateUtils.dateToMilliseconds;
export class TimeZoneCalculator {
constructor(options) {
this.options = options
}
createDate(sourceDate, path, appointmentTimeZone) {
const date = new Date(sourceDate);
switch (path) {
case "toAppointment":
return this.getConvertedDate(date, appointmentTimeZone, false);
case "fromAppointment":
return this.getConvertedDate(date, appointmentTimeZone, true);
case "toGrid":
return this.getConvertedDate(date, void 0, false);
case "fromGrid":
return this.getConvertedDate(date, void 0, true);
default:
throw new Error("not specified pathTimeZoneConversion")
}
}
getOffsets(date, appointmentTimezone) {
const clientOffset = -this.getClientOffset(date) / dateUtils.dateToMilliseconds("hour");
const commonOffset = this.getCommonOffset(date);
const appointmentOffset = this.getAppointmentOffset(date, appointmentTimezone);
return {
client: clientOffset,
common: !isDefined(commonOffset) ? clientOffset : commonOffset,
appointment: "number" !== typeof appointmentOffset ? clientOffset : appointmentOffset
}
}
getOriginStartDateOffsetInMs(date, timezone, isUTCDate) {
const offsetInHours = this.getOffsetInHours(date, timezone, isUTCDate);
return 36e5 * offsetInHours
}
getOffsetInHours(date, timezone, isUTCDate) {
const {
client: client,
appointment: appointment,
common: common
} = this.getOffsets(date, timezone);
if (Boolean(timezone) && isUTCDate) {
return appointment - client
}
if (Boolean(timezone) && !isUTCDate) {
return appointment - common
}
if (!timezone && isUTCDate) {
return common - client
}
return 0
}
getClientOffset(date) {
return this.options.getClientOffset(date)
}
getCommonOffset(date) {
return this.options.tryGetCommonOffset(date)
}
getAppointmentOffset(date, appointmentTimezone) {
return this.options.tryGetAppointmentOffset(date, appointmentTimezone)
}
getConvertedDate(date, appointmentTimezone, isBack) {
const newDate = new Date(date.getTime());
const offsets = this.getOffsets(newDate, appointmentTimezone);
const targetOffsetName = appointmentTimezone ? "appointment" : "common";
const direction = isBack ? -1 : 1;
return dateUtilsTs.addOffsets(newDate, direction * toMs("hour") * offsets[targetOffsetName], -direction * toMs("hour") * offsets.client)
}
}