Skip to content

Commit

Permalink
fix: adjust imports
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Oct 2, 2024
1 parent 9d35fd7 commit a29046d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/CONST.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import dateAdd from 'date-fns/add';
import dateSubtract from 'date-fns/sub';
import {add as dateAdd} from 'date-fns';
import {sub as dateSubtract} from 'date-fns/sub';
import Config from 'react-native-config';
import * as KeyCommand from 'react-native-key-command';
import type {ValueOf} from 'type-fest';
Expand Down
24 changes: 12 additions & 12 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import {
subMilliseconds,
subMinutes,
} from 'date-fns';
import {formatInTimeZone, format as tzFormat, utcToZonedTime, zonedTimeToUtc} from 'date-fns-tz';
import enGB from 'date-fns/locale/en-GB';
import es from 'date-fns/locale/es';
import {formatInTimeZone, fromZonedTime, toZonedTime, format as tzFormat} from 'date-fns-tz';
import {enGB} from 'date-fns/locale/en-GB';
import {es} from 'date-fns/locale/es';
import throttle from 'lodash/throttle';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
Expand Down Expand Up @@ -128,9 +128,9 @@ function setLocale(localeString: Locale) {
function getLocalDateFromDatetime(locale: Locale, datetime?: string, currentSelectedTimezone: SelectedTimezone = timezone.selected): Date {
setLocale(locale);
if (!datetime) {
const res = utcToZonedTime(new Date(), currentSelectedTimezone);
const res = toZonedTime(new Date(), currentSelectedTimezone);
if (Number.isNaN(res.getTime())) {
Log.warn('DateUtils.getLocalDateFromDatetime: utcToZonedTime returned an invalid date. Returning current date.', {
Log.warn('DateUtils.getLocalDateFromDatetime: toZonedTime returned an invalid date. Returning current date.', {
locale,
datetime,
currentSelectedTimezone,
Expand All @@ -148,7 +148,7 @@ function getLocalDateFromDatetime(locale: Locale, datetime?: string, currentSele
parsedDatetime = new Date(datetime);
}

return utcToZonedTime(parsedDatetime, currentSelectedTimezone);
return toZonedTime(parsedDatetime, currentSelectedTimezone);
}

/**
Expand All @@ -160,7 +160,7 @@ function getLocalDateFromDatetime(locale: Locale, datetime?: string, currentSele
*/
function isToday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const currentDateInTimeZone = utcToZonedTime(currentDate, timeZone);
const currentDateInTimeZone = toZonedTime(currentDate, timeZone);
return isSameDay(date, currentDateInTimeZone);
}

Expand All @@ -174,7 +174,7 @@ function isToday(date: Date, timeZone: SelectedTimezone): boolean {
function isTomorrow(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const tomorrow = addDays(currentDate, 1); // Get the date for tomorrow in the current time zone
const tomorrowInTimeZone = utcToZonedTime(tomorrow, timeZone);
const tomorrowInTimeZone = toZonedTime(tomorrow, timeZone);
return isSameDay(date, tomorrowInTimeZone);
}

Expand All @@ -188,7 +188,7 @@ function isTomorrow(date: Date, timeZone: SelectedTimezone): boolean {
function isYesterday(date: Date, timeZone: SelectedTimezone): boolean {
const currentDate = new Date();
const yesterday = subDays(currentDate, 1); // Get the date for yesterday in the current time zone
const yesterdayInTimeZone = utcToZonedTime(yesterday, timeZone);
const yesterdayInTimeZone = toZonedTime(yesterday, timeZone);
return isSameDay(date, yesterdayInTimeZone);
}

Expand Down Expand Up @@ -401,14 +401,14 @@ function getDBTimeWithSkew(timestamp: string | number = ''): string {
}

function subtractMillisecondsFromDateTime(dateTime: string, milliseconds: number): string {
const date = zonedTimeToUtc(dateTime, 'UTC');
const date = fromZonedTime(dateTime, 'UTC');
const newTimestamp = subMilliseconds(date, milliseconds).valueOf();

return getDBTime(newTimestamp);
}

function addMillisecondsFromDateTime(dateTime: string, milliseconds: number): string {
const date = zonedTimeToUtc(dateTime, 'UTC');
const date = fromZonedTime(dateTime, 'UTC');
const newTimestamp = addMilliseconds(date, milliseconds).valueOf();

return getDBTime(newTimestamp);
Expand Down Expand Up @@ -699,7 +699,7 @@ function formatWithUTCTimeZone(datetime: string, dateFormat: string = CONST.DATE
const date = new Date(datetime);

if (isValid(date)) {
return tzFormat(utcToZonedTime(date, 'UTC'), dateFormat);
return tzFormat(toZonedTime(date, 'UTC'), dateFormat);
}

return '';
Expand Down
3 changes: 1 addition & 2 deletions src/libs/DebugUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable max-classes-per-file */
import {isMatch} from 'date-fns';
import isValid from 'date-fns/isValid';
import {isMatch, isValid} from 'date-fns';
import type {OnyxEntry} from 'react-native-onyx';
import CONST from '@src/CONST';
import type {Report, ReportAction} from '@src/types/onyx';
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {findFocusedRoute} from '@react-navigation/native';
import {format as timezoneFormat, utcToZonedTime} from 'date-fns-tz';
import {format as timezoneFormat, toZonedTime} from 'date-fns-tz';
import {Str} from 'expensify-common';
import isEmpty from 'lodash/isEmpty';
import {DeviceEventEmitter, InteractionManager, Linking} from 'react-native';
Expand Down Expand Up @@ -2513,7 +2513,7 @@ function clearIOUError(reportID: string) {
* Uses the NEW FORMAT for "emojiReactions"
*/
function addEmojiReaction(reportID: string, reportActionID: string, emoji: Emoji, skinTone: string | number = preferredSkinTone) {
const createdAt = timezoneFormat(utcToZonedTime(new Date(), 'UTC'), CONST.DATE.FNS_DB_FORMAT_STRING);
const createdAt = timezoneFormat(toZonedTime(new Date(), 'UTC'), CONST.DATE.FNS_DB_FORMAT_STRING);
const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
10 changes: 5 additions & 5 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {afterEach, beforeAll, beforeEach, describe, expect, it} from '@jest/globals';
import {utcToZonedTime} from 'date-fns-tz';
import {toZonedTime} from 'date-fns-tz';
import Onyx from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('actions/Report', () => {
.then(() => {
// The report will be read
expect(ReportUtils.isUnread(report)).toBe(false);
expect(utcToZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(utcToZonedTime(currentTime, UTC).getTime());
expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime());

// And no longer show the green dot for unread mentions in the LHN
expect(ReportUtils.isUnreadWithMention(report)).toBe(false);
Expand All @@ -316,7 +316,7 @@ describe('actions/Report', () => {
// The report will be read, the green dot for unread mentions will go away, and the lastReadTime updated
expect(ReportUtils.isUnread(report)).toBe(false);
expect(ReportUtils.isUnreadWithMention(report)).toBe(false);
expect(utcToZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(utcToZonedTime(currentTime, UTC).getTime());
expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime());
expect(report?.lastMessageText).toBe('Current User Comment 1');

// When another comment is added by the current user
Expand All @@ -328,7 +328,7 @@ describe('actions/Report', () => {
.then(() => {
// The report will be read and the lastReadTime updated
expect(ReportUtils.isUnread(report)).toBe(false);
expect(utcToZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(utcToZonedTime(currentTime, UTC).getTime());
expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime());
expect(report?.lastMessageText).toBe('Current User Comment 2');

// When another comment is added by the current user
Expand All @@ -340,7 +340,7 @@ describe('actions/Report', () => {
.then(() => {
// The report will be read and the lastReadTime updated
expect(ReportUtils.isUnread(report)).toBe(false);
expect(utcToZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(utcToZonedTime(currentTime, UTC).getTime());
expect(toZonedTime(report?.lastReadTime ?? '', UTC).getTime()).toBeGreaterThanOrEqual(toZonedTime(currentTime, UTC).getTime());
expect(report?.lastMessageText).toBe('Current User Comment 3');

const USER_1_BASE_ACTION = {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/UnreadIndicatorsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as NativeNavigation from '@react-navigation/native';
import {act, fireEvent, render, screen, waitFor} from '@testing-library/react-native';
import {addSeconds, format, subMinutes, subSeconds} from 'date-fns';
import {utcToZonedTime} from 'date-fns-tz';
import {toZonedTime} from 'date-fns-tz';
import React from 'react';
import {AppState, DeviceEventEmitter} from 'react-native';
import type {TextStyle, ViewStyle} from 'react-native';
Expand Down Expand Up @@ -301,7 +301,7 @@ describe('Unread Indicators', () => {
reportID: NEW_REPORT_ID,
reportName: CONST.REPORT.DEFAULT_REPORT_NAME,
lastReadTime: '',
lastVisibleActionCreated: DateUtils.getDBTime(utcToZonedTime(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, 'UTC').valueOf()),
lastVisibleActionCreated: DateUtils.getDBTime(toZonedTime(NEW_REPORT_FIST_MESSAGE_CREATED_DATE, 'UTC').valueOf()),
lastMessageText: 'Comment 1',
lastActorAccountID: USER_C_ACCOUNT_ID,
participants: {
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {addDays, addMinutes, format, setHours, setMinutes, subDays, subHours, subMinutes, subSeconds} from 'date-fns';
import {format as tzFormat, utcToZonedTime} from 'date-fns-tz';
import {toZonedTime, format as tzFormat} from 'date-fns-tz';
import Onyx from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -162,9 +162,9 @@ describe('DateUtils', () => {
const tomorrow = addDays(today, 1);
const yesterday = subDays(today, 1);

const todayInTimezone = utcToZonedTime(today, timezone);
const tomorrowInTimezone = utcToZonedTime(tomorrow, timezone);
const yesterdayInTimezone = utcToZonedTime(yesterday, timezone);
const todayInTimezone = toZonedTime(today, timezone);
const tomorrowInTimezone = toZonedTime(tomorrow, timezone);
const yesterdayInTimezone = toZonedTime(yesterday, timezone);

it('isToday should correctly identify today', () => {
expect(DateUtils.isToday(todayInTimezone, timezone)).toBe(true);
Expand Down

0 comments on commit a29046d

Please sign in to comment.