100aff185Sopenharmony_ci/* 200aff185Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 300aff185Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400aff185Sopenharmony_ci * you may not use this file except in compliance with the License. 500aff185Sopenharmony_ci * You may obtain a copy of the License at 600aff185Sopenharmony_ci * 700aff185Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800aff185Sopenharmony_ci * 900aff185Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000aff185Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100aff185Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200aff185Sopenharmony_ci * See the License for the specific language governing permissions and 1300aff185Sopenharmony_ci * limitations under the License. 1400aff185Sopenharmony_ci */ 1500aff185Sopenharmony_ci 1600aff185Sopenharmony_ciimport { Log } from './Log'; 1700aff185Sopenharmony_ciimport i18n from '@ohos.i18n'; 1800aff185Sopenharmony_ciimport Intl from '@ohos.intl'; 1900aff185Sopenharmony_ci 2000aff185Sopenharmony_ciconst TAG: string = 'common_DateUtil'; 2100aff185Sopenharmony_ci 2200aff185Sopenharmony_ciexport class DateUtil { 2300aff185Sopenharmony_ci public static readonly MILLISECONDS_PER_SECOND: number = 1000; 2400aff185Sopenharmony_ci public static readonly SECONDS_PER_MINUTE: number = 60; 2500aff185Sopenharmony_ci public static readonly SECONDS_PER_HOUR: number = 3600; 2600aff185Sopenharmony_ci private static LANGUAGE_LOCALES_MAP = { 2700aff185Sopenharmony_ci 'zh': 'zh-CN', 2800aff185Sopenharmony_ci 'en': 'en-US' 2900aff185Sopenharmony_ci }; 3000aff185Sopenharmony_ci private static readonly SECONDS_OF_ONE_DAY: number = 24 * 60 * 60; 3100aff185Sopenharmony_ci private static dateTimeFormat: Intl.DateTimeFormat = new Intl.DateTimeFormat(DateUtil.getLocales(), { 3200aff185Sopenharmony_ci year: 'numeric', 3300aff185Sopenharmony_ci month: 'long', 3400aff185Sopenharmony_ci day: 'numeric' 3500aff185Sopenharmony_ci }); 3600aff185Sopenharmony_ci private static yearAndMonthFormat: Intl.DateTimeFormat = new Intl.DateTimeFormat(DateUtil.getLocales(), { 3700aff185Sopenharmony_ci year: 'numeric', 3800aff185Sopenharmony_ci month: 'long' 3900aff185Sopenharmony_ci }); 4000aff185Sopenharmony_ci private static yearFormat: Intl.DateTimeFormat = new Intl.DateTimeFormat(DateUtil.getLocales(), { 4100aff185Sopenharmony_ci year: 'numeric' 4200aff185Sopenharmony_ci }); 4300aff185Sopenharmony_ci 4400aff185Sopenharmony_ci // Get the date after localization (year-month-day) 4500aff185Sopenharmony_ci public static getLocalizedDate(milliseconds: number): string { 4600aff185Sopenharmony_ci if(milliseconds === undefined){ 4700aff185Sopenharmony_ci milliseconds = new Date().getTime() 4800aff185Sopenharmony_ci } 4900aff185Sopenharmony_ci return this.dateTimeFormat.format(new Date(milliseconds)); 5000aff185Sopenharmony_ci } 5100aff185Sopenharmony_ci 5200aff185Sopenharmony_ci public static format(time: Date, format_s?: string): string { 5300aff185Sopenharmony_ci if (!format_s) { 5400aff185Sopenharmony_ci return time.valueOf().toString(); 5500aff185Sopenharmony_ci } 5600aff185Sopenharmony_ci let opts = { 5700aff185Sopenharmony_ci 'MM': time.getMonth() + 1, 5800aff185Sopenharmony_ci 'dd': time.getDate(), 5900aff185Sopenharmony_ci 'HH': time.getHours(), 6000aff185Sopenharmony_ci 'mm': time.getMinutes(), 6100aff185Sopenharmony_ci 'ss': time.getSeconds() 6200aff185Sopenharmony_ci } 6300aff185Sopenharmony_ci 6400aff185Sopenharmony_ci if (/(y+)/.test(format_s)) { 6500aff185Sopenharmony_ci format_s = format_s.replace('yyyy', time.getFullYear().toString().substr(0)); 6600aff185Sopenharmony_ci } 6700aff185Sopenharmony_ci for (let f in opts) { 6800aff185Sopenharmony_ci if (new RegExp('(' + f + ')').test(format_s)) { 6900aff185Sopenharmony_ci format_s = format_s.replace(f, (f.length == 1) ? opts[f] : (('00' + opts[f]).substr( 7000aff185Sopenharmony_ci opts[f].toString().length))) 7100aff185Sopenharmony_ci } 7200aff185Sopenharmony_ci } 7300aff185Sopenharmony_ci return format_s; 7400aff185Sopenharmony_ci } 7500aff185Sopenharmony_ci 7600aff185Sopenharmony_ci public static getDateTimeFormat(milliseconds: number): string { 7700aff185Sopenharmony_ci return DateUtil.format(new Date(milliseconds), 'yyyy/MM/dd HH:mm:ss'); 7800aff185Sopenharmony_ci } 7900aff185Sopenharmony_ci 8000aff185Sopenharmony_ci // Gets the localization date of the photo page grouping data 8100aff185Sopenharmony_ci public static getGroupDataLocalizedDate(milliseconds: number): Resource { 8200aff185Sopenharmony_ci let date = new Date(milliseconds); 8300aff185Sopenharmony_ci let today = new Date(); 8400aff185Sopenharmony_ci if (date.getFullYear() == today.getFullYear() && date.getMonth() == today.getMonth()) { 8500aff185Sopenharmony_ci if (date.getDate() == today.getDate()) { 8600aff185Sopenharmony_ci return $r('app.string.date_today'); 8700aff185Sopenharmony_ci } 8800aff185Sopenharmony_ci if (today.getDate() - date.getDate() == 1) { 8900aff185Sopenharmony_ci return $r('app.string.date_yesterday'); 9000aff185Sopenharmony_ci } 9100aff185Sopenharmony_ci } 9200aff185Sopenharmony_ci return $r('app.string.common_place_holder', this.getLocalizedDate(milliseconds)); 9300aff185Sopenharmony_ci } 9400aff185Sopenharmony_ci 9500aff185Sopenharmony_ci public static getLocalizedYear(milliseconds: number): Resource { 9600aff185Sopenharmony_ci return $r('app.string.common_place_holder', this.yearFormat.format(new Date(milliseconds)).toString()); 9700aff185Sopenharmony_ci } 9800aff185Sopenharmony_ci 9900aff185Sopenharmony_ci public static getLocalizedYearAndMonth(milliseconds: number): string { 10000aff185Sopenharmony_ci return this.yearAndMonthFormat.format(new Date(milliseconds)); 10100aff185Sopenharmony_ci } 10200aff185Sopenharmony_ci 10300aff185Sopenharmony_ci public static getLocalizedTime(milliseconds: number): string { 10400aff185Sopenharmony_ci let locales: string = this.getLocales(); 10500aff185Sopenharmony_ci let is24HourClock = i18n.is24HourClock(); 10600aff185Sopenharmony_ci Log.info(TAG, `get is24HourClock ${is24HourClock}`); 10700aff185Sopenharmony_ci return new Intl.DateTimeFormat(locales, { 10800aff185Sopenharmony_ci hour: (!is24HourClock ? '2-digit' : 'numeric'), 10900aff185Sopenharmony_ci minute: '2-digit' 11000aff185Sopenharmony_ci }).format(new Date(milliseconds)); 11100aff185Sopenharmony_ci } 11200aff185Sopenharmony_ci 11300aff185Sopenharmony_ci // Format duration 11400aff185Sopenharmony_ci public static getFormattedDuration(milliSecond: number): string { 11500aff185Sopenharmony_ci if (milliSecond == null) { 11600aff185Sopenharmony_ci Log.error(TAG, 'getFormattedDuration, input is null!'); 11700aff185Sopenharmony_ci return '00:00'; 11800aff185Sopenharmony_ci } 11900aff185Sopenharmony_ci if (milliSecond <= 0) { 12000aff185Sopenharmony_ci Log.error(TAG, 'getFormattedDuration, input is negative number!'); 12100aff185Sopenharmony_ci return '00:00'; 12200aff185Sopenharmony_ci } 12300aff185Sopenharmony_ci if (milliSecond < this.MILLISECONDS_PER_SECOND) { 12400aff185Sopenharmony_ci return '00:01'; 12500aff185Sopenharmony_ci } 12600aff185Sopenharmony_ci let seconds = Math.floor(milliSecond / this.MILLISECONDS_PER_SECOND); 12700aff185Sopenharmony_ci let hourTime: number = Math.floor(seconds / this.SECONDS_PER_HOUR); 12800aff185Sopenharmony_ci let minuteTime: number = Math.floor(Math.floor(seconds / this.SECONDS_PER_MINUTE) % this.SECONDS_PER_MINUTE); 12900aff185Sopenharmony_ci let secondTime: number = Math.floor(seconds % this.SECONDS_PER_MINUTE); 13000aff185Sopenharmony_ci if (hourTime > 0) { 13100aff185Sopenharmony_ci return `${hourTime}:${this.checkTime(minuteTime)}:${this.checkTime(secondTime)}`; 13200aff185Sopenharmony_ci } else { 13300aff185Sopenharmony_ci return `${this.checkTime(minuteTime)}:${this.checkTime(secondTime)}`; 13400aff185Sopenharmony_ci } 13500aff185Sopenharmony_ci } 13600aff185Sopenharmony_ci 13700aff185Sopenharmony_ci public static isTheSameDay(startTime: number, endTime: number): boolean { 13800aff185Sopenharmony_ci if (startTime == null || endTime == null) { 13900aff185Sopenharmony_ci return false; 14000aff185Sopenharmony_ci } 14100aff185Sopenharmony_ci const startTimeMs = new Date(startTime).setHours(0, 0, 0, 0); 14200aff185Sopenharmony_ci const endTimeMs = new Date(endTime).setHours(0, 0, 0, 0); 14300aff185Sopenharmony_ci return startTimeMs === endTimeMs ? true : false; 14400aff185Sopenharmony_ci } 14500aff185Sopenharmony_ci 14600aff185Sopenharmony_ci public static isTheSameYear(startTime: number, endTime: number): boolean { 14700aff185Sopenharmony_ci if (startTime == null || endTime == null) { 14800aff185Sopenharmony_ci return false; 14900aff185Sopenharmony_ci } 15000aff185Sopenharmony_ci const startYear = new Date(startTime).getFullYear(); 15100aff185Sopenharmony_ci const endYear = new Date(endTime).getFullYear(); 15200aff185Sopenharmony_ci return startYear === endYear ? true : false; 15300aff185Sopenharmony_ci } 15400aff185Sopenharmony_ci 15500aff185Sopenharmony_ci // Seconds converted to days (Less than 1 day is counted as 1 day) 15600aff185Sopenharmony_ci public static convertSecondsToDays(seconds: number): number { 15700aff185Sopenharmony_ci if (seconds % this.SECONDS_OF_ONE_DAY == 0) { 15800aff185Sopenharmony_ci return seconds / this.SECONDS_OF_ONE_DAY; 15900aff185Sopenharmony_ci } else { 16000aff185Sopenharmony_ci return parseInt(seconds / this.SECONDS_OF_ONE_DAY + '') + 1; 16100aff185Sopenharmony_ci } 16200aff185Sopenharmony_ci } 16300aff185Sopenharmony_ci 16400aff185Sopenharmony_ci public static isEmpty(obj: unknown): boolean { 16500aff185Sopenharmony_ci return obj == undefined || obj == null; 16600aff185Sopenharmony_ci } 16700aff185Sopenharmony_ci 16800aff185Sopenharmony_ci private static getLocales(): string { 16900aff185Sopenharmony_ci let systemLocale = i18n.getSystemLanguage(); 17000aff185Sopenharmony_ci let language = systemLocale.split('-')[0]; 17100aff185Sopenharmony_ci let locales: string = this.LANGUAGE_LOCALES_MAP['en']; 17200aff185Sopenharmony_ci if (this.LANGUAGE_LOCALES_MAP[language]) { 17300aff185Sopenharmony_ci locales = this.LANGUAGE_LOCALES_MAP[language]; 17400aff185Sopenharmony_ci } 17500aff185Sopenharmony_ci return locales; 17600aff185Sopenharmony_ci } 17700aff185Sopenharmony_ci 17800aff185Sopenharmony_ci private static checkTime(time: number): string { 17900aff185Sopenharmony_ci if (time < 0) { 18000aff185Sopenharmony_ci Log.error(TAG, 'checkTime, input is negative number!'); 18100aff185Sopenharmony_ci return '00'; 18200aff185Sopenharmony_ci } 18300aff185Sopenharmony_ci let formatTime: string = time.toString(); 18400aff185Sopenharmony_ci if (time < 10) { 18500aff185Sopenharmony_ci let zeroString = '0'; 18600aff185Sopenharmony_ci formatTime = zeroString.concat(formatTime); 18700aff185Sopenharmony_ci } 18800aff185Sopenharmony_ci return formatTime; 18900aff185Sopenharmony_ci } 19000aff185Sopenharmony_ci}