Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dst time jump (#563) #2364

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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
Loading