Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ module.exports = {
},
],
curly: 'error',
'you-dont-need-lodash-underscore/throttle': 'off',
},
},
{
Expand Down
143 changes: 55 additions & 88 deletions src/libs/DateUtils.js → src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import lodashGet from 'lodash/get';
import {zonedTimeToUtc, utcToZonedTime, formatInTimeZone} from 'date-fns-tz';
import {es, enGB} from 'date-fns/locale';
import {
Expand All @@ -18,14 +17,15 @@ import {
isSameYear,
} from 'date-fns';

import _ from 'underscore';
import Onyx from 'react-native-onyx';
import throttle from 'lodash/throttle';
import ONYXKEYS from '../ONYXKEYS';
import CONST from '../CONST';
import * as Localize from './Localize';
import * as CurrentDate from './actions/CurrentDate';
import {Timezone} from '../types/onyx/PersonalDetails';

let currentUserAccountID;
let currentUserAccountID: number | undefined;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: (val) => {
Expand All @@ -38,20 +38,27 @@ Onyx.connect({
},
});

let timezone = CONST.DEFAULT_TIME_ZONE;
let timezone: Required<Timezone> = CONST.DEFAULT_TIME_ZONE;
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => {
timezone = lodashGet(val, [currentUserAccountID, 'timezone'], CONST.DEFAULT_TIME_ZONE);
callback: (value) => {
if (!currentUserAccountID) {
return;
}

const personalDetailsTimezone = value?.[currentUserAccountID]?.timezone;

timezone = {
selected: personalDetailsTimezone?.selected ?? CONST.DEFAULT_TIME_ZONE.selected,
automatic: personalDetailsTimezone?.automatic ?? CONST.DEFAULT_TIME_ZONE.automatic,
};
},
});

/**
* Gets the locale string and setting default locale for date-fns
*
* @param {String} localeString
*/
function setLocale(localeString) {
function setLocale(localeString: string) {
switch (localeString) {
case CONST.LOCALES.EN:
setDefaultOptions({locale: enGB});
Expand All @@ -67,15 +74,8 @@ function setLocale(localeString) {
/**
* Gets the user's stored time zone NVP and returns a localized
* Moment object for the given ISO-formatted datetime string
*
* @private
* @param {String} locale
* @param {String} datetime
* @param {String} [currentSelectedTimezone]
* @returns {Moment}
*
*/
function getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone = timezone.selected) {
function getLocalDateFromDatetime(locale: string, datetime: string, currentSelectedTimezone = timezone.selected): Date {
setLocale(locale);
if (!datetime) {
return utcToZonedTime(new Date(), currentSelectedTimezone);
Expand All @@ -87,11 +87,11 @@ function getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone = ti
/**
* Checks if a given date is today in the specified time zone.
*
* @param {Date} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is today; otherwise, false.
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is today; otherwise, false.
*/
function isToday(date, timeZone) {
function isToday(date: Date, timeZone: string): boolean {
const currentDate = new Date();
const currentDateInTimeZone = utcToZonedTime(currentDate, timeZone);
return isSameDay(date, currentDateInTimeZone);
Expand All @@ -100,11 +100,11 @@ function isToday(date, timeZone) {
/**
* Checks if a given date is tomorrow in the specified time zone.
*
* @param {Date} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is tomorrow; otherwise, false.
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is tomorrow; otherwise, false.
*/
function isTomorrow(date, timeZone) {
function isTomorrow(date: Date, timeZone: string): 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);
Expand All @@ -114,11 +114,11 @@ function isTomorrow(date, timeZone) {
/**
* Checks if a given date is yesterday in the specified time zone.
*
* @param {Date} date - The date to compare.
* @param {String} timeZone - The time zone to consider.
* @returns {Boolean} True if the date is yesterday; otherwise, false.
* @param date - The date to compare.
* @param timeZone - The time zone to consider.
* @returns True if the date is yesterday; otherwise, false.
*/
function isYesterday(date, timeZone) {
function isYesterday(date: Date, timeZone: string): 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);
Expand All @@ -132,15 +132,8 @@ function isYesterday(date, timeZone) {
*
* Jan 20 at 5:30 PM within the past year
* Jan 20, 2019 at 5:30 PM anything over 1 year ago
*
* @param {String} locale
* @param {String} datetime
* @param {Boolean} includeTimeZone
* @param {String} [currentSelectedTimezone]
* @param {Boolean} isLowercase
* @returns {String}
*/
function datetimeToCalendarTime(locale, datetime, includeTimeZone = false, currentSelectedTimezone, isLowercase = false) {
function datetimeToCalendarTime(locale: string, datetime: string, includeTimeZone = false, currentSelectedTimezone = timezone.selected, isLowercase = false): string {
const date = getLocalDateFromDatetime(locale, datetime, currentSelectedTimezone);
const tz = includeTimeZone ? ' [UTC]Z' : '';
let todayAt = Localize.translate(locale, 'common.todayAt');
Expand Down Expand Up @@ -184,12 +177,8 @@ function datetimeToCalendarTime(locale, datetime, includeTimeZone = false, curre
* 3 days ago within the past month
* Jan 20 within the past year
* Jan 20, 2019 anything over 1 year
*
* @param {String} locale
* @param {String} datetime
* @returns {String}
*/
function datetimeToRelative(locale, datetime) {
function datetimeToRelative(locale: string, datetime: string): string {
const date = getLocalDateFromDatetime(locale, datetime);
return formatDistanceToNow(date, {addSuffix: true});
}
Expand All @@ -203,51 +192,50 @@ function datetimeToRelative(locale, datetime) {
* EST
* GMT +07 - For GMT timezone
*
* @param {String} datetime
* @param {String} selectedTimezone
* @returns {String}
* @param datetime
* @param selectedTimezone
* @returns
*/
function getZoneAbbreviation(datetime, selectedTimezone) {
function getZoneAbbreviation(datetime: string, selectedTimezone: string): string {
return formatInTimeZone(datetime, selectedTimezone, 'zzz');
}

/**
* Format date to a long date format with weekday
*
* @param {String} datetime
* @returns {String} Sunday, July 9, 2023
* @returns Sunday, July 9, 2023
*/
function formatToLongDateWithWeekday(datetime) {
function formatToLongDateWithWeekday(datetime: string): string {
return format(new Date(datetime), CONST.DATE.LONG_DATE_FORMAT_WITH_WEEKDAY);
}

/**
* Format date to a weekday format
*
* @param {String} datetime
* @returns {String} Sunday
* @returns Sunday
*/
function formatToDayOfWeek(datetime) {
function formatToDayOfWeek(datetime: string): string {
return format(new Date(datetime), CONST.DATE.WEEKDAY_TIME_FORMAT);
}

/**
* Format date to a local time
*
* @param {String} datetime
* @returns {String} 2:30 PM
* @returns 2:30 PM
*/
function formatToLocalTime(datetime) {
function formatToLocalTime(datetime: string): string {
return format(new Date(datetime), CONST.DATE.LOCAL_TIME_FORMAT);
}

const THREE_HOURS = 1000 * 60 * 60 * 3;

/**
* A throttled version of a function that updates the current date in Onyx store
*/
const updateCurrentDate = _.throttle(() => {
const updateCurrentDate = throttle(() => {
const currentDate = format(new Date(), CONST.DATE.FNS_FORMAT_STRING);
CurrentDate.setCurrentDate(currentDate);
}, 1000 * 60 * 60 * 3); // 3 hours
}, THREE_HOURS);

/**
* Initialises the event listeners that trigger the current date update
Expand All @@ -259,10 +247,7 @@ function startCurrentDateUpdater() {
});
}

/**
* @returns {Object}
*/
function getCurrentTimezone() {
function getCurrentTimezone(): Required<Timezone> {
const currentTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if (timezone.automatic && timezone.selected !== currentTimezone) {
return {...timezone, selected: currentTimezone};
Expand All @@ -273,10 +258,7 @@ function getCurrentTimezone() {
// Used to throttle updates to the timezone when necessary
let lastUpdatedTimezoneTime = new Date();

/**
* @returns {Boolean}
*/
function canUpdateTimezone() {
function canUpdateTimezone(): boolean {
const currentTime = new Date();
const fiveMinutesAgo = subMinutes(currentTime, 5);
// Compare the last updated time with five minutes ago
Expand All @@ -289,40 +271,31 @@ function setTimezoneUpdated() {

/**
* Get the UNIX timestamp in microseconds, with millisecond precision.
*
* @returns {Number}
*/
function getMicroseconds() {
function getMicroseconds(): number {
return Date.now() * CONST.MICROSECONDS_PER_MS;
}

/**
* Returns the current time in milliseconds in the format expected by the database
*
* @param {String|Number} [timestamp]
* @returns {String}
*/
function getDBTime(timestamp = '') {
function getDBTime(timestamp: string | number = ''): string {
const datetime = timestamp ? new Date(timestamp) : new Date();
return datetime.toISOString().replace('T', ' ').replace('Z', '');
}

/**
* @param {String} dateTime
* @param {Number} milliseconds
* @returns {String}
*/
function subtractMillisecondsFromDateTime(dateTime, milliseconds) {
function subtractMillisecondsFromDateTime(dateTime: string, milliseconds: number): string {
const date = zonedTimeToUtc(dateTime, 'UTC');
const newTimestamp = subMilliseconds(date, milliseconds).valueOf();

return getDBTime(newTimestamp);
}

/**
* @param {string} isoTimestamp example: 2023-05-16 05:34:14.388
* @returns {string} example: 2023-05-16
* @param isoTimestamp example: 2023-05-16 05:34:14.388
* @returns example: 2023-05-16
*/
function getDateStringFromISOTimestamp(isoTimestamp) {
function getDateStringFromISOTimestamp(isoTimestamp: string): string {
if (!isoTimestamp) {
return '';
}
Expand All @@ -333,11 +306,8 @@ function getDateStringFromISOTimestamp(isoTimestamp) {

/**
* receive date like 2020-05-16 05:34:14 and format it to show in string like "Until 05:34 PM"
*
* @param {String} inputDate
* @returns {String}
*/
function getStatusUntilDate(inputDate) {
function getStatusUntilDate(inputDate: string): string {
if (!inputDate) {
return '';
}
Expand Down Expand Up @@ -366,9 +336,6 @@ function getStatusUntilDate(inputDate) {
return translateLocal('statusPage.untilTime', {time: format(input, `${CONST.DATE.FNS_FORMAT_STRING} ${CONST.DATE.LOCAL_TIME_FORMAT}`)});
}

/**
* @namespace DateUtils
*/
const DateUtils = {
formatToDayOfWeek,
formatToLongDateWithWeekday,
Expand Down
18 changes: 11 additions & 7 deletions src/types/onyx/PersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
type Timezone = {
/** Value of selected timezone */
selected?: string;

/** Whether timezone is automatically set */
automatic?: boolean;
};

type PersonalDetails = {
/** ID of the current user from their personal details */
accountID: number;
Expand Down Expand Up @@ -33,13 +41,9 @@ type PersonalDetails = {
localCurrencyCode?: string;

/** Timezone of the current user from their personal details */
timezone?: {
/** Value of selected timezone */
selected?: string;

/** Whether timezone is automatically set */
automatic?: boolean;
};
timezone?: Timezone;
};

export type {Timezone};

export default PersonalDetails;