19b256929Sopenharmony_ci/**
29b256929Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
39b256929Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49b256929Sopenharmony_ci * you may not use this file except in compliance with the License.
59b256929Sopenharmony_ci * You may obtain a copy of the License at
69b256929Sopenharmony_ci *
79b256929Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89b256929Sopenharmony_ci *
99b256929Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109b256929Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119b256929Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129b256929Sopenharmony_ci * See the License for the specific language governing permissions and
139b256929Sopenharmony_ci * limitations under the License.
149b256929Sopenharmony_ci */
159b256929Sopenharmony_ciimport {ConvertLunarCalendar} from '../../../../../common/src/main/ets/default/LunarCalendar'
169b256929Sopenharmony_ci
179b256929Sopenharmony_ciexport class DateTimeCommon {
189b256929Sopenharmony_ci
199b256929Sopenharmony_ci  static getInstance(): DateTimeCommon {
209b256929Sopenharmony_ci    if (globalThis.DateTimeCommon == null) {
219b256929Sopenharmony_ci      globalThis.DateTimeCommon = new DateTimeCommon();
229b256929Sopenharmony_ci    }
239b256929Sopenharmony_ci    return globalThis.DateTimeCommon;
249b256929Sopenharmony_ci  }
259b256929Sopenharmony_ci
269b256929Sopenharmony_ci  getSystemTime(isUsing24hFormat: boolean) {
279b256929Sopenharmony_ci    let dateTime = new Date();
289b256929Sopenharmony_ci    let hours = dateTime.getHours();
299b256929Sopenharmony_ci    if (!isUsing24hFormat && hours > 12) {
309b256929Sopenharmony_ci      hours = hours % 12;
319b256929Sopenharmony_ci    }
329b256929Sopenharmony_ci    let minutes = dateTime.getMinutes();
339b256929Sopenharmony_ci    let time = this.concatTime(hours, minutes)
349b256929Sopenharmony_ci    return time;
359b256929Sopenharmony_ci  }
369b256929Sopenharmony_ci
379b256929Sopenharmony_ci  getSystemDate(): {} {
389b256929Sopenharmony_ci    let dateTime = new Date();
399b256929Sopenharmony_ci    let result = {
409b256929Sopenharmony_ci      'year': dateTime.getFullYear(),
419b256929Sopenharmony_ci      'month': dateTime.getMonth() + 1,
429b256929Sopenharmony_ci      'day': dateTime.getDate()
439b256929Sopenharmony_ci    }
449b256929Sopenharmony_ci    return result
459b256929Sopenharmony_ci  }
469b256929Sopenharmony_ci
479b256929Sopenharmony_ci  getCalendarDate(): {} {
489b256929Sopenharmony_ci    let dateTime = new Date();
499b256929Sopenharmony_ci    let res = {
509b256929Sopenharmony_ci      'calendarYear': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarYear,
519b256929Sopenharmony_ci      'calendarMonth': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarMonth,
529b256929Sopenharmony_ci      'calendarDay': ConvertLunarCalendar( dateTime.getFullYear(), dateTime.getMonth() + 1, dateTime.getDate()).lunarDay
539b256929Sopenharmony_ci    }
549b256929Sopenharmony_ci    return res
559b256929Sopenharmony_ci  }
569b256929Sopenharmony_ci
579b256929Sopenharmony_ci  getSystemWeek() {
589b256929Sopenharmony_ci    let dateTime = new Date();
599b256929Sopenharmony_ci    let days = dateTime.getDay();
609b256929Sopenharmony_ci    let week = this.convert(days)
619b256929Sopenharmony_ci    return week
629b256929Sopenharmony_ci  }
639b256929Sopenharmony_ci
649b256929Sopenharmony_ci  concatTime(hours, minutes): string{
659b256929Sopenharmony_ci    return `${this.fill(hours)}:${this.fill(minutes)}`;
669b256929Sopenharmony_ci  };
679b256929Sopenharmony_ci
689b256929Sopenharmony_ci  fill(value) {
699b256929Sopenharmony_ci    return (value > 9 ? "" : "0") + value;
709b256929Sopenharmony_ci  };
719b256929Sopenharmony_ci
729b256929Sopenharmony_ci  convert(days) {
739b256929Sopenharmony_ci    switch (days) {
749b256929Sopenharmony_ci      case 0:
759b256929Sopenharmony_ci        return $r('app.string.sunday');
769b256929Sopenharmony_ci        break;
779b256929Sopenharmony_ci      case 1:
789b256929Sopenharmony_ci        return $r('app.string.monday');
799b256929Sopenharmony_ci        break;
809b256929Sopenharmony_ci      case 2:
819b256929Sopenharmony_ci        return $r('app.string.tuesday');
829b256929Sopenharmony_ci        break;
839b256929Sopenharmony_ci      case 3:
849b256929Sopenharmony_ci        return $r('app.string.wednesday');
859b256929Sopenharmony_ci        break;
869b256929Sopenharmony_ci      case 4:
879b256929Sopenharmony_ci        return $r('app.string.thursday');
889b256929Sopenharmony_ci        break;
899b256929Sopenharmony_ci      case 5:
909b256929Sopenharmony_ci        return $r('app.string.friday');
919b256929Sopenharmony_ci        break;
929b256929Sopenharmony_ci      case 6:
939b256929Sopenharmony_ci        return $r('app.string.saturday');
949b256929Sopenharmony_ci        break;
959b256929Sopenharmony_ci      default:
969b256929Sopenharmony_ci        break;
979b256929Sopenharmony_ci    }
989b256929Sopenharmony_ci  }
999b256929Sopenharmony_ci}
1009b256929Sopenharmony_ci
1019b256929Sopenharmony_ciexport let dateTimeCommon = DateTimeCommon.getInstance();