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 <gtest/gtest.h>
17 #include "datetime_ex.h"
18 #include <unistd.h>
19 using namespace testing::ext;
20 #include <iostream>
21 using namespace std;
22 
23 namespace OHOS {
24 namespace {
25 class UtilsDateTimeTest : public testing::Test {
26 public :
27     static void SetUpTestCase(void);
28     static void TearDownTestCase(void);
29     void SetUp();
30     void TearDown();
31 };
32 
SetUpTestCase(void)33 void UtilsDateTimeTest::SetUpTestCase(void)
34 {
35 }
36 
TearDownTestCase(void)37 void UtilsDateTimeTest::TearDownTestCase(void)
38 {
39 }
40 
SetUp(void)41 void UtilsDateTimeTest::SetUp(void)
42 {
43 }
44 
TearDown(void)45 void UtilsDateTimeTest::TearDown(void)
46 {
47 }
48 
49 /*
50  * @tc.name: testTimecover001
51  * @tc.desc: convert all letters of str to uppercase
52  */
53 HWTEST_F(UtilsDateTimeTest, testTimecover001, TestSize.Level0)
54 {
55     int64_t second = 20;
56     EXPECT_EQ(SecToNanosec(second), 20000000000);
57     int64_t millsec = 10;
58     EXPECT_EQ(MillisecToNanosec(millsec), 10000000);
59     int64_t microsec = 5;
60     EXPECT_EQ(MicrosecToNanosec(microsec), 5000);
61 
62     int64_t nanoces = 1000000000;
63     EXPECT_EQ(NanosecToSec(nanoces), 1);
64     EXPECT_EQ(NanosecToMillisec(nanoces), 1000);
65     EXPECT_EQ(NanosecToMicrosec(nanoces), 1000000);
66 }
67 
68 /*
69  * @tc.name: testTime001
70  * @tc.desc: datetime unit
71  */
72 HWTEST_F(UtilsDateTimeTest, testTime001, TestSize.Level0)
73 {
74     int64_t second = GetSecondsSince1970ToNow();
75 
76     struct tm curTime = {0};
77     bool ret = GetSystemCurrentTime(&curTime);
78     EXPECT_EQ(ret, true);
79     ret = GetSystemCurrentTime(nullptr);
80     EXPECT_EQ(ret, false);
81 
82     int64_t second2 = GetSecondsSince1970ToPointTime(curTime);
83     EXPECT_EQ(second, second2);
84     struct tm info;
85     info.tm_year = INT32_MIN;
86     info.tm_mon = 0;
87     info.tm_mday = 0;
88     info.tm_hour = 0;
89     info.tm_min = 0;
90     info.tm_sec = 0;
91     info.tm_isdst = 0;
92     second2 = GetSecondsSince1970ToPointTime(info);
93     EXPECT_EQ(-1, second2);
94     int64_t ret2 = GetSecondsBetween(curTime, info);
95     EXPECT_TRUE(ret2 = -1);
96 }
97 
98 /*
99  * @tc.name: testTime002
100  * @tc.desc: datetime unit
101  */
102 HWTEST_F(UtilsDateTimeTest, testTime002, TestSize.Level0)
103 {
104     int64_t days = GetDaysSince1970ToNow();
105     int64_t seconds = GetSecondsSince1970ToNow();
106     int64_t resultdays = seconds / (3600 * 24);
107     EXPECT_EQ(days, resultdays);
108 }
109 
110 /*
111  * @tc.name: testTime003
112  * @tc.desc: datetime unit
113  */
114 HWTEST_F(UtilsDateTimeTest, testTime003, TestSize.Level0)
115 {
116     struct tm curTime = { 0 };
117     bool ret = GetSystemCurrentTime(&curTime);
118     EXPECT_EQ(ret, true);
119 
120     sleep(3);
121 
122     struct tm curTime2 = { 0 };
123     ret = GetSystemCurrentTime(&curTime2);
124     EXPECT_EQ(ret, true);
125     int64_t betweensec = GetSecondsBetween(curTime, curTime2);
126     EXPECT_TRUE(betweensec >= 3);
127 }
128 
129 /*
130  * @tc.name: testTime004
131  * @tc.desc: datetime unit
132  */
133 HWTEST_F(UtilsDateTimeTest, testTime004, TestSize.Level0)
134 {
135     int timezone = 0;
136     bool ret = GetLocalTimeZone(timezone);
137     EXPECT_EQ(ret, true);
138 }
139 
140 /*
141  * @tc.name: testGetTickCount001
142  * @tc.desc: datetime unit
143  */
144 HWTEST_F(UtilsDateTimeTest, testGetTickCount001, TestSize.Level0)
145 {
146     int64_t begin = GetTickCount();
147     usleep(100000);
148     int64_t end = GetTickCount();
149 
150     EXPECT_TRUE(end - begin >= 100);
151     EXPECT_TRUE(end - begin < 120);
152 }
153 
154 /*
155  * @tc.name: testGetMicroTickCount001
156  * @tc.desc: datetime unit
157  */
158 HWTEST_F(UtilsDateTimeTest, testGetMicroTickCount001, TestSize.Level0)
159 {
160     int64_t begin = GetMicroTickCount();
161     usleep(100000);
162     int64_t end = GetMicroTickCount();
163 
164     EXPECT_TRUE(end - begin >= 100000);
165     EXPECT_TRUE(end - begin < 120000);
166 }
167 }  // namespace
168 }  // namespace OHOS