18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciimport settings from '@ohos.settings';
168779efd5Sopenharmony_ciimport CalendarUtil from './CalendarUtil'
178779efd5Sopenharmony_ci
188779efd5Sopenharmony_ciexport default {
198779efd5Sopenharmony_ci  numberFormatDateString(year : number, month : number, day : number) : string {
208779efd5Sopenharmony_ci    return year + '-' + month + '-' + day;
218779efd5Sopenharmony_ci  },
228779efd5Sopenharmony_ci
238779efd5Sopenharmony_ci  stringFormatDateResource(data: string, lunar: boolean): Resource | string {
248779efd5Sopenharmony_ci    let year: number = parseInt(data.substr(0, data.indexOf('-')));
258779efd5Sopenharmony_ci    let month: number = parseInt(data.substr(data.indexOf('-') + 1, data.lastIndexOf('-')));
268779efd5Sopenharmony_ci    let day: number = parseInt(data.substr(data.lastIndexOf('-') + 1, data.length));
278779efd5Sopenharmony_ci    if (lunar) {
288779efd5Sopenharmony_ci      return CalendarUtil.formatLunarDate(CalendarUtil.getLunarDate(new Date(year, month - 1, day)))
298779efd5Sopenharmony_ci    }
308779efd5Sopenharmony_ci    return $r('app.string.yearMonthDay', year, month, day);
318779efd5Sopenharmony_ci  },
328779efd5Sopenharmony_ci
338779efd5Sopenharmony_ci  judgeSysTime(context?: Context) {
348779efd5Sopenharmony_ci    return settings.getValueSync(context ? context : globalThis.context, settings.date.TIME_FORMAT, '24');
358779efd5Sopenharmony_ci  },
368779efd5Sopenharmony_ci
378779efd5Sopenharmony_ci  /**
388779efd5Sopenharmony_ci   * Obtain the description of the time within a day based on the hour.
398779efd5Sopenharmony_ci   *
408779efd5Sopenharmony_ci   * @param {number} hour
418779efd5Sopenharmony_ci   * @return {string} Time node
428779efd5Sopenharmony_ci   */
438779efd5Sopenharmony_ci  getDayMessage(hour, minutes) {
448779efd5Sopenharmony_ci    if (hour >= 0 && hour < 5) {
458779efd5Sopenharmony_ci      return $r('app.string.time_early_morning', hour, minutes);
468779efd5Sopenharmony_ci    }
478779efd5Sopenharmony_ci    if (hour >= 5 && hour < 11) {
488779efd5Sopenharmony_ci      return $r('app.string.time_morning', hour, minutes);
498779efd5Sopenharmony_ci    }
508779efd5Sopenharmony_ci    if (hour >= 11 && hour < 13) {
518779efd5Sopenharmony_ci      return $r('app.string.time_noon', hour, minutes);
528779efd5Sopenharmony_ci    }
538779efd5Sopenharmony_ci    if (hour >= 13 && hour < 17) {
548779efd5Sopenharmony_ci      return $r('app.string.time_afternoon', (parseInt(hour) - 12).toString(), minutes);
558779efd5Sopenharmony_ci    }
568779efd5Sopenharmony_ci    if (hour >= 17 && hour < 19) {
578779efd5Sopenharmony_ci
588779efd5Sopenharmony_ci      return $r('app.string.time_nightfall', (parseInt(hour) - 12).toString(), minutes);
598779efd5Sopenharmony_ci    }
608779efd5Sopenharmony_ci    if (hour >= 19 && hour < 22) {
618779efd5Sopenharmony_ci
628779efd5Sopenharmony_ci      return $r('app.string.time_night', (parseInt(hour) - 12).toString(), minutes);
638779efd5Sopenharmony_ci    }
648779efd5Sopenharmony_ci    if (hour >= 22 && hour < 24) {
658779efd5Sopenharmony_ci      return $r('app.string.time_middle_night', (parseInt(hour) - 12).toString(), minutes);
668779efd5Sopenharmony_ci    }
678779efd5Sopenharmony_ci  }
688779efd5Sopenharmony_ci}