Time, we are dealing with it every day.
The world of programs is more, time is everywhere.
When you are writing a program, there are times when you need to get the current time and format the output, so on the whim, just think about sorting out some of the "time" functions.
However, it is important to emphasize that the functions described in this blog are in the C + + language.
Time function
Prototype:
timetimer);
Role:
Get the current calendar time as a value of type time_t.
The function returns this value, and if the argument isn't a null pointer, it also sets this value to the object pointed by a timer.
The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the current UNIX t Imestamp).
What is the timestamp here?
The Unix timestamp (Unix timestamp), or Unix time (Unix times), POSIX time, is a time representation defined as 00:00 GMT January 01, 1970, 00 seconds ( Beijing Time January 01, 1970 08:00 00 seconds) up to now the total number of seconds. Unix timestamps are used not only in UNIX systems, Unix-like systems (such as Linux systems), but also in many other operating systems.
Use:
#include <stdio.h>/* printf */#include < Time.h>/* time_t, struct TM, Difftime, Time, Mktime */intMain () {time_tTimer; struct TM Y2K = {0}; Double seconds; Y2k.tm_hour =0; Y2k.tm_min =0; Y2k.tm_sec =0; Y2k.tm_year = -; Y2k.tm_mon =0; Y2k.tm_mday =1; Time(&Timer); /*GetCurrent Time; Same as:Timer= Time(NULL) */seconds = Difftime (Timer, Mktime (&Y2K)); printf ("%.f seconds since January 1, the current timezone", seconds); Return0;}
Although libraries may use a different representation of time:portable programs should does use the value returned by this function directly, but all rely on calls to other elements of the standard library to translate them to portable types (such as localtime, Gmtime or Difftime).
localtime function
struct tm * localtime (const time_t * timer);
Role:
Uses the value pointed by timer to fill a TM structure with the values of that represent the corresponding time, expressed fo R the local timezone.
Application:
#include <stdio.h> /* puts, printf */#include <time.h> /* time_t, struct tm, time, localtime */int main (){ time_t rawtime; struct tm * timeinfo; time (&rawtime); localtime (&rawtime); printf ("Current local time and date: %s", asctime(timeinfo)); return0;}
gmtime function
Prototype:
struct tm * gmtime (const time_t * timer);
Role:
Convert time_t to TM as UTC time
Use:
#include <stdio.h>/* puts, printf * /#include <time.h>/* time_t, struct TM, time, Gmtime * /#define MST ( -7)#define UTC (0)#define CCT (+8)intMain () {time_t rawtime; struct TM * PTM; Time(&rawtime); PTM =gmtime(&rawtime); Puts ("Current Time around the world:");printf("Phoenix, AZ (U.S.): %2d:%02d\ n", (PTM->TM_HOUR+MST)%24, ptm->tm_min);printf("Reykjavik (Iceland): %2d:%02d\ n", (PTM->TM_HOUR+UTC)%24, ptm->tm_min);printf("Beijing (China): %2d:%02d\ n", (PTM->TM_HOUR+CCT)%24, ptm->tm_min);return 0;}
TM Structure
in standard C + +, we can obtain the date and time by the TM structure, which is defined in Time.h as follows:
#ifndef _tm_definedstructTM {intTm_sec;/ * seconds – The value interval is [0,59] * /intTm_min;/* min-value interval is [0,59] */intTm_hour;/* When-the value interval is [0,23] */intTm_mday;/* Date in one months-the value interval is [1,31] */intTm_mon;/ * Month (starting from January, 0 for January)-the value range is [0,11] * /intTm_year;/ * Year with a value equal to the actual year minus 1900 * /intTm_wday;/ * Week – The value interval is [0,6], where 0 stands for Sunday, 1 for Monday, and so on * /intTm_yday;/ * Number of days starting January 1 of each year – the value interval is [0,365], where 0 is January 1, 1 is January 2, and so on .intTM_ISDST;/ * Daylight Saving time identifier, the TM_ISDST is positive when daylight savings is applied. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */
strftime function
Prototype:
size_t strftime (charconstchar* format, conststruct tm* timeptr );
Role:
Format time As String
Specifier replaced byExample%a abbreviatedWeekday name* Thu%a FullWeekday name* Thursday%b abbreviatedMonth name* Aug%b FullMonth name* August%c Date and TimeRepresentation * Thu at -: -: Geneva 2001%c Year divided by - andTruncated to integer(xx- About) -%d Day of the Month, zero-padded ( on- to) at%d Short Mm/dd/yyDate, equivalent to%m/%d/%y ,/ at/ on%e Day of the Month,Space-padded (1- to) at%F Short Yyyy-mm-ddDate, equivalent to%y-%m-%d2001- ,- at%g week-based Year, LastDigits (xx- About) on%G week-based Year 2001%h abbreviatedMonth name* (same as%b) aug%h Hourinch -H Format (xx- at) -%I Hourinch AH Format ( on- A) Geneva%j Day of the Year(001-366)235%m Month asA decimal Number( on- A) ,%M Minute (xx- -) -%n New-linecharacter(' \ n ')%p AMorPM designation Pm%r A-hour Clock Time* Geneva: -: GenevaPm%r --hour hh:mm Time, equivalent to%h:%m -: -%s Second (xx- A) Geneva%t horizontal-Tab character(' \ t ')%T ISO8601 TimeFormat (HH:MM:SS), equivalent to%h:%m:%s -: -: Geneva%u ISO8601 Weekday as Number withMonday as 1(1-7)4%u Week Number with the FirstSunday as the First Day ofWeek One (xx- -) -%V ISO8601Week Number(xx- -) the%w Weekday asA decimal Number withSunday as 0(0-6)4%W Week Number with the FirstMonday as the First Day ofWeek One (xx- -) the%x Date Representation * ,/ at/ on%x Time Representation * -: -: Geneva%y year, LastDigits (xx- About) on%Y year2001%z ISO8601 Offset fromUtcinchTimeZone (1Minute=1,1Hour= -) If timezone cannot be determined, nocharacters+ -%Z Timezonename orAbbreviation * IF timezone cannot be determined, nocharacterscdt%% A% Sign%
The following, from the blog http://blog.csdn.net/shellching/article/details/8114266
time_t Tmbegin =1351118531;// --Ten- - .: the: Onetime_t tmend =1351218731;// --Ten- - Ten: +: Onetm* Ptmbegin =localtime(&tmbegin); tm* ptmend =localtime(&tmend);//The second call modifies the last called TM structure and loses//tm* if the last data is not saved PTM3 =gmtime(&tmend);//The same effect as the previous statement, also overrides the preceding data char ctmbegin1[ -], ctmend[ -];strftime (Ctmbegin, -,"%Y%m%d%H%m%s", Ptmbegin);//The output here will be the Tmend time value strftime (Ctmend, -,"%Y%m%d%H%m%s", ptmend);instructions for/*MSDN: Both the 32-bit and 64-bit versions ofgmtime, Mktime, Mkgmtime, and localtimeall use a single TM Structure per thread for the conversion. The these routines destroys the result of the previous call. */
So remember, once you call the LocalTime function, you should take out the contents of the TM structure immediately.
time_t Tmbegin =1351118531;// --Ten- - .: the: Onetime_t tmend =1351218731;// --Ten- - Ten: +: OneChar ctmbegin1[ -], ctmend[ -];tm* Ptmbegin =localtime(&tmbegin); Strftime (Ctmbegin, -,"%Y%m%d%H%m%s", ptmbegin); tm* ptmend =localtime(&tmend); Strftime (Ctmend, -,"%Y%m%d%H%m%s", ptmend);//tm* PTM3 =gmtime(&tmend);
Describes the functions of "time" in several C + + programs