1c36cf2e9Sopenharmony_ci/*
2c36cf2e9Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3c36cf2e9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c36cf2e9Sopenharmony_ci * you may not use this file except in compliance with the License.
5c36cf2e9Sopenharmony_ci * You may obtain a copy of the License at
6c36cf2e9Sopenharmony_ci *
7c36cf2e9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c36cf2e9Sopenharmony_ci *
9c36cf2e9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c36cf2e9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c36cf2e9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c36cf2e9Sopenharmony_ci * See the License for the specific language governing permissions and
13c36cf2e9Sopenharmony_ci * limitations under the License.
14c36cf2e9Sopenharmony_ci */
15c36cf2e9Sopenharmony_ci
16c36cf2e9Sopenharmony_ciimport { DateTimeFormat } from '../model/PrintBean';
17c36cf2e9Sopenharmony_ci
18c36cf2e9Sopenharmony_ci/**
19c36cf2e9Sopenharmony_ci * Date utils
20c36cf2e9Sopenharmony_ci */
21c36cf2e9Sopenharmony_ciexport class DateUtils {
22c36cf2e9Sopenharmony_ci  private static readonly NUMBER_TEN: Number = 10;
23c36cf2e9Sopenharmony_ci
24c36cf2e9Sopenharmony_ci  /**
25c36cf2e9Sopenharmony_ci   * get curr date time
26c36cf2e9Sopenharmony_ci   */
27c36cf2e9Sopenharmony_ci  public static getCurrTime(): string {
28c36cf2e9Sopenharmony_ci    return DateUtils.getCurrentDate(DateTimeFormat.DATE_TIME);
29c36cf2e9Sopenharmony_ci  }
30c36cf2e9Sopenharmony_ci
31c36cf2e9Sopenharmony_ci  /**
32c36cf2e9Sopenharmony_ci   * get curr date
33c36cf2e9Sopenharmony_ci   *
34c36cf2e9Sopenharmony_ci   * @param format DATE_TIME_FORMAT
35c36cf2e9Sopenharmony_ci   */
36c36cf2e9Sopenharmony_ci  public static getCurrentDate(format: DateTimeFormat): string {
37c36cf2e9Sopenharmony_ci    let now = new Date();
38c36cf2e9Sopenharmony_ci
39c36cf2e9Sopenharmony_ci    let year = now.getFullYear(); //得到年份
40c36cf2e9Sopenharmony_ci    let month = now.getMonth() + 1; //得到月份
41c36cf2e9Sopenharmony_ci    let date = now.getDate(); //得到日期
42c36cf2e9Sopenharmony_ci    let hour = now.getHours(); //得到小时
43c36cf2e9Sopenharmony_ci    let minus = now.getMinutes(); //得到分钟
44c36cf2e9Sopenharmony_ci    let sec = now.getSeconds(); //得到秒
45c36cf2e9Sopenharmony_ci
46c36cf2e9Sopenharmony_ci    let currMonth = DateUtils.completionZero(month);
47c36cf2e9Sopenharmony_ci    let currDate = DateUtils.completionZero(date);
48c36cf2e9Sopenharmony_ci    let currHour = DateUtils.completionZero(hour);
49c36cf2e9Sopenharmony_ci    let currMinus = DateUtils.completionZero(minus);
50c36cf2e9Sopenharmony_ci    let currSec = DateUtils.completionZero(sec);
51c36cf2e9Sopenharmony_ci
52c36cf2e9Sopenharmony_ci    let time = '';
53c36cf2e9Sopenharmony_ci    switch (format) {
54c36cf2e9Sopenharmony_ci      case DateTimeFormat.DATE:
55c36cf2e9Sopenharmony_ci        time = year + '-' + currMonth + '-' + currDate;
56c36cf2e9Sopenharmony_ci        break;
57c36cf2e9Sopenharmony_ci      case DateTimeFormat.TIME:
58c36cf2e9Sopenharmony_ci        time = currHour + ':' + currMinus + ':' + currSec;
59c36cf2e9Sopenharmony_ci        break;
60c36cf2e9Sopenharmony_ci      case DateTimeFormat.DATE_TIME:
61c36cf2e9Sopenharmony_ci        time = year + '-' + currMonth + '-' + currDate + ' ' + currHour + ':' + currMinus + ':' + currSec;
62c36cf2e9Sopenharmony_ci        break;
63c36cf2e9Sopenharmony_ci      default:
64c36cf2e9Sopenharmony_ci        break;
65c36cf2e9Sopenharmony_ci    }
66c36cf2e9Sopenharmony_ci    return time;
67c36cf2e9Sopenharmony_ci  }
68c36cf2e9Sopenharmony_ci
69c36cf2e9Sopenharmony_ci  static completionZero(data: number): string {
70c36cf2e9Sopenharmony_ci    let currDate = data.toString();
71c36cf2e9Sopenharmony_ci    if (data < DateUtils.NUMBER_TEN) {
72c36cf2e9Sopenharmony_ci      currDate = '0' + data;
73c36cf2e9Sopenharmony_ci    }
74c36cf2e9Sopenharmony_ci    return currDate;
75c36cf2e9Sopenharmony_ci  }
76c36cf2e9Sopenharmony_ci}