1/** 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import settings from '@ohos.settings'; 16import CalendarUtil from './CalendarUtil' 17import { HiLog } from "../../../../../common" 18 19const TAG = 'StringFormatUtil' 20 21export default { 22 numberFormatDateString(year : number, month : number, day : number) : string { 23 return year + "-" + month + "-" + day; 24 }, 25 26 stringFormatDateResource(data: string, lunar: boolean): Resource|string{ 27 let year: number = parseInt(data.substr(0, data.indexOf("-"))); 28 let month: number = parseInt(data.substr(data.indexOf("-") + 1, data.lastIndexOf("-"))); 29 let day: number = parseInt(data.substr(data.lastIndexOf("-") + 1, data.length)); 30 if (lunar) { 31 return CalendarUtil.formatLunarDate(CalendarUtil.getLunarDate(new Date(year,month-1,day))) 32 } 33 return $r("app.string.yearMonthDay", year, month, day); 34 }, 35 36 judgeSysTime(context?: Context) { 37 let timeFormat = AppStorage.get("timeFormat") as string; 38 if (timeFormat === undefined){ 39 HiLog.i(TAG, 'judgeSysTime', 'init judgeSysTime'); 40 timeFormat=settings.getValueSync(globalThis.context, settings.date.TIME_FORMAT, "24"); 41 AppStorage.setOrCreate("timeFormat",timeFormat); 42 } 43 HiLog.i(TAG, 'judgeSysTime', 'calljudgeSysTime:+ timeFormat'); 44 return timeFormat 45 }, 46 47 /** 48 * Obtain the description of the time within a day based on the hour. 49 * 50 * @param {number} hour 51 * @return {string} Time node 52 */ 53 getDayMessage(hour, minutes) { 54 if (hour >= 0 && hour < 5) { 55 return $r("app.string.time_early_morning", hour, minutes); 56 } 57 if (hour >= 5 && hour < 11) { 58 return $r("app.string.time_morning", hour, minutes); 59 } 60 if (hour >= 11 && hour < 13) { 61 return $r("app.string.time_noon", hour, minutes); 62 } 63 if (hour >= 13 && hour < 17) { 64 return $r("app.string.time_afternoon", (parseInt(hour) - 12).toString(), minutes); 65 } 66 if (hour >= 17 && hour < 19) { 67 68 return $r("app.string.time_nightfall", (parseInt(hour) - 12).toString(), minutes); 69 } 70 if (hour >= 19 && hour < 22) { 71 72 return $r("app.string.time_night", (parseInt(hour) - 12).toString(), minutes); 73 } 74 if (hour >= 22 && hour < 24) { 75 return $r("app.string.time_middle_night", (parseInt(hour) - 12).toString(), minutes); 76 } 77 } 78}