Skip to content

Commit

Permalink
fix dst time jump
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 committed Dec 23, 2024
1 parent c9aff0e commit cfda8ed
Showing 1 changed file with 7 additions and 23 deletions.
30 changes: 7 additions & 23 deletions frontend/taipy-gui/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* specific language governing permissions and limitations under the License.
*/

import { toZonedTime, getTimezoneOffset, formatInTimeZone } from "date-fns-tz";
import { toZonedTime, formatInTimeZone, fromZonedTime } from "date-fns-tz";
import { format } from "date-fns";
import { sprintf } from "sprintf-js";
import { FormatConfig } from "../context/taipyReducers";
Expand Down Expand Up @@ -61,32 +61,16 @@ interface StyleKit {

// return date with right time and tz
export const getTimeZonedDate = (d: Date, tz: string, withTime: boolean): Date => {
const newDate = d;
// dispatch new date which offset by the timeZone differences between client and server
const hours = getClientServerTimeZoneOffset(tz) / 60;
const minutes = getClientServerTimeZoneOffset(tz) % 60;
newDate.setSeconds(0);
newDate.setMilliseconds(0);
if (withTime) {
// Parse data with selected time if it is a datetime selector
newDate.setHours(newDate.getHours() + hours);
newDate.setMinutes(newDate.getMinutes() + minutes);
} else {
// Parse data with 00:00 UTC time if it is a date selector
newDate.setHours(hours);
newDate.setMinutes(minutes);
if (!withTime) {
d.setHours(0, 0, 0, 0);
}
return newDate;
return fromZonedTime(d, tz);
};

export const dateToString = (d: Date, withTime: boolean = true): string => {
return withTime ? d.toISOString() : d.toDateString();
};

// return client server timeZone offset in minutes
export const getClientServerTimeZoneOffset = (tz: string): number =>
(getTimezoneOffset(TIMEZONE_CLIENT) - getTimezoneOffset(tz)) / 60000;

export const getDateTime = (value: string | null | undefined, tz?: string, withTime = true): Date | null => {
if (value === null || value === undefined) {
return null;
Expand Down Expand Up @@ -114,7 +98,7 @@ export const getDateTimeString = (
datetimeformat: string | undefined,
formatConf: FormatConfig,
tz?: string,
withTime: boolean = true
withTime: boolean = true,
): string => {
const dateVal = getDateTime(value);
try {
Expand All @@ -123,7 +107,7 @@ export const getDateTimeString = (
dateVal || "",
formatConf.forceTZ || !tz ? formatConf.timeZone : tz,
datetimeformat || formatConf.dateTime,
{ useAdditionalDayOfYearTokens: true }
{ useAdditionalDayOfYearTokens: true },
);
}
return format(dateVal || 0, datetimeformat || formatConf.date, { useAdditionalDayOfYearTokens: true });
Expand Down Expand Up @@ -170,7 +154,7 @@ export const formatWSValue = (
value: string | number,
dataType: string | undefined,
dataFormat: string | undefined,
formatConf: FormatConfig
formatConf: FormatConfig,
): string => {
dataType = dataType || typeof value;
switch (dataType) {
Expand Down

0 comments on commit cfda8ed

Please sign in to comment.