In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand the time function in C language". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Time expression and conversion functions are often used in programming. They are defined in the time.h library function. The basic form of the time function is time (). The prototype of this function is time_t time (time_t *time).
Several time concepts:
1:Coordinated Universal Time(UTC):
Coordinated Universal Time (UTC), also known as Universal Standard Time (GMT), is +8 UTC in mainland China and-5 UTC in the United States.
2:Calendar Time:
Calendar time is the time expressed as "seconds elapsed from a standard time point to this time." Standard time points may vary from compiler to compiler, but standard time is constant for a compiled system. Usually it is the number of seconds from UTC 1970-01-01 00:00:00.
3:epoch:
Time point. In standard c/c++, it is an integer expressed by the number of seconds (calendar time) between the time and the standard time point.
4:clock tick:
Clock timing unit (not called clock ticks), the length of a clock timing unit is controlled by the cpu, a clock tick is not a clock cycle of the cpu, but a basic timing unit of c/c++.
Definition of time.h
The time.h header file defines four variable types, two macros, and various functions that operate on dates and times.
4 variables
size_t is an unsigned integer type that is the result of the sizeof keyword. clock_t This is a suitable type for storing processor time, type unsigned longtime_t This is a suitable type for storing calendar time. struct tm This is a structure used to store time and date.
The definition of tm structure is as follows:
struct tm{ int tm_sec; /* seconds, ranging from 0 to 59 */ int tm_min; /* min, ranging from 0 to 59 */ int tm_hour; /* hours, ranging from 0 to 23 */ int tm_mday; /* Day of the month, ranging from 1 to 31 */ int tm_mon; /* Month, range 0 to 11(Note) */ int tm_year; /* Years since 1900 */ int tm_wday; /* Day of the week, ranging from 0 to 6 */ int tm_yday; /* Day of the year, ranging from 0 to 365 */ int tm_isdst; /* Daylight Saving Time */};
two macro
NULL This macro is the value of a null pointer constant. CLOCKS_PER_SEC This macro indicates the number of processor clocks per second. Used to convert the result of the clock() function into a quantity in seconds, the specific value of this quantity is operating system dependent, usually 1000.
library function
1: The clock function
Function prototype: clock_t clock(void)
Function returns: Returns the time used by the processor clock since the execution of the clock function (usually at the beginning of the program).
Function: Used to calculate the execution time of a program or a segment of a program.
examples
#include#includeint main(){ clock_t start_t,finish_t; double total_t = 0; int i = 0; start_t = clock(); for(;itm_hour+BST)%24, info->tm_min); printf("China: %2d:d\n", (info->tm_hour+CCT)%24, info->tm_min); return(0);}
8: mktime function
Function prototype: time_t mktime(struct tm *timeptr)
Function: converts the structure pointed to by timeptr into a time_t value according to the local time zone
Function Returns: This function returns a time_t value corresponding to the calendar time passed as an argument. If an error occurs, a value of-1 is returned.
examples
#include#includeint main (){ int ret; struct tm info; char buffer[80]; info.tm_year = 2001 - 1900; info.tm_mon = 7 - 1; info.tm_mday = 4; info.tm_hour = 0; info.tm_min = 0; info.tm_sec = 1; info.tm_isdst = -1; ret = mktime(&info); if( ret == -1 ) { printf("Error: Cannot convert time using mktime.\ n"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); print(buffer); } return(0);}
9: strftime function
function prototype: size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
Function: Place the time information stored in timeptr into the string pointed to by strDest according to the format command in the string pointed to by format, and store maxsize characters into strDest at most.
Parameter description: The conversion result is stored in s, and max. maxsize characters are written in s.
Function Returns: This function returns the number of characters placed into the string pointed to by strDest (excluding '\0'), or 0 if the number of characters exceeds maxsize.
examples
/*format is as follows: they are case-sensitive. %a Abbreviation for day of week %A Full name of day of week Abbreviations for %b month minutes %B Full name of month %c Time string of standard date %C Last two digits of year %d decimal day of month %D month/day/year %e Decimal day of month in two character fields %F Y-MM-DD %g Last two digits of year, using week-based year %G Year minutes, using week-based years Month name abbreviated by %h %H Hours of the 24-hour clock %I Hours of the 12-hour clock %j Decimal day of year %m Decimal month %M Minutes in ten-hour notation %n New line character %p local equivalent display of AM or PM %r 12 hours %R Display hours and minutes: hh:mm %S Seconds in decimal %t Horizontal tab %T Display hour minute: hh:mm:ss %u Day of the week, Monday is the first day (values from 0 to 6, Monday is 0) %U Week number of year, with Sunday as the first day (values 0 to 53) %V Week of year, using week-based year %w Decimal day of the week (values from 0 to 6, Sunday 0) %W Week of the year with Monday as the first day (values from 0 to 53) %x standard date string %X Standard Time String %y Decimal year without century (values from 0 to 99) %Y Decimal year with century component %z, %Z Time zone name, null character if time zone name is not available. %%% percent sign */ #include#includeint main (){ time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); strftime(buffer,80,"%Y%m%e_%H%M%S", info);//Represents the current time in MM/DD_HH/CS printf("%s\n", buffer ); return(0);}"How to understand the time function in C language" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.