Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ctime () and gmtime () in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use ctime () and gmtime () of C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The function prototype is as follows:

_ _ CRT_INLINE char * _ cdecl ctime (const time_t * _ Time); _ _ CRT_INLINE struct tm * _ cdecl gmtime (const time_t * _ Time); ctime function

The ctime function converts the current time value to a string format to return. The returned string format is: Www Mmm dd hh:mm:ss yyyy, where Www represents the day of the week, Mmm represents the month in letters, dd represents the day of the month, hh:mm:ss represents the time, and yyyy represents the year.

The current time value needs to be obtained through the time function, and the number of seconds that have elapsed since 00:00:00 on 1970-01-01 is obtained through the time function. The number of seconds is then passed as an argument to the ctime function, which converts the number of seconds to a string in the current time format.

Here is a simple example to demonstrate the use of the ctime function.

# include # include # include int main (int argc, char** argv) {time_t now_time; time (& now_time); printf ("% s", ctime (& now_time)); return 0;}

First get the time count value through the time function, then pass the count value into the ctime function, and then use the printf function to print out the string returned by the ctime function. The running result of the program is as follows:

Gmtime function

The gmtime function is a function that converts a date and time to GMT time. The information in the time_t structure referred to by the parameter time is converted into the real-world time and date representation, and the result is returned by the structure tm.

This is a bit abstract to understand, so let's demonstrate the use of the gmtime function directly through the code.

The time structure information is as follows:

Struct tm {int tm_sec; / * seconds, ranging from 0 to 59 * / int tm_min; / * minutes, from 0 to 59 * / int tm_hour; / * hours, from 0 to 23 * / int tm_mday / * days of the month, from 1 to 31 * / int tm_mon; / * months, from 0 to 11 * / int tm_year; / * years from 1900 * / int tm_wday; / * days of the week, from 0 to 6 * / int tm_yday / * the day of the year, ranging from 0 to 365 * / int tm_isdst; / * daylight saving time * /}

This time structure is defined in the header file time.h.

# include # include # include int main (int argc, char** argv) {char* wday [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; time_t timep; struct tm * p; time (& timep); p=gmtime (& timep) / * get the current time of the system * / printf ("% d, day", (1900 days p-> tm_year), (1 days p-> tm_mon), p-> tm_mday); printf ("% s d:d:d\ n", Wdaytime [p-> tm_wday], (p-> tm_hour+8), p-> tm_min,p- > tm_sec); return 0;}

The running results of the system are as follows:

Note here that the number of years returned by the system has been calculated since 1900, so remember to add 1900 to the value of the year when printing the year.

Through the above two functions, we can see that the time format obtained by the ctime function is in the form of a string, and the time format obtained by the gmtime function is in the form of a structure. Make your own choices in the program to call different functions.

This is the end of the introduction of "how to use ctime () and gmtime () in C language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report