1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_TIME)
18
19 #include <unistd.h>
20 #include <time.h>
21 #include <sys/time.h>
22 #include <sys/times.h>
23 #include "bsl_sal.h"
24 #include "sal_time.h"
25 #include "bsl_errno.h"
26
TIME_GetSysTime(void)27 int64_t TIME_GetSysTime(void)
28 {
29 return (int64_t)time(NULL);
30 }
31
TIME_DateToStrConvert(const BSL_TIME * dateTime,char * timeStr,size_t len)32 uint32_t TIME_DateToStrConvert(const BSL_TIME *dateTime, char *timeStr, size_t len)
33 {
34 struct tm timeStruct = {0};
35 timeStruct.tm_year = (int32_t)dateTime->year - (int32_t)BSL_TIME_YEAR_START;
36 timeStruct.tm_mon = (int32_t)dateTime->month - 1;
37 timeStruct.tm_mday = (int32_t)dateTime->day;
38 timeStruct.tm_hour = (int32_t)dateTime->hour;
39 timeStruct.tm_min = (int32_t)dateTime->minute;
40 timeStruct.tm_sec = (int32_t)dateTime->second;
41 if (asctime_r(&timeStruct, timeStr) != NULL) {
42 return BSL_SUCCESS;
43 }
44 (void)len;
45 return BSL_INTERNAL_EXCEPTION;
46 }
47
TIME_SysTimeGet(BSL_TIME * sysTime)48 uint32_t TIME_SysTimeGet(BSL_TIME *sysTime)
49 {
50 time_t currentTime;
51 struct timeval tv;
52 uint32_t ret = BSL_SAL_ERR_BAD_PARAM;
53
54 tzset();
55 currentTime = (time_t)BSL_SAL_CurrentSysTimeGet();
56 if (currentTime != 0) {
57 ret = BSL_SAL_UtcTimeToDateConvert(currentTime, sysTime);
58 if (ret == BSL_SUCCESS) {
59 /* milliseconds : non-thread safe */
60 (void)gettimeofday(&tv, NULL);
61 sysTime->millSec = (uint16_t)tv.tv_usec / 1000U; /* 1000 is multiple */
62 sysTime->microSec = (uint32_t)tv.tv_usec % 1000U; /* 1000 is multiple */
63 }
64 }
65
66 return ret;
67 }
68
TIME_UtcTimeToDateConvert(int64_t utcTime,BSL_TIME * sysTime)69 uint32_t TIME_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime)
70 {
71 struct tm tempTime;
72 time_t utcTimeTmp = (time_t)utcTime;
73 if (gmtime_r(&utcTimeTmp, &tempTime) == NULL) {
74 return BSL_SAL_ERR_BAD_PARAM;
75 }
76
77 sysTime->year = (uint16_t)((uint16_t)tempTime.tm_year + BSL_TIME_YEAR_START); /* 1900 is base year */
78 sysTime->month = (uint8_t)((uint8_t)tempTime.tm_mon + 1U);
79 sysTime->day = (uint8_t)tempTime.tm_mday;
80 sysTime->hour = (uint8_t)tempTime.tm_hour;
81 sysTime->minute = (uint8_t)tempTime.tm_min;
82 sysTime->second = (uint8_t)tempTime.tm_sec;
83 sysTime->millSec = 0U;
84 sysTime->microSec = 0U;
85 return BSL_SUCCESS;
86 }
87
SAL_Sleep(uint32_t time)88 void SAL_Sleep(uint32_t time)
89 {
90 sleep(time);
91 }
92
SAL_Tick(void)93 long SAL_Tick(void)
94 {
95 struct tms buf = {0};
96 clock_t tickCount = times(&buf);
97 return (long)tickCount;
98 }
99
SAL_TicksPerSec(void)100 long SAL_TicksPerSec(void)
101 {
102 return sysconf(_SC_CLK_TCK);
103 }
104 #endif
105