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