1 /*
2  * Copyright (c) 2022 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 <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <wchar.h>
21 #include <locale.h>
22 #include "test.h"
23 
24 /**
25  * @tc.name      : wcsftime_l_0100
26  * @tc.desc      : Format system time
27  * @tc.level     : Level 0
28  */
wcsftime_l_0100(void)29 void wcsftime_l_0100(void)
30 {
31     time_t rtime;
32     struct tm *timeinfo;
33     wchar_t buffer[80];
34     time(&rtime);
35     timeinfo = localtime(&rtime);
36     locale_t newlocale_ = newlocale(LC_ALL_MASK, "en_US", NULL);
37     size_t result = wcsftime_l(buffer, 80, L"%I:%M%p", timeinfo, newlocale_);
38     if (!result) {
39         t_error("%s wcsftime_l failed\n", __func__);
40     }
41     if (newlocale_) {
42         freelocale(newlocale_);
43         newlocale_ = NULL;
44     }
45 }
46 
47 /**
48  * @tc.name      : wcsftime_l_0200
49  * @tc.desc      : Format the given tm time
50  * @tc.level     : Level 1
51  */
wcsftime_l_0200(void)52 void wcsftime_l_0200(void)
53 {
54     wchar_t buff[40];
55     struct tm mtime = {
56         .tm_year = 122,
57         .tm_mon = 5,
58         .tm_mday = 6,
59         .tm_hour = 8,
60         .tm_min = 10,
61         .tm_sec = 20,
62     };
63     locale_t newlocale_ = newlocale(LC_ALL_MASK, "en_US", NULL);
64     size_t result = wcsftime_l(buff, sizeof buff, L"%A %c", &mtime, newlocale_);
65     if (!result) {
66         t_error("%s wcsftime_l failed\n", __func__);
67     }
68     if (wcscmp(buff, L"Sunday Sun Jun  6 08:10:20 2022")) {
69         t_error("%s wrong format result is %ls\n", __func__, buff);
70     }
71     if (newlocale_) {
72         freelocale(newlocale_);
73         newlocale_ = NULL;
74     }
75 }
76 
main(int argc, char *argv[])77 int main(int argc, char *argv[])
78 {
79     wcsftime_l_0100();
80     wcsftime_l_0200();
81     return t_status;
82 }