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' 20export default { 21 numberFormatDateString(year : number, month : number, day : number) : string { 22 return year + "-" + month + "-" + day; 23 }, 24 25 stringFormatDateResource(data: string, lunar: boolean): Resource|string{ 26 let year: number = parseInt(data.substr(0, data.indexOf("-"))); 27 let month: number = parseInt(data.substr(data.indexOf("-") + 1, data.lastIndexOf("-"))); 28 let day: number = parseInt(data.substr(data.lastIndexOf("-") + 1, data.length)); 29 if (lunar) { 30 return CalendarUtil.formatLunarDate(CalendarUtil.getLunarDate(new Date(year,month-1,day))) 31 } 32 return $r("app.string.yearMonthDay", year, month, day); 33 }, 34 35 judgeSysTime(context?: Context) { 36 HiLog.i(TAG, 'judgeSysTime', 'init judgeSysTime'); 37 return settings.getValueSync(context ? context : globalThis.context, settings.date.TIME_FORMAT, "24"); 38 }, 39 40 /** 41 * Obtain the description of the time within a day based on the hour. 42 * 43 * @param {number} hour 44 * @return {string} Time node 45 */ 46 getDayMessage(hour, minutes) { 47 if (hour >= 0 && hour < 5) { 48 return $r("app.string.time_early_morning", hour, minutes); 49 } 50 if (hour >= 5 && hour < 11) { 51 return $r("app.string.time_morning", hour, minutes); 52 } 53 if (hour >= 11 && hour < 13) { 54 return $r("app.string.time_noon", hour, minutes); 55 } 56 if (hour >= 13 && hour < 17) { 57 return $r("app.string.time_afternoon", (parseInt(hour) - 12).toString(), minutes); 58 } 59 if (hour >= 17 && hour < 19) { 60 61 return $r("app.string.time_nightfall", (parseInt(hour) - 12).toString(), minutes); 62 } 63 if (hour >= 19 && hour < 22) { 64 65 return $r("app.string.time_night", (parseInt(hour) - 12).toString(), minutes); 66 } 67 if (hour >= 22 && hour < 24) { 68 return $r("app.string.time_middle_night", (parseInt(hour) - 12).toString(), minutes); 69 } 70 } 71}