• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/utils/date_util.h"
17 
18 #include <chrono>
19 #include <ctime>
20 
21 #include "base/utils/utils.h"
22 
23 namespace OHOS::Ace {
24 
Current()25 Date Date::Current()
26 {
27     Date date;
28     auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
29     auto local = std::localtime(&now);
30     CHECK_NULL_RETURN_NOLOG(local, date);
31     date.year = static_cast<uint32_t>(local->tm_year) + 1900; // local date start from 1900
32     date.month = static_cast<uint32_t>(local->tm_mon) + 1;    // local month start from 0 to 11, need add one.
33     date.day = static_cast<uint32_t>(local->tm_mday);
34     date.week = static_cast<uint32_t>(local->tm_wday);
35     return date;
36 }
37 
IsLeapYear(int32_t year)38 bool Date::IsLeapYear(int32_t year)
39 {
40     return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
41 }
42 
DayOfMonth(int32_t year,int32_t month)43 int32_t Date::DayOfMonth(int32_t year, int32_t month)
44 {
45     int32_t day = 0;
46     switch (month) {
47         case 1:
48         case 3:
49         case 5:
50         case 7:
51         case 8:
52         case 10:
53         case 12:
54             day = 31;
55             break;
56         case 4:
57         case 6:
58         case 9:
59         case 11:
60             day = 30;
61             break;
62         case 2:
63             day = IsLeapYear(year) ? 29 : 28;
64             break;
65         default:
66             day = 0;
67     }
68     return day;
69 }
70 
CalculateWeekDay(int32_t year,int32_t month,int32_t day)71 int32_t Date::CalculateWeekDay(int32_t year, int32_t month, int32_t day)
72 {
73     if (month == 1 || month == 2) {
74         month += 12;
75         year--;
76     }
77 
78     // Day of the week calculation formula
79     return (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
80 }
81 
GetMilliSecondsByDateTime(std::tm & dateTime)82 int64_t Date::GetMilliSecondsByDateTime(std::tm& dateTime)
83 {
84     auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
85     auto local = std::localtime(&now);
86     CHECK_NULL_RETURN_NOLOG(local, 0);
87     if (dateTime.tm_year == 0) {
88         dateTime.tm_year = static_cast<uint32_t>(local->tm_year);
89     }
90     if (dateTime.tm_mday == 0) {
91         dateTime.tm_mday = static_cast<uint32_t>(local->tm_mday);
92     }
93     auto timestamp = std::chrono::system_clock::from_time_t(std::mktime(&dateTime));
94     auto duration = timestamp.time_since_epoch();
95     auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
96     return milliseconds;
97 }
98 } // namespace OHOS::Ace
99