Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the localtime_r () and localtime () functions in linux

Shulou Source: shulou.com Published: 2022-06-01 03:00:53 09月16日 Update

This article mainly introduces the linux localtime_r () and localtime () function how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Localtime_r () localtime () gets the local current time and date

The function prototype is as follows:

1 # include 2 3 struct tm * localtime (const time_t * timep); 4 struct tm * localtime_r (const time_t * timep, struct tm * result) 5 6 / * this function will have the value obtained by the time function timep to convert the time and date representation used in the real world, and then return the result from the structure tm to * / 7 8 / * * it should be noted that the localtime function can convert time to local time, but the localtime function is not thread safe. 9 in multithreaded applications, the localtime_r function should be used instead of the localtime function, because localtime_r is thread-safe * * / eg:

1 # include 2 # include 3 # include 4 5 int main () 6 {7 time_t timep; 8 struct tm * p; 9 10 time (& timep); 11 p = localtime (& timep) 12 13 printf ("% d-%d-%d% d:%d:%d\ n", (1900 + p-> tm_year), (1 + p-> tm_mon), p-> tm_mday, 14 (p-> tm_hour + 12), p-> tm_min, p-> tm_sec); 15 16 return 0 17} Thank you for reading this article carefully. I hope the article "how to use the localtime_r () and localtime () functions in linux" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Function time article thread safety date world value interest prototype at the same time method more friends knowledge structure result ribbon industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Shulou Information vpn Linux