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

Send clearAfter as UTC time to the backend #35976

Closed
2 changes: 1 addition & 1 deletion src/components/LHNOptionsList/OptionRowLHN.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function OptionRowLHN({reportID, isFocused = false, onSelectRow = () => {}, opti

const emojiCode = optionItem.status?.emojiCode ?? '';
const statusText = optionItem.status?.text ?? '';
const statusClearAfterDate = optionItem.status?.clearAfter ?? '';
const statusClearAfterDate = DateUtils.formatUTCToLocal(optionItem.status?.clearAfter ?? '');
const formattedDate = DateUtils.getStatusUntilDate(statusClearAfterDate);
const statusContent = formattedDate ? `${statusText ? `${statusText} ` : ''}(${formattedDate})` : statusText;
const report = ReportUtils.getReport(optionItem.reportID ?? '');
Expand Down
25 changes: 25 additions & 0 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,30 @@ function formatWithUTCTimeZone(datetime: string, dateFormat: string = CONST.DATE
return '';
}

/**
* Format the date from UTC to local timezone
* @param dateString
* @param dateFormat
* @returns
*/

function formatUTCToLocal(dateString: string, dateFormat = 'yyyy-MM-dd HH:mm:ss') {
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
if (dateString === '' || !dateString) {
return dateString;
}

const date = parse(dateString, dateFormat, new Date());
// Check if the date is valid
if (!isValid(date)) {
return '';
}

const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be the timezone user selected in the preferences?

Copy link
Contributor Author

@abzokhattab abzokhattab Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree that we need to use timezone.selected however it will not work here because the input date is in the local timezone not in the selected timezone format so i had to use the same timezone as the local machine so that we don't have any mismatch in the date

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

const utcDate = zonedTimeToUtc(date, 'UTC');
const localDate = zonedTimeToUtc(utcDate, localTimezone);
return tzFormat(localDate, dateFormat, {timeZone: localTimezone});
}

/**
*
* @param timezone
Expand Down Expand Up @@ -807,6 +831,7 @@ const DateUtils = {
getMonthNames,
getDaysOfWeek,
formatWithUTCTimeZone,
formatUTCToLocal,
getWeekStartsOn,
getWeekEndsOn,
isTimeAtLeastOneMinuteInFuture,
Expand Down
12 changes: 10 additions & 2 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
ValidateSecondaryLoginParams,
} from '@libs/API/parameters';
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import DateUtils from '@libs/DateUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -891,19 +892,26 @@ function updateTheme(theme: ValueOf<typeof CONST.THEME>) {
* Sets a custom status
*/
function updateCustomStatus(status: Status) {
const clearAfterInUtc = DateUtils.formatWithUTCTimeZone(status.clearAfter, 'yyyy-MM-dd HH:mm:ss');
const newStatus = {
text: status.text,
abzokhattab marked this conversation as resolved.
Show resolved Hide resolved
emojiCode: status.emojiCode,
clearAfter: clearAfterInUtc,
};

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
status,
status: newStatus,
},
},
},
];

const parameters: UpdateStatusParams = {text: status.text, emojiCode: status.emojiCode, clearAfter: status.clearAfter};
const parameters: UpdateStatusParams = {text: status.text, emojiCode: status.emojiCode, clearAfter: clearAfterInUtc};

API.write(WRITE_COMMANDS.UPDATE_STATUS, parameters, {
optimisticData,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/home/report/ReportActionItemSingle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ function ReportActionItemSingle({
);
};
const hasEmojiStatus = !displayAllActors && status?.emojiCode;
const formattedDate = DateUtils.getStatusUntilDate(status?.clearAfter ?? '');
const statusClearAfter = DateUtils.formatUTCToLocal(status?.clearAfter ?? '');
const formattedDate = DateUtils.getStatusUntilDate(statusClearAfter);
const statusText = status?.text ?? '';
const statusTooltipText = formattedDate ? `${statusText ? `${statusText} ` : ''}(${formattedDate})` : statusText;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const useValidateCustomDate = (data) => {
function StatusClearAfterPage({currentUserPersonalDetails, customStatus}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const clearAfter = lodashGet(currentUserPersonalDetails, 'status.clearAfter', '');
const clearAfter = DateUtils.formatUTCToLocal(lodashGet(currentUserPersonalDetails, 'status.clearAfter', ''));
const draftClearAfter = lodashGet(customStatus, 'clearAfter', '');
const [draftPeriod, setDraftPeriod] = useState(getSelectedStatusType(draftClearAfter || clearAfter));
const statusType = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/Profile/CustomStatus/StatusPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function StatusPage({draftStatus, currentUserPersonalDetails}) {
const [brickRoadIndicator, setBrickRoadIndicator] = useState('');
const currentUserEmojiCode = lodashGet(currentUserPersonalDetails, 'status.emojiCode', '');
const currentUserStatusText = lodashGet(currentUserPersonalDetails, 'status.text', '');
const currentUserClearAfter = lodashGet(currentUserPersonalDetails, 'status.clearAfter', '');
const currentUserClearAfter = DateUtils.formatUTCToLocal(lodashGet(currentUserPersonalDetails, 'status.clearAfter', ''));
const draftEmojiCode = lodashGet(draftStatus, 'emojiCode');
const draftText = lodashGet(draftStatus, 'text');
const draftClearAfter = lodashGet(draftStatus, 'clearAfter');
Expand Down
Loading