Transferred from: http://blog.csdn.net/educast/article/details/17239735
1, the common time storage method
- 1) Thetime_t type, which is essentially a long integer, represents the number of seconds from 1970-01-01 00:00:00 to the current time elapsed, and if more precise is needed, the timeval can be used precisely to milliseconds.
- 2) TheTM structure, which is essentially a struct, contains the time fields
1 structTM {2 intTm_sec;/*seconds after the minute-[0,59]*/ 3 intTm_min;/*minutes after the hour-[0,59]*/ 4 intTm_hour;/*hours since midnight-[0,23]*/ 5 intTm_mday;/*Day of the month-[1,31]*/ 6 intTm_mon;/*months since January-[0,11]*/ 7 intTm_year;/*years since 1900*/ 8 intTm_wday;/*Days since Sunday-[0,6]*/ 9 intTm_yday;/*Days since January 1-[0,365]*/ Ten intTM_ISDST;/*Daylight Savings Time Flag*/ One};
Where tm_year represents the number of years from 1900 to the current time interval, if the value is set manually, TM_ISDST usually takes a value of-1.
2, the time function commonly used
1time_t time (time_t *t);//gets the number of seconds since January 1, 19702 Char*asctime (Const structTM *tm);//converts information in a structure to real-world time, as a string3 Char*ctime (Consttime_t *TIMEP);//convert TIMEP to True world time, shown in string, it differs from asctime in that the Parameter form passed in is different4 structTM *gmtime (Consttime_t *TIMEP);//converts the time represented by time_t to UTC time without time zone conversion, which is a struct TM structure pointer5 structTM *localtime (Consttime_t *TIMEP);//similar to Gmtime, but it is time-zone-converted. 6time_t Mktime (structTM *tm);//converts the time of the struct TM structure to the number of seconds from 1970 to the present7.intGettimeofday (structTimeval *tv,structTimeZone *tz);//returns the number of seconds and subtleties of the current distance of 1970, and the following TZ is the time zone, typically not8 DoubleDifftime (time_t time1, time_t time2);//returns the number of seconds between two time differences
3. Conversion of time and string
The header files you need to include are as follows:
1 #include <iostream> 2 #include <time.h> 3 #include <stdlib.h> 4 #include <string.h>
1) Unix/windows time-to-string reference code
1time_t T;//seconds Time2Tm* Local;//local time3tm* GMT;//GMT4 Charbuf[ -]= {0}; 5 6T = time (NULL);//get current seconds time7Local = LocalTime (&t);//Convert to local time8Strftime (BUF, -,"%y-%m-%d%h:%m:%s", local); 9Std::cout << buf <<Std::endl; Ten OneGMT = Gmtime (&t);//To Greenwich mean AStrftime (BUF, -,"%y-%m-%d%h:%m:%s", GMT); -Std::cout << buf << Std::endl;
2) Unix string turn Time reference code
1 TM Tm_; 2 time_t T_; 3 Charbuf[ -]= {0}; 4 5strcpy (BUF,"2012-01-01 14:00:00"); 6Strptime (BUF,"%y-%m-%d%h:%m:%s", &tm_);//Convert a string to a TM time7TM_.TM_ISDST =-1; 8T_ = Mktime (&tm_);//convert TM time to seconds9T_ + =3600;//number of seconds plus 3600Ten OneTm_ = *localtime (&t_);//Output Time AStrftime (BUF, -,"%y-%m-%d%h:%m:%s", &tm_); -Std::cout << buf << Std::endl;
3) because there is no strptime function under Windows, you can use scanf to format
1time_t Stringtodatetime (Char*str)2 { 3 TM Tm_; 4 intYear , month, day, hour, Minute,second; 5SSCANF (str,"%d-%d-%d%d:%d:%d", &year, &month, &day, &hour, &minute, &second); 6Tm_.tm_year = year-1900; 7Tm_.tm_mon = month-1; 8Tm_.tm_mday =Day ; 9Tm_.tm_hour =hour; TenTm_.tm_min =minute; OneTm_.tm_sec =second; ATM_.TM_ISDST =0; - -time_t t_ = mktime (&tm_);//has lost 8 time zones. the returnT_;//seconds Time -}
C + + time and string conversions