1bea4f105Sopenharmony_ci/* 2bea4f105Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3bea4f105Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bea4f105Sopenharmony_ci * you may not use this file except in compliance with the License. 5bea4f105Sopenharmony_ci * You may obtain a copy of the License at 6bea4f105Sopenharmony_ci * 7bea4f105Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bea4f105Sopenharmony_ci * 9bea4f105Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bea4f105Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bea4f105Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bea4f105Sopenharmony_ci * See the License for the specific language governing permissions and 13bea4f105Sopenharmony_ci * limitations under the License. 14bea4f105Sopenharmony_ci */ 15bea4f105Sopenharmony_ci 16bea4f105Sopenharmony_ciimport { MILLISECOND } from '../constants/Constant'; 17bea4f105Sopenharmony_ciimport StringUtil from './StringUtil'; 18bea4f105Sopenharmony_ciimport { getResourceString } from './Tools'; 19bea4f105Sopenharmony_ciimport Logger from '../../base/log/Logger'; 20bea4f105Sopenharmony_ciimport LanguageUtil from './LanguageUtil'; 21bea4f105Sopenharmony_ci 22bea4f105Sopenharmony_ciconst TAG = 'DateTimeUtil'; 23bea4f105Sopenharmony_ci 24bea4f105Sopenharmony_cinamespace DateTimeUtil { 25bea4f105Sopenharmony_ci /** 26bea4f105Sopenharmony_ci * 日期格式常量 27bea4f105Sopenharmony_ci */ 28bea4f105Sopenharmony_ci export const DATE_FORMAT = { 29bea4f105Sopenharmony_ci YYYY_MM_DD: 'yyyy/mm/dd', 30bea4f105Sopenharmony_ci MM_DD: 'mm/dd', 31bea4f105Sopenharmony_ci DATE_TIME: 'yyyy/mm/dd HH:MM:SS', 32bea4f105Sopenharmony_ci YYYY_MM_DD_CHINESE: 'yyyy年mm月dd日', 33bea4f105Sopenharmony_ci MM_DD_CHINESE: 'm月d日', 34bea4f105Sopenharmony_ci DATE_TIME_CHINESE: 'yyyy年mm月dd日 HH:MM' 35bea4f105Sopenharmony_ci }; 36bea4f105Sopenharmony_ci 37bea4f105Sopenharmony_ci /** 38bea4f105Sopenharmony_ci * @description 获取指定时间戳当天0点的时间戳 39bea4f105Sopenharmony_ci * @param timeStamp 指定时间戳 40bea4f105Sopenharmony_ci * @return 当天0点的时间戳 41bea4f105Sopenharmony_ci */ 42bea4f105Sopenharmony_ci export function getDayBeginTimeStamp(timeStamp: number) { 43bea4f105Sopenharmony_ci const date = new Date(timeStamp); 44bea4f105Sopenharmony_ci const year = date.getFullYear(); 45bea4f105Sopenharmony_ci const month = date.getMonth() + 1; 46bea4f105Sopenharmony_ci const day = date.getDate(); 47bea4f105Sopenharmony_ci 48bea4f105Sopenharmony_ci return new Date(`${year}-${month}-${day}`).getTime(); 49bea4f105Sopenharmony_ci } 50bea4f105Sopenharmony_ci 51bea4f105Sopenharmony_ci /** 52bea4f105Sopenharmony_ci * @description 将时长转化成转化为时分秒 53bea4f105Sopenharmony_ci * @param duration 时长 54bea4f105Sopenharmony_ci * @param unit 时间单位 55bea4f105Sopenharmony_ci * @return 格式化时长,如:08:20 56bea4f105Sopenharmony_ci */ 57bea4f105Sopenharmony_ci export function formatDuration(duration: number): string { 58bea4f105Sopenharmony_ci let remainTime = duration; 59bea4f105Sopenharmony_ci const hours = Math.floor(remainTime / MILLISECOND.ONE_HOUR); 60bea4f105Sopenharmony_ci remainTime -= hours * MILLISECOND.ONE_HOUR; 61bea4f105Sopenharmony_ci const minutes = Math.floor(remainTime / MILLISECOND.ONE_MINUTE); 62bea4f105Sopenharmony_ci remainTime -= minutes * MILLISECOND.ONE_MINUTE; 63bea4f105Sopenharmony_ci const seconds = Math.floor(remainTime / MILLISECOND.ONE_SECOND); 64bea4f105Sopenharmony_ci 65bea4f105Sopenharmony_ci const minutesStr = StringUtil.padStart(minutes); 66bea4f105Sopenharmony_ci const secondsStr = StringUtil.padStart(seconds); 67bea4f105Sopenharmony_ci if (hours) { 68bea4f105Sopenharmony_ci const hoursStr = StringUtil.padStart(hours); 69bea4f105Sopenharmony_ci return `${hoursStr}:${minutesStr}:${secondsStr}`; 70bea4f105Sopenharmony_ci } else { 71bea4f105Sopenharmony_ci return `${minutesStr}:${secondsStr}`; 72bea4f105Sopenharmony_ci } 73bea4f105Sopenharmony_ci } 74bea4f105Sopenharmony_ci 75bea4f105Sopenharmony_ci /** 76bea4f105Sopenharmony_ci * @description 格式化文件更新时间 77bea4f105Sopenharmony_ci * @param timeStamp 时间戳,单位毫秒 78bea4f105Sopenharmony_ci * @param 格式化时间,如:YYYY/MM/HH hh:mm:ss 79bea4f105Sopenharmony_ci */ 80bea4f105Sopenharmony_ci export function formatFileModifiedTime(timeStamp: number): string { 81bea4f105Sopenharmony_ci const dateTime = new Date(timeStamp); 82bea4f105Sopenharmony_ci const year = dateTime.getFullYear(); 83bea4f105Sopenharmony_ci const month = StringUtil.padStart(dateTime.getMonth() + 1); 84bea4f105Sopenharmony_ci const day = StringUtil.padStart(dateTime.getDate()); 85bea4f105Sopenharmony_ci const hour = StringUtil.padStart(dateTime.getHours()); 86bea4f105Sopenharmony_ci const minute = StringUtil.padStart(dateTime.getMinutes()); 87bea4f105Sopenharmony_ci const second = StringUtil.padStart(dateTime.getSeconds()); 88bea4f105Sopenharmony_ci return `${year}/${month}/${day} ${hour}:${minute}:${second}`; 89bea4f105Sopenharmony_ci } 90bea4f105Sopenharmony_ci 91bea4f105Sopenharmony_ci /** 92bea4f105Sopenharmony_ci * 日期格式转换 93bea4f105Sopenharmony_ci * @param timestamp 时间戳 94bea4f105Sopenharmony_ci * @param format 格式(可选) 95bea4f105Sopenharmony_ci * @example formatDate(new Date(), "YYYY-mm-dd HH:MM:SS") => 2021-11-02 09:39:59 96bea4f105Sopenharmony_ci */ 97bea4f105Sopenharmony_ci export function formatDate(timestamp, format = DATE_FORMAT.YYYY_MM_DD) { 98bea4f105Sopenharmony_ci let res = ''; 99bea4f105Sopenharmony_ci try { 100bea4f105Sopenharmony_ci const date = new Date(timestamp); 101bea4f105Sopenharmony_ci const opt = { 102bea4f105Sopenharmony_ci 'y+': date.getFullYear().toString(), // 年 103bea4f105Sopenharmony_ci 'm+': (date.getMonth() + 1).toString(), // 月 104bea4f105Sopenharmony_ci 'd+': date.getDate().toString(), // 日 105bea4f105Sopenharmony_ci 'H+': date.getHours().toString(), // 时 106bea4f105Sopenharmony_ci 'M+': date.getMinutes().toString(), // 分 107bea4f105Sopenharmony_ci 'S+': date.getSeconds().toString(), // 秒 108bea4f105Sopenharmony_ci }; 109bea4f105Sopenharmony_ci for (let key in opt) { 110bea4f105Sopenharmony_ci const reg = new RegExp(key); 111bea4f105Sopenharmony_ci let ret = reg.exec(format); 112bea4f105Sopenharmony_ci if (ret) { 113bea4f105Sopenharmony_ci format = format.replace( 114bea4f105Sopenharmony_ci reg, ret[0].length == 1 ? opt[key] : opt[key].padStart(ret[0].length, '0') 115bea4f105Sopenharmony_ci ); 116bea4f105Sopenharmony_ci } 117bea4f105Sopenharmony_ci } 118bea4f105Sopenharmony_ci res = format; 119bea4f105Sopenharmony_ci } catch (error) { 120bea4f105Sopenharmony_ci Logger.e(TAG, 'formatDate error: ' + error.toString()); 121bea4f105Sopenharmony_ci } 122bea4f105Sopenharmony_ci return res; 123bea4f105Sopenharmony_ci } 124bea4f105Sopenharmony_ci 125bea4f105Sopenharmony_ci /** 126bea4f105Sopenharmony_ci * @description 获取YYYY/MM/DD格式的日期 127bea4f105Sopenharmony_ci * @param timeStamp 时间戳,单位毫秒 128bea4f105Sopenharmony_ci * @return 格式化时间,格式:中文显示“2022/10/08”,其他显示“08/10/2022” 129bea4f105Sopenharmony_ci */ 130bea4f105Sopenharmony_ci export function getSimpleDateString(timeStamp: number, format: string = ''): string { 131bea4f105Sopenharmony_ci return formatDate(timeStamp, format || DATE_FORMAT.YYYY_MM_DD); 132bea4f105Sopenharmony_ci } 133bea4f105Sopenharmony_ci 134bea4f105Sopenharmony_ci /** 135bea4f105Sopenharmony_ci * @description 格式化时间 136bea4f105Sopenharmony_ci * @param timeStamp 时间戳,单位毫秒 137bea4f105Sopenharmony_ci * @return 格式化时间,规则如下: 138bea4f105Sopenharmony_ci * 当前日期同一天:今天 139bea4f105Sopenharmony_ci * 当前日期前一天:昨天 140bea4f105Sopenharmony_ci * 其他:YYYY/MM/DD 141bea4f105Sopenharmony_ci */ 142bea4f105Sopenharmony_ci export function getDateStringForCategory(timeStamp: number): string { 143bea4f105Sopenharmony_ci const now = Date.now(); 144bea4f105Sopenharmony_ci const current = getDayBeginTimeStamp(timeStamp); 145bea4f105Sopenharmony_ci const today = getDayBeginTimeStamp(now); 146bea4f105Sopenharmony_ci const yesterday = today - MILLISECOND.ONE_DAY; 147bea4f105Sopenharmony_ci if (current === today) { 148bea4f105Sopenharmony_ci return getResourceString($r('app.string.today')); 149bea4f105Sopenharmony_ci } else if (current === yesterday) { 150bea4f105Sopenharmony_ci return getResourceString($r('app.string.yesterday')); 151bea4f105Sopenharmony_ci } else { 152bea4f105Sopenharmony_ci return getSimpleDateString(timeStamp); 153bea4f105Sopenharmony_ci } 154bea4f105Sopenharmony_ci } 155bea4f105Sopenharmony_ci 156bea4f105Sopenharmony_ci /** 157bea4f105Sopenharmony_ci * @description 获取最近卡片显示的格式日期 158bea4f105Sopenharmony_ci * @param timeStamp 时间戳,单位毫秒 159bea4f105Sopenharmony_ci * @return 格式化时间,规则如下: 160bea4f105Sopenharmony_ci * 当前时间1分钟内:刚刚 161bea4f105Sopenharmony_ci * 当前时间1小时内:XX分钟前 162bea4f105Sopenharmony_ci * 当前日期同一天:上午 08:00 163bea4f105Sopenharmony_ci * 当前日期前一天:昨天 164bea4f105Sopenharmony_ci * 今年:MM月DD日 165bea4f105Sopenharmony_ci * 往年:YYYY/MM/DD 166bea4f105Sopenharmony_ci */ 167bea4f105Sopenharmony_ci export function getDateStringForRecentCard(timeStamp: number): Resource | string { 168bea4f105Sopenharmony_ci let dateString = undefined; 169bea4f105Sopenharmony_ci const date = new Date(); 170bea4f105Sopenharmony_ci const now = date.getTime(); 171bea4f105Sopenharmony_ci const nowYear = date.getFullYear(); 172bea4f105Sopenharmony_ci const cardYear = new Date(timeStamp).getFullYear(); 173bea4f105Sopenharmony_ci const timeGap = now - timeStamp; 174bea4f105Sopenharmony_ci const todayBegin = getDayBeginTimeStamp(now); 175bea4f105Sopenharmony_ci const yesterdayBegin = todayBegin - MILLISECOND.ONE_DAY; 176bea4f105Sopenharmony_ci if (timeStamp > todayBegin) { 177bea4f105Sopenharmony_ci if (timeGap < MILLISECOND.ONE_MINUTE) { 178bea4f105Sopenharmony_ci dateString = $r('app.string.justNow'); 179bea4f105Sopenharmony_ci } else if (timeGap < MILLISECOND.ONE_HOUR) { 180bea4f105Sopenharmony_ci const minute = Math.floor(timeGap / MILLISECOND.ONE_MINUTE); 181bea4f105Sopenharmony_ci dateString = $r('app.plural.minute_ago', minute, minute); 182bea4f105Sopenharmony_ci } else { 183bea4f105Sopenharmony_ci dateString = $r('app.string.today'); 184bea4f105Sopenharmony_ci } 185bea4f105Sopenharmony_ci } else if (timeStamp > yesterdayBegin) { 186bea4f105Sopenharmony_ci dateString = $r('app.string.yesterday'); 187bea4f105Sopenharmony_ci } else if (cardYear === nowYear) { 188bea4f105Sopenharmony_ci const format = LanguageUtil.isChineseLanguage() ? DATE_FORMAT.MM_DD_CHINESE : DATE_FORMAT.MM_DD; 189bea4f105Sopenharmony_ci dateString = formatDate(timeStamp, format); 190bea4f105Sopenharmony_ci } else { 191bea4f105Sopenharmony_ci dateString = getSimpleDateString(timeStamp); 192bea4f105Sopenharmony_ci } 193bea4f105Sopenharmony_ci return dateString; 194bea4f105Sopenharmony_ci } 195bea4f105Sopenharmony_ci} 196bea4f105Sopenharmony_ci 197bea4f105Sopenharmony_ciexport default DateTimeUtil; 198