migrate consts/datetime.js to ts

This commit is contained in:
MaysWind
2025-01-02 00:31:28 +08:00
parent 5525635df1
commit ad9a390b58
22 changed files with 633 additions and 665 deletions
+54 -57
View File
@@ -1,6 +1,6 @@
import moment from 'moment';
import dateTimeConstants from '@/consts/datetime.js';
import { Month, WeekDay, MeridiemIndicator, DateRangeScene, DateRange, LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE } from '@/core/datetime.ts';
import { isObject, isString, isNumber } from './common.ts';
export function isYearMonthValid(year, month) {
@@ -198,16 +198,16 @@ export function getDay(date) {
export function getDayOfWeekName(date) {
const dayOfWeek = moment(date).days();
return dateTimeConstants.allWeekDaysArray[dayOfWeek].name;
return WeekDay.valueOf(dayOfWeek).name;
}
export function getMonthName(date) {
const dayOfWeek = moment(date).month();
return dateTimeConstants.allMonthsArray[dayOfWeek];
const month = moment(date).month();
return Month.valueOf(month + 1).name;
}
export function getAMOrPM(hour) {
return isPM(hour) ? dateTimeConstants.allMeridiemIndicators.PM : dateTimeConstants.allMeridiemIndicators.AM;
return isPM(hour) ? MeridiemIndicator.PM.name : MeridiemIndicator.AM.name;
}
export function getUnixTimeBeforeUnixTime(unixTime, amount, unit) {
@@ -429,9 +429,9 @@ export function getAllMonthsStartAndEndUnixTimes(startYearMonth, endYearMonth) {
}
export function getDateTimeFormatType(allFormatMap, allFormatArray, localeDefaultFormatTypeName, systemDefaultFormatType, formatTypeValue) {
if (formatTypeValue > dateTimeConstants.defaultDateTimeFormatValue && allFormatArray[formatTypeValue - 1] && allFormatArray[formatTypeValue - 1].key) {
if (formatTypeValue > LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE && allFormatArray[formatTypeValue - 1] && allFormatArray[formatTypeValue - 1].key) {
return allFormatArray[formatTypeValue - 1];
} else if (formatTypeValue === dateTimeConstants.defaultDateTimeFormatValue && allFormatMap[localeDefaultFormatTypeName] && allFormatMap[localeDefaultFormatTypeName].key) {
} else if (formatTypeValue === LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE && allFormatMap[localeDefaultFormatTypeName] && allFormatMap[localeDefaultFormatTypeName].key) {
return allFormatMap[localeDefaultFormatTypeName];
} else {
return systemDefaultFormatType;
@@ -489,12 +489,12 @@ export function getShiftedDateRangeAndDateType(minTime, maxTime, scale, firstDay
}
export function getShiftedDateRangeAndDateTypeForBillingCycle(minTime, maxTime, scale, firstDayOfWeek, scene, statementDate) {
if (!statementDate || !dateTimeConstants.allDateRanges.PreviousBillingCycle.availableScenes[scene] || !dateTimeConstants.allDateRanges.CurrentBillingCycle.availableScenes[scene]) {
if (!statementDate || !DateRange.PreviousBillingCycle.isAvailableForScene(scene) || !DateRange.CurrentBillingCycle.isAvailableForScene(scene)) {
return null;
}
const previousBillingCycleRange = getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.PreviousBillingCycle.type, firstDayOfWeek, statementDate);
const currentBillingCycleRange = getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.CurrentBillingCycle.type, firstDayOfWeek, statementDate);
const previousBillingCycleRange = getDateRangeByBillingCycleDateType(DateRange.PreviousBillingCycle.type, firstDayOfWeek, statementDate);
const currentBillingCycleRange = getDateRangeByBillingCycleDateType(DateRange.CurrentBillingCycle.type, firstDayOfWeek, statementDate);
if (previousBillingCycleRange && getUnixTimeBeforeUnixTime(previousBillingCycleRange.maxTime, 1, 'months') === maxTime && getUnixTimeBeforeUnixTime(previousBillingCycleRange.minTime, 1, 'months') === minTime && scale === 1) {
return previousBillingCycleRange;
@@ -510,23 +510,20 @@ export function getShiftedDateRangeAndDateTypeForBillingCycle(minTime, maxTime,
}
export function getDateTypeByDateRange(minTime, maxTime, firstDayOfWeek, scene) {
let newDateType = dateTimeConstants.allDateRanges.Custom.type;
const allDateRanges = DateRange.values();
let newDateType = DateRange.Custom.type;
for (let dateRangeField in dateTimeConstants.allDateRanges) {
if (!Object.prototype.hasOwnProperty.call(dateTimeConstants.allDateRanges, dateRangeField)) {
for (let i = 0; i < allDateRanges.length; i++) {
const dateRange = allDateRanges[i];
if (!dateRange.isAvailableForScene(scene)) {
continue;
}
const dateRangeType = dateTimeConstants.allDateRanges[dateRangeField];
const range = getDateRangeByDateType(dateRange.type, firstDayOfWeek);
if (!dateRangeType.availableScenes[scene]) {
continue;
}
const dateRange = getDateRangeByDateType(dateRangeType.type, firstDayOfWeek);
if (dateRange && dateRange.minTime === minTime && dateRange.maxTime === maxTime) {
newDateType = dateRangeType.type;
if (range && range.minTime === minTime && range.maxTime === maxTime) {
newDateType = dateRange.type;
break;
}
}
@@ -535,12 +532,12 @@ export function getDateTypeByDateRange(minTime, maxTime, firstDayOfWeek, scene)
}
export function getDateTypeByBillingCycleDateRange(minTime, maxTime, firstDayOfWeek, scene, statementDate) {
if (!statementDate || !dateTimeConstants.allDateRanges.PreviousBillingCycle.availableScenes[scene] || !dateTimeConstants.allDateRanges.CurrentBillingCycle.availableScenes[scene]) {
if (!statementDate || !DateRange.PreviousBillingCycle.isAvailableForScene(scene) || !DateRange.CurrentBillingCycle.isAvailableForScene(scene)) {
return null;
}
const previousBillingCycleRange = getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.PreviousBillingCycle.type, firstDayOfWeek, statementDate);
const currentBillingCycleRange = getDateRangeByBillingCycleDateType(dateTimeConstants.allDateRanges.CurrentBillingCycle.type, firstDayOfWeek, statementDate);
const previousBillingCycleRange = getDateRangeByBillingCycleDateType(DateRange.PreviousBillingCycle.type, firstDayOfWeek, statementDate);
const currentBillingCycleRange = getDateRangeByBillingCycleDateType(DateRange.CurrentBillingCycle.type, firstDayOfWeek, statementDate);
if (previousBillingCycleRange && previousBillingCycleRange.maxTime === maxTime && previousBillingCycleRange.minTime === minTime) {
return previousBillingCycleRange.dateType;
@@ -555,55 +552,55 @@ export function getDateRangeByDateType(dateType, firstDayOfWeek) {
let maxTime = 0;
let minTime = 0;
if (dateType === dateTimeConstants.allDateRanges.All.type) { // All
if (dateType === DateRange.All.type) { // All
maxTime = 0;
minTime = 0;
} else if (dateType === dateTimeConstants.allDateRanges.Today.type) { // Today
} else if (dateType === DateRange.Today.type) { // Today
maxTime = getTodayLastUnixTime();
minTime = getTodayFirstUnixTime();
} else if (dateType === dateTimeConstants.allDateRanges.Yesterday.type) { // Yesterday
} else if (dateType === DateRange.Yesterday.type) { // Yesterday
maxTime = getUnixTimeBeforeUnixTime(getTodayLastUnixTime(), 1, 'days');
minTime = getUnixTimeBeforeUnixTime(getTodayFirstUnixTime(), 1, 'days');
} else if (dateType === dateTimeConstants.allDateRanges.LastSevenDays.type) { // Last 7 days
} else if (dateType === DateRange.LastSevenDays.type) { // Last 7 days
maxTime = getTodayLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getTodayFirstUnixTime(), 6, 'days');
} else if (dateType === dateTimeConstants.allDateRanges.LastThirtyDays.type) { // Last 30 days
} else if (dateType === DateRange.LastThirtyDays.type) { // Last 30 days
maxTime = getTodayLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getTodayFirstUnixTime(), 29, 'days');
} else if (dateType === dateTimeConstants.allDateRanges.ThisWeek.type) { // This week
} else if (dateType === DateRange.ThisWeek.type) { // This week
maxTime = getThisWeekLastUnixTime(firstDayOfWeek);
minTime = getThisWeekFirstUnixTime(firstDayOfWeek);
} else if (dateType === dateTimeConstants.allDateRanges.LastWeek.type) { // Last week
} else if (dateType === DateRange.LastWeek.type) { // Last week
maxTime = getUnixTimeBeforeUnixTime(getThisWeekLastUnixTime(firstDayOfWeek), 7, 'days');
minTime = getUnixTimeBeforeUnixTime(getThisWeekFirstUnixTime(firstDayOfWeek), 7, 'days');
} else if (dateType === dateTimeConstants.allDateRanges.ThisMonth.type) { // This month
} else if (dateType === DateRange.ThisMonth.type) { // This month
maxTime = getThisMonthLastUnixTime();
minTime = getThisMonthFirstUnixTime();
} else if (dateType === dateTimeConstants.allDateRanges.LastMonth.type) { // Last month
} else if (dateType === DateRange.LastMonth.type) { // Last month
maxTime = getUnixTimeBeforeUnixTime(getThisMonthFirstUnixTime(), 1, 'seconds');
minTime = getUnixTimeBeforeUnixTime(getThisMonthFirstUnixTime(), 1, 'months');
} else if (dateType === dateTimeConstants.allDateRanges.ThisYear.type) { // This year
} else if (dateType === DateRange.ThisYear.type) { // This year
maxTime = getThisYearLastUnixTime();
minTime = getThisYearFirstUnixTime();
} else if (dateType === dateTimeConstants.allDateRanges.LastYear.type) { // Last year
} else if (dateType === DateRange.LastYear.type) { // Last year
maxTime = getUnixTimeBeforeUnixTime(getThisYearLastUnixTime(), 1, 'years');
minTime = getUnixTimeBeforeUnixTime(getThisYearFirstUnixTime(), 1, 'years');
} else if (dateType === dateTimeConstants.allDateRanges.RecentTwelveMonths.type) { // Recent 12 months
} else if (dateType === DateRange.RecentTwelveMonths.type) { // Recent 12 months
maxTime = getThisMonthLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisMonthFirstUnixTime(), 11, 'months');
} else if (dateType === dateTimeConstants.allDateRanges.RecentTwentyFourMonths.type) { // Recent 24 months
} else if (dateType === DateRange.RecentTwentyFourMonths.type) { // Recent 24 months
maxTime = getThisMonthLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisMonthFirstUnixTime(), 23, 'months');
} else if (dateType === dateTimeConstants.allDateRanges.RecentThirtySixMonths.type) { // Recent 36 months
} else if (dateType === DateRange.RecentThirtySixMonths.type) { // Recent 36 months
maxTime = getThisMonthLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisMonthFirstUnixTime(), 35, 'months');
} else if (dateType === dateTimeConstants.allDateRanges.RecentTwoYears.type) { // Recent 2 years
} else if (dateType === DateRange.RecentTwoYears.type) { // Recent 2 years
maxTime = getThisYearLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisYearFirstUnixTime(), 1, 'years');
} else if (dateType === dateTimeConstants.allDateRanges.RecentThreeYears.type) { // Recent 3 years
} else if (dateType === DateRange.RecentThreeYears.type) { // Recent 3 years
maxTime = getThisYearLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisYearFirstUnixTime(), 2, 'years');
} else if (dateType === dateTimeConstants.allDateRanges.RecentFiveYears.type) { // Recent 5 years
} else if (dateType === DateRange.RecentFiveYears.type) { // Recent 5 years
maxTime = getThisYearLastUnixTime();
minTime = getUnixTimeBeforeUnixTime(getThisYearFirstUnixTime(), 4, 'years');
} else {
@@ -621,7 +618,7 @@ export function getDateRangeByBillingCycleDateType(dateType, firstDayOfWeek, sta
let maxTime = 0;
let minTime = 0;
if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type || dateType === dateTimeConstants.allDateRanges.CurrentBillingCycle.type) { // Previous Billing Cycle | Current Billing Cycle
if (dateType === DateRange.PreviousBillingCycle.type || dateType === DateRange.CurrentBillingCycle.type) { // Previous Billing Cycle | Current Billing Cycle
if (statementDate) {
if (getCurrentDay() <= statementDate) {
maxTime = getThisMonthSpecifiedDayLastUnixTime(statementDate);
@@ -631,17 +628,17 @@ export function getDateRangeByBillingCycleDateType(dateType, firstDayOfWeek, sta
minTime = getUnixTimeAfterUnixTime(getThisMonthSpecifiedDayFirstUnixTime(statementDate), 1, 'days');
}
if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type) {
if (dateType === DateRange.PreviousBillingCycle.type) {
maxTime = getUnixTimeBeforeUnixTime(maxTime, 1, 'months');
minTime = getUnixTimeBeforeUnixTime(minTime, 1, 'months');
}
} else {
let fallbackDateRange = null;
if (dateType === dateTimeConstants.allDateRanges.CurrentBillingCycle.type) { // same as This Month
fallbackDateRange = getDateRangeByDateType(dateTimeConstants.allDateRanges.ThisMonth.type, firstDayOfWeek);
} else if (dateType === dateTimeConstants.allDateRanges.PreviousBillingCycle.type) { // same as Last Month
fallbackDateRange = getDateRangeByDateType(dateTimeConstants.allDateRanges.LastMonth.type, firstDayOfWeek);
if (dateType === DateRange.CurrentBillingCycle.type) { // same as This Month
fallbackDateRange = getDateRangeByDateType(DateRange.ThisMonth.type, firstDayOfWeek);
} else if (dateType === DateRange.PreviousBillingCycle.type) { // same as Last Month
fallbackDateRange = getDateRangeByDateType(DateRange.LastMonth.type, firstDayOfWeek);
}
if (fallbackDateRange) {
@@ -672,14 +669,14 @@ export function getRecentMonthDateRanges(monthCount) {
}
let maxTime = getUnixTimeBeforeUnixTime(getUnixTimeAfterUnixTime(minTime, 1, 'months'), 1, 'seconds');
let dateType = dateTimeConstants.allDateRanges.Custom.type;
let dateType = DateRange.Custom.type;
let year = getYear(parseDateFromUnixTime(minTime));
let month = getMonth(parseDateFromUnixTime(minTime));
if (i === 0) {
dateType = dateTimeConstants.allDateRanges.ThisMonth.type;
dateType = DateRange.ThisMonth.type;
} else if (i === 1) {
dateType = dateTimeConstants.allDateRanges.LastMonth.type;
dateType = DateRange.LastMonth.type;
}
recentDateRanges.push({
@@ -707,17 +704,17 @@ export function getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, dateT
export function getRecentDateRangeType(allRecentMonthDateRanges, dateType, minTime, maxTime, firstDayOfWeek) {
let dateRange = getDateRangeByDateType(dateType, firstDayOfWeek);
if (dateRange && dateRange.dateType === dateTimeConstants.allDateRanges.All.type) {
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, dateTimeConstants.allDateRanges.All.type);
if (dateRange && dateRange.dateType === DateRange.All.type) {
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, DateRange.All.type);
}
if (!dateRange && (!maxTime || !minTime)) {
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, dateTimeConstants.allDateRanges.Custom.type);
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, DateRange.Custom.type);
}
if (!dateRange) {
dateRange = {
dateType: dateTimeConstants.allDateRanges.Custom.type,
dateType: DateRange.Custom.type,
maxTime: maxTime,
minTime: minTime
};
@@ -731,7 +728,7 @@ export function getRecentDateRangeType(allRecentMonthDateRanges, dateType, minTi
}
}
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, dateTimeConstants.allDateRanges.Custom.type);
return getRecentDateRangeTypeByDateType(allRecentMonthDateRanges, DateRange.Custom.type);
}
export function getTimeValues(date, is24Hour, isMeridiemIndicatorFirst) {
@@ -779,7 +776,7 @@ export function getCombinedDateAndTimeValues(date, timeValues, is24Hour, isMerid
hours = 0;
}
if (meridiemIndicator === dateTimeConstants.allMeridiemIndicators.PM) {
if (meridiemIndicator === MeridiemIndicator.PM.name) {
hours += 12;
}
}
+113 -113
View File
@@ -2,6 +2,7 @@ import moment from 'moment-timezone';
import { defaultLanguage, allLanguages } from '@/locales/index.ts';
import { Month, WeekDay, MeridiemIndicator, LongDateFormat, ShortDateFormat, LongTimeFormat, ShortTimeFormat, DateRangeScene, DateRange, LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE } from '@/core/datetime.ts';
import { TimezoneTypeForStatistics } from '@/core/timezone.ts';
import { CurrencyDisplayType, CurrencySortingType } from '@/core/currency.ts';
import { PresetAmountColor } from '@/core/color.ts';
@@ -11,7 +12,6 @@ import { TransactionEditScopeType, TransactionTagFilterType } from '@/core/trans
import { ScheduledTemplateFrequencyType } from '@/core/template.ts';
import numeralConstants from '@/consts/numeral.js';
import datetimeConstants from '@/consts/datetime.js';
import { UTC_TIMEZONE, ALL_TIMEZONES } from '@/consts/timezone.ts';
import { ALL_CURRENCIES } from '@/consts/currency.ts';
import { SUPPORTED_IMPORT_FILE_TYPES } from '@/consts/file.ts';
@@ -24,7 +24,6 @@ import {
isString,
isNumber,
isBoolean,
getNameByKeyValue,
copyObjectTo,
copyArrayTo
} from './common.ts';
@@ -247,90 +246,102 @@ function getCurrencyUnitName(currencyCode, isPlural, translateFn) {
return '';
}
function getAllMeridiemIndicatorNames(translateFn) {
const allMeridiemIndicatorNames = [];
function getAllMeridiemIndicators(translateFn) {
const allMeridiemIndicators = MeridiemIndicator.values();
const meridiemIndicatorNames = [];
const localizedMeridiemIndicatorNames = [];
for (let i = 0; i < datetimeConstants.allMeridiemIndicatorsArray.length; i++) {
const indicatorName = datetimeConstants.allMeridiemIndicatorsArray[i];
allMeridiemIndicatorNames.push(translateFn(`datetime.${indicatorName}.content`));
for (let i = 0; i < allMeridiemIndicators.length; i++) {
const indicator = allMeridiemIndicators[i];
meridiemIndicatorNames.push(indicator.name);
localizedMeridiemIndicatorNames.push(translateFn(`datetime.${indicator.name}.content`));
}
return allMeridiemIndicatorNames;
return {
values: meridiemIndicatorNames,
displayValues: localizedMeridiemIndicatorNames
};
}
function getAllLongMonthNames(translateFn) {
const allMonthNames = [];
const ret = [];
const allMonths = Month.values();
for (let i = 0; i < datetimeConstants.allMonthsArray.length; i++) {
const monthName = datetimeConstants.allMonthsArray[i];
allMonthNames.push(translateFn(`datetime.${monthName}.long`));
for (let i = 0; i < allMonths.length; i++) {
const month = allMonths[i];
ret.push(translateFn(`datetime.${month.name}.long`));
}
return allMonthNames;
return ret;
}
function getAllShortMonthNames(translateFn) {
const allMonthNames = [];
const ret = [];
const allMonths = Month.values();
for (let i = 0; i < datetimeConstants.allMonthsArray.length; i++) {
const monthName = datetimeConstants.allMonthsArray[i];
allMonthNames.push(translateFn(`datetime.${monthName}.short`));
for (let i = 0; i < allMonths.length; i++) {
const month = allMonths[i];
ret.push(translateFn(`datetime.${month.name}.short`));
}
return allMonthNames;
return ret;
}
function getAllLongWeekdayNames(translateFn) {
const allWeekNames = [];
const ret = [];
const allWeekDays = WeekDay.values();
for (let i = 0; i < datetimeConstants.allWeekDaysArray.length; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
allWeekNames.push(translateFn(`datetime.${weekDay.name}.long`));
for (let i = 0; i < allWeekDays.length; i++) {
const weekDay = allWeekDays[i];
ret.push(translateFn(`datetime.${weekDay.name}.long`));
}
return allWeekNames;
return ret;
}
function getAllShortWeekdayNames(translateFn) {
const allWeekNames = [];
const ret = [];
const allWeekDays = WeekDay.values();
for (let i = 0; i < datetimeConstants.allWeekDaysArray.length; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
allWeekNames.push(translateFn(`datetime.${weekDay.name}.short`));
for (let i = 0; i < allWeekDays.length; i++) {
const weekDay = allWeekDays[i];
ret.push(translateFn(`datetime.${weekDay.name}.short`));
}
return allWeekNames;
return ret;
}
function getAllMinWeekdayNames(translateFn) {
const allWeekNames = [];
const ret = [];
const allWeekDays = WeekDay.values();
for (let i = 0; i < datetimeConstants.allWeekDaysArray.length; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
allWeekNames.push(translateFn(`datetime.${weekDay.name}.min`));
for (let i = 0; i < allWeekDays.length; i++) {
const weekDay = allWeekDays[i];
ret.push(translateFn(`datetime.${weekDay.name}.min`));
}
return allWeekNames;
return ret;
}
function getAllLongDateFormats(translateFn) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
return getDateTimeFormats(translateFn, datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, 'format.longDate', defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat);
return getDateTimeFormats(translateFn, LongDateFormat.all(), LongDateFormat.values(), 'format.longDate', defaultLongDateFormatTypeName, LongDateFormat.Default);
}
function getAllShortDateFormats(translateFn) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
return getDateTimeFormats(translateFn, datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, 'format.shortDate', defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat);
return getDateTimeFormats(translateFn, ShortDateFormat.all(), ShortDateFormat.values(), 'format.shortDate', defaultShortDateFormatTypeName, ShortDateFormat.Default);
}
function getAllLongTimeFormats(translateFn) {
const defaultLongTimeFormatTypeName = translateFn('default.longTimeFormat');
return getDateTimeFormats(translateFn, datetimeConstants.allLongTimeFormat, datetimeConstants.allLongTimeFormatArray, 'format.longTime', defaultLongTimeFormatTypeName, datetimeConstants.defaultLongTimeFormat);
return getDateTimeFormats(translateFn, LongTimeFormat.values(), LongTimeFormat.values(), 'format.longTime', defaultLongTimeFormatTypeName, LongTimeFormat.Default);
}
function getAllShortTimeFormats(translateFn) {
const defaultShortTimeFormatTypeName = translateFn('default.shortTimeFormat');
return getDateTimeFormats(translateFn, datetimeConstants.allShortTimeFormat, datetimeConstants.allShortTimeFormatArray, 'format.shortTime', defaultShortTimeFormatTypeName, datetimeConstants.defaultShortTimeFormat);
return getDateTimeFormats(translateFn, ShortTimeFormat.values(), ShortTimeFormat.values(), 'format.shortTime', defaultShortTimeFormatTypeName, ShortTimeFormat.Default);
}
function getMonthShortName(monthName, translateFn) {
@@ -380,96 +391,89 @@ function getMultiMonthdayShortNames(monthDays, translateFn) {
function getMultiWeekdayLongNames(weekdayTypes, firstDayOfWeek, translateFn) {
const weekdayTypesMap = {};
const finalWeekdayTypes = [];
if (!isNumber(firstDayOfWeek)) {
firstDayOfWeek = datetimeConstants.allWeekDays.Sunday.type;
firstDayOfWeek = WeekDay.DefaultFirstDay.type;
}
for (let i = 0; i < weekdayTypes.length; i++) {
weekdayTypesMap[weekdayTypes[i]] = true;
}
for (let i = firstDayOfWeek; i < datetimeConstants.allWeekDaysArray.length; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
const allWeekDays = getAllWeekDays(firstDayOfWeek, translateFn);
const finalWeekdayNames = [];
for (let i = 0; i < allWeekDays.length; i++) {
const weekDay = allWeekDays[i];
if (weekdayTypesMap[weekDay.type]) {
finalWeekdayTypes.push(weekDay.type);
finalWeekdayNames.push(weekDay.displayName);
}
}
for (let i = 0; i < firstDayOfWeek; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
if (weekdayTypesMap[weekDay.type]) {
finalWeekdayTypes.push(weekDay.type);
}
}
const allWeekDays = getAllWeekDays(null, translateFn)
return joinMultiText(finalWeekdayTypes.map(type => getNameByKeyValue(allWeekDays, type, 'type', 'displayName')), translateFn);
return joinMultiText(finalWeekdayNames, translateFn);
}
function getI18nLongDateFormat(translateFn, formatTypeValue) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, 'format.longDate', defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, LongDateFormat.all(), LongDateFormat.values(), 'format.longDate', defaultLongDateFormatTypeName, LongDateFormat.Default, formatTypeValue);
}
function getI18nShortDateFormat(translateFn, formatTypeValue) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, 'format.shortDate', defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, ShortDateFormat.all(), ShortDateFormat.values(), 'format.shortDate', defaultShortDateFormatTypeName, ShortDateFormat.Default, formatTypeValue);
}
function getI18nLongYearFormat(translateFn, formatTypeValue) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, 'format.longYear', defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, LongDateFormat.all(), LongDateFormat.values(), 'format.longYear', defaultLongDateFormatTypeName, LongDateFormat.Default, formatTypeValue);
}
function getI18nShortYearFormat(translateFn, formatTypeValue) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, 'format.shortYear', defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, ShortDateFormat.all(), ShortDateFormat.values(), 'format.shortYear', defaultShortDateFormatTypeName, ShortDateFormat.Default, formatTypeValue);
}
function getI18nLongYearMonthFormat(translateFn, formatTypeValue) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, 'format.longYearMonth', defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, LongDateFormat.all(), LongDateFormat.values(), 'format.longYearMonth', defaultLongDateFormatTypeName, LongDateFormat.Default, formatTypeValue);
}
function getI18nShortYearMonthFormat(translateFn, formatTypeValue) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, 'format.shortYearMonth', defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, ShortDateFormat.all(), ShortDateFormat.values(), 'format.shortYearMonth', defaultShortDateFormatTypeName, ShortDateFormat.Default, formatTypeValue);
}
function getI18nLongMonthDayFormat(translateFn, formatTypeValue) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, 'format.longMonthDay', defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, LongDateFormat.all(), LongDateFormat.values(), 'format.longMonthDay', defaultLongDateFormatTypeName, LongDateFormat.Default, formatTypeValue);
}
function getI18nShortMonthDayFormat(translateFn, formatTypeValue) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, 'format.shortMonthDay', defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat, formatTypeValue);
return getDateTimeFormat(translateFn, ShortDateFormat.all(), ShortDateFormat.values(), 'format.shortMonthDay', defaultShortDateFormatTypeName, ShortDateFormat.Default, formatTypeValue);
}
function isLongDateMonthAfterYear(translateFn, formatTypeValue) {
const defaultLongDateFormatTypeName = translateFn('default.longDateFormat');
const type = getDateTimeFormatType(datetimeConstants.allLongDateFormat, datetimeConstants.allLongDateFormatArray, defaultLongDateFormatTypeName, datetimeConstants.defaultLongDateFormat, formatTypeValue);
const type = getDateTimeFormatType(LongDateFormat.all(), LongDateFormat.values(), defaultLongDateFormatTypeName, LongDateFormat.Default, formatTypeValue);
return type.isMonthAfterYear;
}
function isShortDateMonthAfterYear(translateFn, formatTypeValue) {
const defaultShortDateFormatTypeName = translateFn('default.shortDateFormat');
const type = getDateTimeFormatType(datetimeConstants.allShortDateFormat, datetimeConstants.allShortDateFormatArray, defaultShortDateFormatTypeName, datetimeConstants.defaultShortDateFormat, formatTypeValue);
const type = getDateTimeFormatType(ShortDateFormat.all(), ShortDateFormat.values(), defaultShortDateFormatTypeName, ShortDateFormat.Default, formatTypeValue);
return type.isMonthAfterYear;
}
function getI18nLongTimeFormat(translateFn, formatTypeValue) {
const defaultLongTimeFormatTypeName = translateFn('default.longTimeFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allLongTimeFormat, datetimeConstants.allLongTimeFormatArray, 'format.longTime', defaultLongTimeFormatTypeName, datetimeConstants.defaultLongTimeFormat, formatTypeValue);
return getDateTimeFormat(translateFn, LongTimeFormat.values(), LongTimeFormat.values(), 'format.longTime', defaultLongTimeFormatTypeName, LongTimeFormat.Default, formatTypeValue);
}
function getI18nShortTimeFormat(translateFn, formatTypeValue) {
const defaultShortTimeFormatTypeName = translateFn('default.shortTimeFormat');
return getDateTimeFormat(translateFn, datetimeConstants.allShortTimeFormat, datetimeConstants.allShortTimeFormatArray, 'format.shortTime', defaultShortTimeFormatTypeName, datetimeConstants.defaultShortTimeFormat, formatTypeValue);
return getDateTimeFormat(translateFn, ShortTimeFormat.values(), ShortTimeFormat.values(), 'format.shortTime', defaultShortTimeFormatTypeName, ShortTimeFormat.Default, formatTypeValue);
}
function formatYearQuarter(translateFn, year, quarter) {
@@ -485,35 +489,35 @@ function formatYearQuarter(translateFn, year, quarter) {
function isLongTime24HourFormat(translateFn, formatTypeValue) {
const defaultLongTimeFormatTypeName = translateFn('default.longTimeFormat');
const type = getDateTimeFormatType(datetimeConstants.allLongTimeFormat, datetimeConstants.allLongTimeFormatArray, defaultLongTimeFormatTypeName, datetimeConstants.defaultLongTimeFormat, formatTypeValue);
const type = getDateTimeFormatType(LongTimeFormat.values(), LongTimeFormat.values(), defaultLongTimeFormatTypeName, LongTimeFormat.Default, formatTypeValue);
return type.is24HourFormat;
}
function isLongTimeMeridiemIndicatorFirst(translateFn, formatTypeValue) {
const defaultLongTimeFormatTypeName = translateFn('default.longTimeFormat');
const type = getDateTimeFormatType(datetimeConstants.allLongTimeFormat, datetimeConstants.allLongTimeFormatArray, defaultLongTimeFormatTypeName, datetimeConstants.defaultLongTimeFormat, formatTypeValue);
const type = getDateTimeFormatType(LongTimeFormat.values(), LongTimeFormat.values(), defaultLongTimeFormatTypeName, LongTimeFormat.Default, formatTypeValue);
return type.isMeridiemIndicatorFirst;
}
function isShortTime24HourFormat(translateFn, formatTypeValue) {
const defaultShortTimeFormatTypeName = translateFn('default.shortTimeFormat');
const type = getDateTimeFormatType(datetimeConstants.allShortTimeFormat, datetimeConstants.allShortTimeFormatArray, defaultShortTimeFormatTypeName, datetimeConstants.defaultShortTimeFormat, formatTypeValue);
const type = getDateTimeFormatType(ShortTimeFormat.values(), ShortTimeFormat.values(), defaultShortTimeFormatTypeName, ShortTimeFormat.Default, formatTypeValue);
return type.is24HourFormat;
}
function isShortTimeMeridiemIndicatorFirst(translateFn, formatTypeValue) {
const defaultShortTimeFormatTypeName = translateFn('default.shortTimeFormat');
const type = getDateTimeFormatType(datetimeConstants.allShortTimeFormat, datetimeConstants.allShortTimeFormatArray, defaultShortTimeFormatTypeName, datetimeConstants.defaultShortTimeFormat, formatTypeValue);
const type = getDateTimeFormatType(ShortTimeFormat.values(), ShortTimeFormat.values(), defaultShortTimeFormatTypeName, ShortTimeFormat.Default, formatTypeValue);
return type.isMeridiemIndicatorFirst;
}
function getDateTimeFormats(translateFn, allFormatMap, allFormatArray, localeFormatPathPrefix, localeDefaultFormatTypeName, systemDefaultFormatType) {
const defaultFormat = getDateTimeFormat(translateFn, allFormatMap, allFormatArray,
localeFormatPathPrefix, localeDefaultFormatTypeName, systemDefaultFormatType, datetimeConstants.defaultDateTimeFormatValue);
localeFormatPathPrefix, localeDefaultFormatTypeName, systemDefaultFormatType, LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE);
const ret = [];
ret.push({
type: datetimeConstants.defaultDateTimeFormatValue,
type: LANGUAGE_DEFAULT_DATE_TIME_FORMAT_VALUE,
format: defaultFormat,
displayName: `${translateFn('Language Default')} (${formatCurrentTime(defaultFormat)})`
});
@@ -635,68 +639,66 @@ function getAllCurrencies(translateFn) {
}
function getAllWeekDays(firstDayOfWeek, translateFn) {
const allWeekDays = [];
const ret = [];
const allWeekDays = WeekDay.values();
if (!isNumber(firstDayOfWeek)) {
firstDayOfWeek = datetimeConstants.allWeekDays.Sunday.type;
firstDayOfWeek = WeekDay.DefaultFirstDay.type;
}
for (let i = firstDayOfWeek; i < datetimeConstants.allWeekDaysArray.length; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
for (let i = firstDayOfWeek; i < allWeekDays.length; i++) {
const weekDay = allWeekDays[i];
allWeekDays.push({
ret.push({
type: weekDay.type,
displayName: translateFn(`datetime.${weekDay.name}.long`)
});
}
for (let i = 0; i < firstDayOfWeek; i++) {
const weekDay = datetimeConstants.allWeekDaysArray[i];
const weekDay = allWeekDays[i];
allWeekDays.push({
ret.push({
type: weekDay.type,
displayName: translateFn(`datetime.${weekDay.name}.long`)
});
}
return allWeekDays;
return ret;
}
function getAllDateRanges(scene, includeCustom, includeBillingCycle, translateFn) {
const allDateRanges = [];
const ret = [];
const allDateRanges = DateRange.values();
for (let dateRangeField in datetimeConstants.allDateRanges) {
if (!Object.prototype.hasOwnProperty.call(datetimeConstants.allDateRanges, dateRangeField)) {
for (let i = 0; i < allDateRanges.length; i++) {
const dateRange = allDateRanges[i];
if (!dateRange.isAvailableForScene(scene)) {
continue;
}
const dateRangeType = datetimeConstants.allDateRanges[dateRangeField];
if (!dateRangeType.availableScenes[scene]) {
continue;
}
if (dateRangeType.isBillingCycle) {
if (dateRange.isBillingCycle) {
if (includeBillingCycle) {
allDateRanges.push({
type: dateRangeType.type,
displayName: translateFn(dateRangeType.name),
isBillingCycle: dateRangeType.isBillingCycle
ret.push({
type: dateRange.type,
displayName: translateFn(dateRange.name),
isBillingCycle: dateRange.isBillingCycle
});
}
continue;
}
if (includeCustom || dateRangeType.type !== datetimeConstants.allDateRanges.Custom.type) {
allDateRanges.push({
type: dateRangeType.type,
displayName: translateFn(dateRangeType.name)
if (includeCustom || dateRange.type !== DateRange.Custom.type) {
ret.push({
type: dateRange.type,
displayName: translateFn(dateRange.name)
});
}
}
return allDateRanges;
return ret;
}
function getAllRecentMonthDateRanges(userStore, includeAll, includeCustom, translateFn) {
@@ -705,7 +707,7 @@ function getAllRecentMonthDateRanges(userStore, includeAll, includeCustom, trans
if (includeAll) {
allRecentMonthDateRanges.push({
dateType: datetimeConstants.allDateRanges.All.type,
dateType: DateRange.All.type,
minTime: 0,
maxTime: 0,
displayName: translateFn('All')
@@ -728,7 +730,7 @@ function getAllRecentMonthDateRanges(userStore, includeAll, includeCustom, trans
if (includeCustom) {
allRecentMonthDateRanges.push({
dateType: datetimeConstants.allDateRanges.Custom.type,
dateType: DateRange.Custom.type,
minTime: 0,
maxTime: 0,
displayName: translateFn('Custom Date')
@@ -739,18 +741,16 @@ function getAllRecentMonthDateRanges(userStore, includeAll, includeCustom, trans
}
function getDateRangeDisplayName(userStore, dateType, startTime, endTime, translateFn) {
if (dateType === datetimeConstants.allDateRanges.All.type) {
return translateFn(datetimeConstants.allDateRanges.All.name);
if (dateType === DateRange.All.type) {
return translateFn(DateRange.All.name);
}
for (let dateRangeField in datetimeConstants.allDateRanges) {
if (!Object.prototype.hasOwnProperty.call(datetimeConstants.allDateRanges, dateRangeField)) {
continue;
}
const allDateRanges = DateRange.values();
const dateRange = datetimeConstants.allDateRanges[dateRangeField];
for (let i = 0; i < allDateRanges.length; i++) {
const dateRange = allDateRanges[i];
if (dateRange && dateRange.type !== datetimeConstants.allDateRanges.Custom.type && dateRange.type === dateType && dateRange.name) {
if (dateRange && dateRange.type !== DateRange.Custom.type && dateRange.type === dateType && dateRange.name) {
return translateFn(dateRange.name);
}
}
@@ -1560,9 +1560,9 @@ function setLanguage(i18nGlobal, locale, force) {
weekdaysMin : getAllMinWeekdayNames(i18nGlobal.t),
meridiem: function (hours) {
if (isPM(hours)) {
return i18nGlobal.t(`datetime.${datetimeConstants.allMeridiemIndicators.PM}.content`);
return i18nGlobal.t(`datetime.${MeridiemIndicator.PM.name}.content`);
} else {
return i18nGlobal.t(`datetime.${datetimeConstants.allMeridiemIndicators.AM}.content`);
return i18nGlobal.t(`datetime.${MeridiemIndicator.AM.name}.content`);
}
}
});
@@ -1572,10 +1572,10 @@ function setLanguage(i18nGlobal, locale, force) {
const defaultCurrency = getDefaultCurrency(i18nGlobal.t);
const defaultFirstDayOfWeekName = getDefaultFirstDayOfWeek(i18nGlobal.t);
let defaultFirstDayOfWeek = datetimeConstants.defaultFirstDayOfWeek;
let defaultFirstDayOfWeek = WeekDay.DefaultFirstDay.type;
if (datetimeConstants.allWeekDays[defaultFirstDayOfWeekName]) {
defaultFirstDayOfWeek = datetimeConstants.allWeekDays[defaultFirstDayOfWeekName].type;
if (WeekDay.parse(defaultFirstDayOfWeekName)) {
defaultFirstDayOfWeek = WeekDay.parse(defaultFirstDayOfWeekName).type;
}
return {
@@ -1666,7 +1666,7 @@ export function i18nFunctions(i18nGlobal) {
getDefaultCurrency: () => getDefaultCurrency(i18nGlobal.t),
getDefaultFirstDayOfWeek: () => getDefaultFirstDayOfWeek(i18nGlobal.t),
getCurrencyName: (currencyCode) => getCurrencyName(currencyCode, i18nGlobal.t),
getAllMeridiemIndicatorNames: () => getAllMeridiemIndicatorNames(i18nGlobal.t),
getAllMeridiemIndicators: () => getAllMeridiemIndicators(i18nGlobal.t),
getAllLongMonthNames: () => getAllLongMonthNames(i18nGlobal.t),
getAllShortMonthNames: () => getAllShortMonthNames(i18nGlobal.t),
getAllLongWeekdayNames: () => getAllLongWeekdayNames(i18nGlobal.t),