Two ways of representing time date value time_t and struct TM type in C language

Source: Internet
Author: User

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 :



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


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 :



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_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",



return 0;



Two ways of representing time date value time_t and struct TM type in C language

Related Article

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: 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.