From ce3d2ec761e8884cd8410da0595aa09b392da62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20V=C3=A9lez?= Date: Mon, 18 Nov 2024 11:25:32 +0100 Subject: [PATCH] MM-61782 - fix invalid time when changing to today after 9 am (#29287) * MM-61782 - fix invalid time when changing to today after 9 am * reset time interval to correct value * Updated time reset logic --------- Co-authored-by: Harshil Sharma Co-authored-by: Mattermost Build --- .../src/components/custom_status/date_time_input.tsx | 9 +++++---- webapp/channels/src/utils/timezone.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/webapp/channels/src/components/custom_status/date_time_input.tsx b/webapp/channels/src/components/custom_status/date_time_input.tsx index 7467cc516e..12ea875d2b 100644 --- a/webapp/channels/src/components/custom_status/date_time_input.tsx +++ b/webapp/channels/src/components/custom_status/date_time_input.tsx @@ -27,7 +27,7 @@ import type {A11yFocusEventDetail} from 'utils/constants'; import Constants, {A11yCustomEventTypes} from 'utils/constants'; import {relativeFormatDate} from 'utils/datetime'; import {isKeyPressed} from 'utils/keyboard'; -import {getCurrentMomentForTimezone} from 'utils/timezone'; +import {getCurrentMomentForTimezone, isBeforeTime} from 'utils/timezone'; const CUSTOM_STATUS_TIME_PICKER_INTERVALS_IN_MINUTES = 30; @@ -117,9 +117,10 @@ const DateTimeInputContainer: React.FC = ({ const handleDayChange = (day: Date, modifiers: DayModifiers) => { if (modifiers.today) { const baseTime = getCurrentMomentForTimezone(timezone); - baseTime.hour(time.hours()); - baseTime.minute(time.minutes()); - + if (isBeforeTime(baseTime, time)) { + baseTime.hour(time.hours()); + baseTime.minute(time.minutes()); + } const roundedTime = getRoundedTime(baseTime, timePickerInterval); handleChange(roundedTime); } else { diff --git a/webapp/channels/src/utils/timezone.ts b/webapp/channels/src/utils/timezone.ts index 49066c5247..17559fd242 100644 --- a/webapp/channels/src/utils/timezone.ts +++ b/webapp/channels/src/utils/timezone.ts @@ -1,6 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import type {Moment} from 'moment-timezone'; import moment from 'moment-timezone'; export function getBrowserTimezone() { @@ -28,3 +29,10 @@ export function getCurrentDateTimeForTimezone(timezone: string) { export function getCurrentMomentForTimezone(timezone?: string) { return timezone ? moment.tz(timezone) : moment(); } + +export function isBeforeTime(dateTime1: Moment, dateTime2: Moment) { + const a = dateTime1.clone().set({year: 0, month: 0, date: 1}); + const b = dateTime2.clone().set({year: 0, month: 0, date: 1}); + + return a.isBefore(b); +}