In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The editor of this article introduces in detail "how to get the current date and time in c language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to get the current date and time in c language" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Header file time.h
# include
To get the time, you have to say a function time ()
Prototype: time_t time (time_t * timer)
In PHP, there is a time () function that returns a timestamp-- that is, an integer. It is then formatted by the date () function and converted to the time format we want. In C, the function of this function is to get the current system time and return a value of type time_t. It's also a time stamp. It's actually a big integer. How is this integer calculated? it is the number of seconds from 00:00:00 UTC (Coordinate Universal Time) time on January 1, 1970 (called UNIX system Epoch time) to the current moment.
Just this number of seconds is not enough, we also need a way to convert it to the format we need. In C, you can call the localtime () function to convert the UTC time represented by time_t to local time (we are in the + 8 area, 8 hours more than UTC) and convert it to struct tm type.
The number of time_t is calculated in terms of UTC, regardless of the time zone, and the return value of time (NULL) is the same on all computers in the world at the same time. The time zone needs to be considered when converting to a displayable format with localtime ().
There are two functions that convert time_t to struct tm-- localtime () and gmtime ()
Prototypes: struct tm * localtime (time_t * timer) and struct tm * gmtime (time_t * timer)
The difference between these two functions is that gmtime () converts the time in the 0 time zone. Localtime () takes into account the local time zone. What do you mean, that is, our time zone is + 8, which is 8 hours more than UTC. Suppose the current time is 15:40:56. After being transferred out with gmtime (), it is 7 40 56; after being transferred out with localtime (), it is 15:40:56.
Next, let's introduce struct tm.
Struct tm {int tm_sec; / / second int tm_min; / minute int tm_hour; / hour int tm_mday; / / day int tm_mon; / / month int tm_year; / / year int tm_wday; / / indicates the day of the week 0-6 (0: Sunday 1: Monday 2: Tuesday 3: Wednesday 4: Thursday 5: Friday 6: Saturday) int tm_yday / / from January 1st to the present day of the year, a value of 0-365int tm_isdst; / / indicates whether daylight saving time 1 is 0, whether it is used in the mktime () function and sometimes-1, then it is up to mktime () to judge whether it is daylight saving time}
It is important to note that tm_mon starts at 0, that is, the month is added by 1; tm_year indicates how many years have passed since 1900, that is, the difference between this year and 1900. So you have to use tm_year+1900 to indicate the year of this year.
Here is a complete example
# include # include int main (void) {time_t time_ptr;struct tm * tmp_ptr = NULL;int year,month,day,hour,minute,sec;// get the current time time (& time_ptr); tmp_ptr = localtime (& time_ptr); year = tmp_ptr- > tm_year + 1900; month = tmp_ptr- > tm_mon + 1; day = tmp_ptr- > tm_mday; hour = tmp_ptr- > tm_hour Minute = tmp_ptr- > tm_min; sec = tmp_ptr- > tm_sec; printf (date_time, "[% d-%d-%d% d:%d:%d]", year,month,day,hour,minute,sec); return 0 } after reading this, the article "how to get the current date and time in C language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.