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