1/**
2 * Copyright (c) 2021 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 */
15/**
16 * date package tool class
17 */
18
19import ConfigData from './ConfigData';
20import i18n from '@ohos.i18n';
21import Intl from "@ohos.intl";
22
23export class DateAndTimeUtil {
24  getSystemTime(isUsing24hFormat: boolean) {
25    let datetime = new Date();
26
27    // @ts-ignore
28    var datefmt= new Intl.DateTimeFormat(i18n.getSystemLanguage(), { timeStyle: 'short' });
29    return datefmt.format(datetime);
30  }
31
32  getSystemDate() {
33    let datetime = new Date();
34
35    // @ts-ignore
36    var datefmt= new Intl.DateTimeFormat(i18n.getSystemLanguage(), { dateStyle: 'long' });
37    return datefmt.format(datetime);
38  }
39
40  fill(value) {
41    return (value > 9 ? "" : "0") + value;
42  };
43
44  concatDate(year, month, date) {
45    let nYear = AppStorage.Get(ConfigData.DATE_AND_TIME_YEAR);
46    let nMonth = AppStorage.Get(ConfigData.DATE_AND_TIME_MONTH);
47    let nDay = AppStorage.Get(ConfigData.DATE_AND_TIME_DAY);
48    return year + nYear + month + nMonth + date + nDay;
49  };
50
51  concatTime(hours, minutes) {
52    return `${this.fill(hours)}:${this.fill(minutes)}`;
53  };
54
55  convert(y, m, d) {
56    if (m == 1 || m == 2) {
57      m += 12;
58      y--;
59    }
60    let a = 3 * (m + 1) / 5;
61    let b = y / 4
62    let c = y / 100
63    let e = y / 400
64    const week = (d + 2 * m + Math.floor(a) + y + Math.floor(b)
65    - Math.floor(c) + Math.floor(e) + 1) % 7;
66    switch (week) {
67      case 0:
68        return $r('app.string.sunday');
69        break;
70      case 1:
71        return $r('app.string.monday');
72        break;
73      case 2:
74        return $r('app.string.tuesday');
75        break;
76      case 3:
77        return $r('app.string.wednesday');
78        break;
79      case 4:
80        return $r('app.string.thursday');
81        break;
82      case 5:
83        return $r('app.string.friday');
84        break;
85      case 6:
86        return $r('app.string.saturday');
87        break;
88      default:
89        break;
90    }
91  };
92}
93
94let dateAndTimeUtil = new DateAndTimeUtil();
95export default dateAndTimeUtil as DateAndTimeUtil
96