In the project, we often need to get the current time of the system, then how to get it, see the following code:
/*Asctime Example*/#include<stdio.h>/*printf*/#include<time.h>/*time_t, struct TM, time, localtime, Asctime*/intMain () {time_t rawtime; structTM *Timeinfo; Time (&rawtime); Timeinfo= LocalTime (&rawtime); printf ("The current Date/time is:%s", Asctime (Timeinfo)); return 0;}
The output is:
is - : £º
However, in some cases we want to get the value of the month and the minute, instead of getting an entire string like this, so since the time information has a struct timeinfo, let's take a look at the definition of this struct:
structTM {intTm_sec;//seconds of minutes from 0 to intTm_min;//minutes of hour from 0 to intTm_hour;//hours of day from 0 to intTm_mday;//Day of month from 1 to intTm_mon;//month of year from 0 to one intTm_year;//Year since 1900 intTm_wday;//Days since Sunday intTm_yday;//Days since January 1st intTM_ISDST;//hours of daylight savings Time}
Well, after reading the definition of the struct, let's take a look at how the Asctime () function takes out the desired data and converts it:
Char* Asctime (Const structTM *timeptr) { Static Const Charwday_name[][4] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" }; Static Const Charmon_name[][4] = { "Jan","Feb","Mar","APR"," May","June", "Jul"," the","Sep","Oct","Nov","Dec" }; Static Charresult[ -]; sprintf (Result,"%.3s%.3s%3d%.2d:%.2d:%.2d%d\n", Wday_name[timeptr-Tm_wday], mon_name[timeptr-Tm_mon], timeptr->tm_mday, timeptr->Tm_hour, Timeptr->tm_min, timeptr->Tm_sec,1900+ timeptr->tm_year); returnresult;}
If we print the date and time of the timeinfo structure directly, we will get:
the 1 - One $ Panax Notoginseng
And what we need is:
- 2 - One $ Panax Notoginseng
Careful observation of the asctime () function , we can find that year of preservation is since 1900, so to +1900, the month is to put the name shorthand into an array, the array starting from 0, so to get the number of the month, need +1, the back of the day and seconds no change, can be used directly.
To get a string like 20150226114637 minutes and seconds, use the following function:
STD::stringGettimestamp () {time_t rawtime; structTM *Timeinfo; Time (&rawtime); Timeinfo= LocalTime (&rawtime); Charyear[5], mon[3], mday[3], hour[3], minute[3], sec[3]; sprintf (Year,"%d", Timeinfo->tm_year +1900); sprintf (Mon,"%d", Timeinfo->tm_mon +1); sprintf (Mday,"%d", timeinfo->tm_mday); sprintf (Hour,"%d", timeinfo->tm_hour); sprintf (Minute,"%d", timeinfo->tm_min); sprintf (sec,"%d", timeinfo->tm_sec); STD::stringYEARSTR = std::string(year); STD::stringMONSTR = std::string(Mon); if(monstr.size () = =1) Monstr ="0"+Monstr; STD::stringMDAYSTR = std::string(Mday); if(mdaystr.size () = =1) Mdaystr ="0"+Mdaystr; STD::stringHOURSTR = std::string(hour); if(hourstr.size () = =1) Hourstr ="0"+Hourstr; STD::stringMINUTESTR = std::string(minute); if(minutestr.size () = =1) Minutestr ="0"+Minutestr; STD::stringSECSTR = std::string(SEC); if(secstr.size () = =1) Secstr ="0"+Secstr; returnYearstr + monstr + mdaystr +Hourstr+ Minutestr +secstr;}
C + + Get current time get present