1 /*
2 * Copyright (C) 2024-2025 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 #include "gtest/gtest.h"
16 #include "time_format_utils_unit_test.h"
17
18
19 #include <chrono>
20 #include <ctime>
21 #include <iomanip>
22 #include <iostream>
23 #include <regex>
24 #include <sstream>
25 #include <string>
26
27 using namespace OHOS;
28 using namespace OHOS::Media;
29 using namespace std;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Media {
SetUpTestCase(void)34 void TimeFormatUtilsUnitTest::SetUpTestCase(void) {}
35
TearDownTestCase(void)36 void TimeFormatUtilsUnitTest::TearDownTestCase(void) {}
37
SetUp(void)38 void TimeFormatUtilsUnitTest::SetUp(void) {}
39
TearDown(void)40 void TimeFormatUtilsUnitTest::TearDown(void) {}
41
42 // Scenario1: Test standard ISO8601 type string
43 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_001, TestSize.Level0) {
44 std::string iso8601Str = "2023-01-01T01:01:01Z";
45 std::string expected = "2023-01";
46 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str).substr(0, 7), expected);
47 }
48
49 // Scenario2: Test non-standard ISO8601 type string
50 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_002, TestSize.Level0) {
51 std::string iso8601Str = "2023-01-01 01:01:01";
52 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
53 }
54
55 // Scenario3: Test compare("Z")
56 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_003, TestSize.Level0) {
57 std::string iso8601Str = "2023-01-01T23:01:01";
58 std::string expected = "2023-01";
59 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str).substr(0, 7), expected);
60 }
61
62 // Scenario4: Test ISO8601 type string with invalid time
63 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_004, TestSize.Level0) {
64 std::string iso8601Str = "2023-01-01T24:01:01Z";
65 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
66 }
67
68 // Scenario5: Test ISO8601 type string with no timezone
69 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_005, TestSize.Level0) {
70 std::string iso8601Str = "2023-01-01T01:01:01.123";
71 std::string expected = "2023-01-01 01:01:01";
72 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), expected);
73 }
74
75 // Scenario6: Test ISO8601 type string with invalid time
76 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_006, TestSize.Level0) {
77 std::string iso8601Str = "023-01-01T01:01:01.123";
78 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
79 }
80
81 // Scenario7: Test ISO8601 type string with invalid time
82 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_007, TestSize.Level0) {
83 std::string iso8601Str = "2023-01-01T01";
84 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
85 }
86
87 // Scenario8: Test ISO8601 type string with invalid time
88 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_008, TestSize.Level0) {
89 std::string iso8601Str = "2023-01-01T01.01.01.123";
90 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
91 }
92
93 // Scenario9: Test ISO8601 type string with invalid time
94 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_009, TestSize.Level0) {
95 std::string iso8601Str = "2023-01-01T01:01:01.123+0800Z";
96 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
97 }
98
99 // Scenario10: Test ISO8601 type string with invalid time
100 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_010, TestSize.Level0) {
101 std::string iso8601Str = "2023-01-01T01:01:01.123+08";
102 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
103 }
104
105 // Scenario11: Test ISO8601 type string with invalid time
106 HWTEST_F(TimeFormatUtilsUnitTest, FormatDateTimeByTimeZone_011, TestSize.Level0) {
107 std::string iso8601Str = "2023-01-01T01:01:01.123+0800T";
108 EXPECT_EQ(TimeFormatUtils::FormatDateTimeByTimeZone(iso8601Str), iso8601Str);
109 }
110
111 // Scenario1: Test when dataTime is empty
112 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_001, TestSize.Level0)
113 {
114 std::string dataTime = "";
115 std::string expected = "";
116 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
117 }
118
119 // Scenario2: Test when dataTime does not contain space and does not contain '-'
120 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_002, TestSize.Level0)
121 {
122 std::string dataTime = "2022";
123 std::string expected = "2022-01-01 00:00:00";
124 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
125 }
126
127 // Scenario3: Test when dataTime contains space but does not contain '-'
128 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_003, TestSize.Level0)
129 {
130 std::string dataTime = "2022 12";
131 std::string expected = "2022-01-01 12:00:00";
132 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
133 }
134
135 // Scenario4: Test when dataTime contains space and contains '-'
136 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_004, TestSize.Level0)
137 {
138 std::string dataTime = "2022-12-25 12:34:56";
139 std::string expected = "2022-12-25 12:34:56";
140 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
141 }
142
143 // Scenario5: Test when dataTime contains '-' but does not contain ':'
144 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_005, TestSize.Level0)
145 {
146 std::string dataTime = "2022-12-25";
147 std::string expected = "2022-12-25 00:00:00";
148 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
149 }
150
151 // Scenario6: Test when dataTime contains '-' and contains ':'
152 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_006, TestSize.Level0)
153 {
154 std::string dataTime = "2022-12-25 12:34:56.789";
155 std::string expected = "2022-12-25 12:34:56";
156 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
157 }
158
159 // Scenario6: Test when dataTime first '-' = last '-'
160 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_007, TestSize.Level0)
161 {
162 std::string dataTime = "2022-12";
163 std::string expected = "2022-12-01 00:00:00";
164 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
165 }
166
167 // Scenario6: Test when dataTime first '-' = last '-'
168 HWTEST_F(TimeFormatUtilsUnitTest, FormatDataTimeByString_008, TestSize.Level0)
169 {
170 std::string dataTime = "2022-12 12:00";
171 std::string expected = "2022-12-01 12:00:00";
172 EXPECT_EQ(TimeFormatUtils::FormatDataTimeByString(dataTime), expected);
173 }
174 } // namespace Media
175 } // namespace OHOS