Use gmtime function or localtime function will time_t the type's time date is converted to Structtm type :
The time function is used to return a long value that is not meaningful to the user, and it is generally not possible to determine specific year, month, and day data based on its value. The gmtime function can easily convert the time_t type data into the data of the TM structure for easy data reading.
The gmtime function is prototyped as follows :
struct TM *gmtime (TIME_T*TIMEP);
The localtime function is prototyped as follows :
struct TM *localtime (TIME_T*TIMEP);
Converts the time_t type information referred to by the parameter TIMEP to the actual time date representation used, returning the result to a variable of the structure's TM structure type.
The structure variable that the gmtime function uses to hold the actual datetime is statically allocated, and each call to the gmtime function overrides the struct variable. If you want to save the contents of a structure variable, you must copy it to another variable in the TM structure.
gmtime function and localtime the difference between functions :
The time-date returned by the gmtime function is not converted by the time zone, which is UTC time ( also known as world time, or GMT ).
The localtime function returns the time of the current time zone,
Convert Date-time representation time_t type is converted to Structtm Type Example :
#include
#include
int main ()
{
char*wday[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};/* pointer character array * /
time_t T;
struct TM *p;
T=time (NULL);/* gets the number of seconds from 1970 , 1 months, 1 days, 0 o'clock to now, saved in the variable t * /
P=gmtime (&t);/* The value of the variable T is converted to the representation format of the actual date time * /
printf ("%d years %02d month %02d Day ", (1900+p->tm_yeaR), (1+p->tm_moN), P->tm_mday);
printf ("%s", Wday[p->tm_wday]);
printf ("%02d:%02d:%02d\n", P->tm_hour,p->tm_min, p->tm_sec);
return 0;
}
Note:p=gmtime (&t); If this line is changed to P=localtime (&t); Returns the time of the current time zone
② Use the mktime function to convert a time date of type Structtm to time_ type t :
Table header File
#include
Defining functions
time_t mktime (Strcut TM *timeptr);
Function description
mktime () is used to convert the TM structure data referred to by the parameter timeptr from A.D. 1970 Year 1 months 1 days 0 the number of seconds elapsed since the UTC time of 0 minutes and 0 seconds.
return value
Returns the number of seconds elapsed.
example of conversion of date to seconds :
#include
#include
int main ()
{
time_t T;
struct TM STM;
printf (" Please enter datetime value ( by yyyy/mm/dd hh:mm:ss format ):");
scanf ("%d/%d/%d%d:%d:%d", &stm.tm_year,&stm.tm_mon,&stm.tm_mday,
&STM.TM_HOUR,&STM.TM_MIN,&STM.TM_SEC);
stm.tm_year-=1900;/* year value minus 1900to get the year ordinal saved in the TM structure * /
stm.tm_mon-=1; /* month value minus 1to get the ordinal number of months saved in the TM structure * /
T=mktime (&stm); /* If the user enters a wrong datetime, the function return value is -1*/
if ( -1==t)
{
printf (" Input date Time format error !\n");
Exit (1);
}
printf ("1970/01/01 00:00:00~%d/%02d/%02d%02d:%02d:%02d Total %d seconds \ n",
Stm.tm_year+1900,stm.tm_mon,stm.tm_mday,
STM.TM_HOUR,STM.TM_MIN,STM.TM_SEC,T);
return 0;
}
ext.:http://www.360doc.com/content/11/0720/14/1317564_134702417.shtml
Two ways of representing time date value time_t and struct TM type in C language