Major operating system experiment progress (4) ----- how to obtain system time in C/C ++

Source: Internet
Author: User

// Solution-advantage: only the C standard library is used. disadvantage: The value can only be accurate to seconds. # include <time. h> # include <stdio. h> int main (void) {time_t = time (0); char tmp [64]; strftime (tmp, sizeof (tmp ), "% Y/% m/% d % X % A % j day % z this year", localtime (& t); puts (tmp); return 0 ;} size_t strftime (char * strDest, size_t maxsize, const char * format, const struct tm * timeptr); generate a string based on the format string. Struct tm * localtime (const time_t * timer); get the local time. the result obtained by localtime is returned by the structure tm. The returned string can be in the following format: % a abbreviation of the day of the week. Eg: Full name of Tue % A day of the week. Eg: Abbreviation of Tuesday % B month name. % B full name of the month name. % C the local date time is better than the string. % D indicates the day of the month (range: 00 to 31) with a number ). The date % H represents the hour in the 24-hour format (range: 00 to 23 ). % I represents the hour in 12-hour format (range: 01 to 12 ). % J indicates the day of the year (range: 001 to 366 ). The number of % m months (ranging from 1 to 12 ). % M minutes. % P uses ''am'' or ''pm ''to indicate the local time. % S seconds. % U indicates the week of the current year. The first week starts from the first Sunday. % W indicates the week of the current year. The first week starts from the first Monday. % W indicates the day of the week by number (0 indicates Sunday ). % X date representation without time. % X does not include the time representation of the date. Eg: 15:26:30% y two digits indicate the year (range: 00 to 99 ). % Y indicates the complete year number, that is, the four-digit number. Eg: 2008% Z (% z) Time Zone or abbreviation. Eg: Chinese Standard Time % characters. // Solution 2 advantages: accurate to milliseconds; disadvantage: windows API # include <windows. h> # include <stdio. h> int main (void) {SYSTEMTIME sys; GetLocalTime (& sys); printf ("% 4d/% 02d/% 02d % 02d: % 02d: % 02d. % 03d week % 1d/n ", sys. wYear, sys. wMonth, sys. wDay, sys. wHour, sys. wMinute, sys. wSecond, sys. wMilliseconds, sys. wDayOfWeek); return 0;} // method 3. Advantages: the system function can also be used to modify the system time. // the file must be a c ++ file # include <stdlib. h> # include <iostream> using namespace std; void m Ain () {system ("time") ;}// solution 4: Convert the current time to seconds, you can use the corresponding time conversion. // This file must be a c ++ file # include <iostream> # include <ctime> using namespace std; int main () {time_t now_time; now_time = time (NULL); cout <now_time; return 0;} 1. Obtain time: use the Time () function to obtain the Calendar time. Its prototype is: time_t time (time_t * timer); # include "stdafx. h "# include" time. h "# include" stdio. h "# include" stdlib. h "int main (void) {struct tm t; // defines the tm time structure, used to store the time Grid Format: time_t t_of_day; // defines the time_t time structure t. tm_year = 2006-1900; // calculate the time t based on January 1, 1900. tm_mon = 6; // assign a value to the struct t. tm_mday = 1; t. tm_hour = 0; t. tm_min = 0; t. tm_sec = 1; t. tm_isdst = 0; t_of_day = mktime (& t); // use the mktime () function to convert the time represented in the tm structure to a calendar time: time_t variable. The function prototype is as follows: time_t mktime (struct tm * timeptr); The ctime () function (the parameter is in the time_t structure) displays the time in a fixed format, the returned value is a char * string. Return 0;} 2. Storage of Time, which is stored through two predefined structures: 1. The Calendar Time is represented by the time_t data type, the time (calendar time) represented by time_t is the number of seconds from a time point (for example, January 1, 1970 00:00:00) to this time point. In time. h, we can also see that time_t is a long integer: # ifndef _ TIME_T_DEFINEDtypedef long time_t; /* Time Value */# define _ TIME_T_DEFINED/* avoid repeatedly defining time_t */# endif2. In Standard C/C ++, we can use the tm structure to obtain the date and time. The tm structure is in time. the definition in h is as follows: struct tm {int tm_sec;/* Second-value range: [0, 59] */int tm_min; /* minute-value range: [] */int tm_hour;/* Time-value range: [] */int tm_mday; /* date in a month-value range: [1, 31] */int tm_mon;/* month (starting from January 1, January, 0 indicates January 1, January) -The value range is [0, 11] */int tm_year;/* year The value is equal to the actual year minus 1900 */int tm_wday;/* the week-value range is [], where 0 represents Sunday, 1 represents Monday, and so on */int tm_yday; /* The number of days starting from January 1, 0,365-the value range is []. 0 indicates January 1, and so on. */int tm_isdst;/* indicates the identifier of the hour, when the sequence is executed, tm_isdst is positive. When the sequence is not implemented, the tm_isdst value is 0. If the sequence is unknown, the tm_isdst () value is negative. */}; 3. display of time. the h header file provides the asctime () function (the parameter is a tm structure pointer) and ctime () function (the parameter is a time_t structure) to display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week, month, hour, minute, second, year, n, 0, time. h also provides two different functions to convert the calendar time (an integer expressed in time_t) to the time format that we usually see separately displaying year, month, day, hour, minute, and second. tm: struct tm * gmtime (const time_t * timer); The gmtime () function converts the calendar time to the world's Standard Time (GMT ), returns a tm struct to save the time struct tm * localtime (const time_t * timer). The localtime () function converts the calendar time to the local time # include <stdafx. h> # include <time. h> # include <stdio. h> # include <stdlib. h> int main (void) {struct tm * local, * ptr; // defines the tm structure pointer to store the time information time_t ;// Time structure or object t = time (NULL); // obtain the current system Calendar Time // use the time () function to obtain the Calendar time ), // Its prototype is time_t time (time_t * timer); local = localtime (& t); // localtime () the function is to convert the calendar time to the Local time printf ("local hour is: % d/n", Local-> tm_hour ); // printf ("UTC hour is: % d/n", local-> tm_hour); // local = gmtime (& t ); // The gmtime () function converts the calendar time to the world standard time (Greenwich Mean Time). // it returns a tm struct to save the time ptr = gmtime (& t ); // convert The calendar time to The world standard time printf ("The UTC time is % s/n", asctim E (ptr); // format and output The world standard time printf ("The local time is % s/n", ctime (& t )); // output the local time/* asctime () function (the parameter is a tm structure pointer) and ctime () function (the parameter is a time_t structure) to display the time in a fixed format, both return values are char * strings. The returned time format is: day of the week Date Hour: minute: Second year/n/0 */return 0;} 4. Time Difference Calculation: function used: the timing function in C/C ++ is clock (), and the data type associated with it is clock_t. The clock function is defined as follows in MSDN: clock_t clock (void); the function returns from "enable this program process" to "program call clock () the number of clock units (clock tick) between functions. clock_t is a long integer and the Data Type of the storage time. In time. in the H file, a constant CLOCKS_PER_SEC is also defined to indicate the number of clock units in a second. Its definition is as follows: # define CLOCKS_PER_SEC (clock_t) 1000) every 1‰ seconds (1 millisecond), the value returned by calling the clock () function is increased by 1, and the length of the clock timing unit is 1 millisecond, so the timing accuracy is also 1 millisecond, can we change the definition of CLOCKS_PER_SEC to a greater value, so that the timing accuracy is higher? This is not acceptable. In standard C/C ++, the minimum unit of time is one millisecond. Double difftime (time_t time1, time_t time0); this function is used to calculate the time difference. # Include "stdafx. h "# include" time. h "# include" stdio. h "# include" stdlib. h "int main (void) {time_t c_start, t_start, c_end, t_end; c_start = clock (); t_start = time (NULL); system (" pause "); c_end = clock (); t_end = time (NULL); printf ("The pause used % f The MS by time (). /n ", difftime (c_end, c_start); printf (" The pause used % f s by clock (). /n ", difftime (t_end, t_start); system (" pause "); return 0 ;}5, other use of time As the seed of a random number, because the time is actually a long integer of the double type, obtained through the time (NULL) function, as srand (time (NULL )) is better. # Include "stdafx. h "# include" time. h "# include" stdio. h "# include" stdlib. h "int main (void) {srand (time (NULL); // sets the seed. If you comment out this function, for (int I = 0; I <100; I ++) {printf ("% d/t", rand ());} system ("pause"); return 0;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.