• 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 <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 
80     int64_t second2 = GetSecondsSince1970ToPointTime(curTime);
81     EXPECT_EQ(second, second2);
82 }
83 
84 /*
85  * @tc.name: testTime002
86  * @tc.desc: datetime unit
87  */
88 HWTEST_F(UtilsDateTimeTest, testTime002, TestSize.Level0)
89 {
90     int64_t days = GetDaysSince1970ToNow();
91     int64_t seconds = GetSecondsSince1970ToNow();
92     int64_t resultdays = seconds / (3600 * 24);
93     EXPECT_EQ(days, resultdays);
94 }
95 
96 /*
97  * @tc.name: testTime003
98  * @tc.desc: datetime unit
99  */
100 HWTEST_F(UtilsDateTimeTest, testTime003, TestSize.Level0)
101 {
102     struct tm curTime = { 0 };
103     bool ret = GetSystemCurrentTime(&curTime);
104     EXPECT_EQ(ret, true);
105 
106     sleep(3);
107 
108     struct tm curTime2 = { 0 };
109     ret = GetSystemCurrentTime(&curTime2);
110     EXPECT_EQ(ret, true);
111     int64_t betweensec = GetSecondsBetween(curTime, curTime2);
112     EXPECT_TRUE(betweensec >= 3);
113 }
114 
115 /*
116  * @tc.name: testTime004
117  * @tc.desc: datetime unit
118  */
119 HWTEST_F(UtilsDateTimeTest, testTime004, TestSize.Level0)
120 {
121     int timezone = 0;
122     bool ret = GetLocalTimeZone(timezone);
123     EXPECT_EQ(ret, true);
124 }
125 
126 /*
127  * @tc.name: testGetTickCount001
128  * @tc.desc: datetime unit
129  */
130 HWTEST_F(UtilsDateTimeTest, testGetTickCount001, TestSize.Level0)
131 {
132     int64_t begin = GetTickCount();
133     usleep(100000);
134     int64_t end = GetTickCount();
135 
136     EXPECT_TRUE(end - begin >= 100);
137     EXPECT_TRUE(end - begin < 120);
138 }
139 
140 /*
141  * @tc.name: testGetMicroTickCount001
142  * @tc.desc: datetime unit
143  */
144 HWTEST_F(UtilsDateTimeTest, testGetMicroTickCount001, TestSize.Level0)
145 {
146     int64_t begin = GetMicroTickCount();
147     usleep(100000);
148     int64_t end = GetMicroTickCount();
149 
150     EXPECT_TRUE(end - begin >= 100000);
151     EXPECT_TRUE(end - begin < 120000);
152 }
153 }  // namespace
154 }  // namespace OHOS