time.h in C

The file header named time.h in Standard Library C defines four variable types, two macros and various functions for date and time operations.

The file header named time.h in Standard Library C defines four variable types, two macros and various functions for date and time operations.

Variables are defined in time.h

The following lists some types of variables defined in time.h:

Description_t variable size_t

This is an unsigned integer type and the result of the sizeof keyword

clock_t

This is an appropriate type to store Processor time.

time_t is

This is an appropriate type to store Calendar time.

struct tm

This is a structure used to keep date and time.

Structure tm has the following definition:

struct tm { int tm_sec; /* biểu diễn giây, từ 0 tới 59 */ int tm_min; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst; /* biểu diễn Daylight Saving Time */ }; 

Macros are defined in time.h

The table below lists some macros defined in time.h:

  1. NULL: This macro is the value of a null pointer constant
  2. CLOCKS_PER_SEC: This macro represents the clock rate per second (Processor Clock per Second).

Functions are defined in time.h

Here are some functions defined in time.h:

STAMP & Description1

Char * asctime function (const struct tm * timeptr)

Returns a pointer to a string representing the date and time of the structure timeptr

2

Clock_t clock function (void)

Returns the processor clock used from the start of a deployer (usually at the beginning of the program)

3

Char * ctime function (const time_t * timer)

Returns a string representing localtime based on the timer parameter

4

Double difftime function (time_t time1, time_t time2)

Returns the number of seconds between time1 and time2 (ie time1 - time2).

5

Struct tm * gmtime (const time_t * timer)

The value of timer is divided into structure tm and represented in UTC, or GMT

6

Struct tm * localtime (const time_t * timer)

Timer value is divided into structure tm and represented in Local Timezone

7

Function time_t mktime (struct tm * timeptr)

Convert the structure pointed to by timeptr into a time_t value according to Local Timezone

8

Size_t strftime function (char * str, size_t maxsize, const char * format, const struct tm * timeptr)

Time format represented in the structure timeptr according to the formatting rules defined in format and stored in str

9

Time_t time function (time_t * timer)

Estimate the current Calendar time and encode it in time_t format

Asctime () function in C

Char * asctime function (const struct tm * timeptr) Returns a pointer to a string representing the date and time of the structure struct timeptr .

Declare asctime () function in C:

Below is the declaration for asctime () in C:

 char * asctime ( const struct tm * timeptr ) 

Parameters:

The timeptr parameter is a pointer to structure tm that contains a Calendar time broken down into the following components:

 struct tm { int tm_sec ; /* biểu diễn giây, từ 0 tới 59 */ int tm_min ; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour ; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday ; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon ; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year ; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday ; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday ; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst ; /* biểu diễn Daylight Saving Time */ }; 

Return value:

This function returns a string containing date and time information in a human format that can be read Www Mmm dd hh: mm: ss : here Www is the day of the week, Mmm are the month characters, dd is the date of month, hh: mm: ss is time and yyy is the year.

For example:

The following C program illustrates how to use asctime () in C:

 #include #include #include int main () { struct tm t ; /* TipsMake.com */ t . tm_sec = 15 ; t . tm_min = 16 ; t . tm_hour = 6 ; t . tm_mday = 18 ; t . tm_mon = 6 ; t . tm_year = 118 ; t . tm_wday = 5 ; puts ( asctime (& t )); return ( 0 ); } 

Compiling and running the above C program will result:

 Fri Jul 18 06:16:15 2018 

The clock () function in C

The function clock_t clock (void) Returns the number of ticks the clock has passed since the program was run. To get the number of seconds used by the CPU, you will need to divide by CLOCKS_PER_SEC.

On a 32-bit operating system, CLOCKS_PER_SEC equals 1000000, which returns the same value every 72 minutes.

Declare the clock () function in C:

Here is the declaration for the clock () in C:

clock_t clock(void) 

Parameters:

  1. This function does not take any parameters.

Return value:

This function returns the number of ticks the clock has passed since the program was run. If it fails, the function returns -1.

For example:

The following C program illustrates the usage of clock () in C:

#include #include int main() { clock_t start_t, end_t, total_t; int i; start_t = clock(); printf("Bat dau chuong trinh, start_t = %ldn", start_t); printf("Quet qua mot vong lap lon, start_t = %ldn", start_t); for(i=0; i< 10000000; i++) { } end_t = clock(); printf("Ket thuc vong lap, end_t = %ldn", end_t); total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; printf("Tong thoi gian su dung boi CPU: %fn", total_t ); printf("Thoat chuong trinh.n"); return(0); } 

Running the above code we get the following result:

 Start-up, start_t = 884 
Through a series of events, start_t = 884
End of lap, end_t = 32819
Time to use the CPU: 0.000000
Removal of virgin hair .

Function ctime () in C

The char * ctime function (const time_t * timer) returns a string representing localtime based on the timer parameter

The return string has the following format: Www Mmm dd hh: mm: ss where Www is the day of the week, Mmm is the month characters, dd is the day of the month, hh: mm: ss is the time and yy is the year .

Declare the ctime () function in C:

Below is the declaration for ctime () in C:

char *ctime(const time_t *timer) 

Parameters:

  1. timer - This is a pointer to a time_t object that contains a Calendar time.

Return value:

This function returns a string containing date and time information in a human readable format.

For example:

The following C program illustrates the usage of ctime () in C:

#include #include int main () { time_t curtime; time(&curtime); printf("Thời gian hiện tại = %s", ctime(&curtime)); return(0); } 

Compiling and running the above C program will result:

 Current time = Mon Oct 15 04:49:13 2018 

The function of difftime () in C

Double difftime function (time_t time1, time_t time2) returns the number of seconds between time1 and time2 , for example, (time1 - time2) . Two times are defined in Calendar time, representing the time that has elapsed from Epoch (00:00:00 1/1/19700 according to UTC).

Declaring the difftime () function in C:

Below is the declaration for difftime () in C:

 double difftime ( time_t time1 , time_t time2 ) 

Parameters:

  1. time1 - This is the time_t object for the end time.

  2. time2 - This is the time_t object for the start time.

Return value:

This function returns the number of different seconds between time (time2 - time1) as a double value.

For example:

The following C program illustrates the usage of difftime () in C:

 #include #include int main () { time_t start_t , end_t ; double diff_t ; printf ( "Bắt đầu chương trình.n" ); time (& start_t ); time (& end_t ); diff_t = difftime ( end_t , start_t ); printf ( "Thời gian thực thi = %fn" , diff_t ); printf ( "Thoát chương trình.n" ); return ( 0 ); } 

Compiling and running the above C program will result:

 Start the program . 
Execution time = 0.000000
Exit the program .

Function gmtime () in C

Struct tm * gmtime (const time_t * timer) uses the value pointed to by the timer to fill a tm structure with values ​​that represent the corresponding time, expressed in UTC or GMT.

Declare the function gmtime () in C:

Below is the declaration for gmtime () in C:

struct tm *gmtime(const time_t *timer) 

Parameters:

  1. timeptr - This is the pointer to a time_t value representing a Calendar time.

Return value:

This function returns the pointer to tm structure with the time information filled in. Below is the details of the structure timeptr.

struct tm { int tm_sec; /* biểu diễn giây, từ 0 tới 59 */ int tm_min; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst; /* biểu diễn Daylight Saving Time */ }; 

For example:

The following C program illustrates the usage of gmtime () in C, you can refer to the list of time zones here:

#include #include #define BST (+1) #define CCT (+8) int main () { time_t rawtime; struct tm *info; time(&rawtime); /* Get GMT time */ info = gmtime(&rawtime ); printf("Thời gian hiện tại:n"); printf("Tại London: %2d:%02dn", (info->tm_hour+BST)%24, info->tm_min); printf("Tại Trung Quốc: %2d:%02dn", (info->tm_hour+CCT)%24, info->tm_min); return(0); } 

Compiling and running the above C program will result:

 The present time: 
In London: 5:51
In China: 12:51

Function localtime () in C

Struct tm * localtime (const time_t * timer) uses the time pointed to by the timer to fill a tm structure with values ​​that represent the corresponding local time. The timer value is divided into tm structure and expressed in Local Timezone.

Declare localtime () function in C:

Below is the declaration for localtime () in C:

struct tm *localtime(const time_t *timer) 

Parameters:

  1. timer - A pointer to a time value representing a calendar time.

Return value:

This function returns the pointer to tm structure with the time information filled in. Below is the details of the structure timeptr.

struct tm { int tm_sec; /* biểu diễn giây, từ 0 tới 59 */ int tm_min; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst; /* biểu diễn Daylight Saving Time */ }; 

For example:

The following C program illustrates the usage of localtime () in C:

#include #include int main () { time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); printf("Local time và Local date hiện tại là: n%s", asctime(info)); return(0); } 

Compiling and running the above C program will result:

 Current local time and Local date are: 
Mon Oct 15 11:56:31 2018

Function mktime () in C

The time_t mktime function (struct tm * timeptr) converts the structure pointed to by timeptr into a time_t value according to Local Timezone.

Declare the function mktime () in C:

Below is the declaration for mktime () in C:

time_t mktime(struct tm *timeptr) 

Parameters:

  1. timeptr - is a pointer to a time value representing a calendar time, which is broken down into components with the structure:

struct tm { int tm_sec; /* biểu diễn giây, từ 0 tới 59 */ int tm_min; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst; /* biểu diễn Daylight Saving Time */ }; 

Return value:

This function returns the time_t value corresponding to the passed calendar time parameter. If there is an error, this function returns -1.

For example:

The following C program illustrates the usage of mktime () in C:

#include #include int main () { int ret; struct tm info; char buffer[80]; info.tm_year = 2016 - 1900; info.tm_mon = 7 - 1; info.tm_mday = 4; info.tm_hour = 0; info.tm_min = 0; info.tm_sec = 1; info.tm_isdst = -1; ret = mktime(&info); if( ret == -1 ) { printf("Error: không thể lấy time bằng cách sử dụng mktimen"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); printf(buffer); } return(0); } 

Compiling and running the above C program will result:

 Mon Jul 4 00:00:01 2016 

The strftime function () in C

Size_t strftime function (char * str, size_t maxsize, const char * format, const struct tm * timeptr) time format is represented in the structure timeptr according to the formatting rules defined in format and stored in str.

Declare the strftime function () in C:

Below is the declaration for strftime () in C:

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) 

Parameters:

  1. str - This is the pointer to the destination array, where the result string is copied.

  2. maxsize - This is the maximum number of characters to be copied to str.

  3. format - This is a string containing any combination of regular characters and special format specifiers. These format specifiers are replaced by this function by the corresponding values ​​to represent the time specified in tm. The format specifier is:

Specifier Replacement for Example% a Abbreviated week name Sun% A Full weekday name Sunday% b Abbreviated name Mar% B Full month name March% c Perform date and time Sun Aug 19 02:56: 02 2012% d Date of the month (01-31) 19% H Hours, in the format 24 (00-23) 14% I Hours, in 12h (01-12) format 05% j Date of the year (001-366) ) 231% m Months, in the form of numbers (01-12) 08% M Minutes (00-59) 55% p AM or PM PM% S Seconds (00-61) 02% U Number of weeks, Sunday The first is the first day of the week (00-53) 33% w The day of the week as a representation of numbers (0-6) 4% W The number of weeks, the first Monday is the first day of the week (00- 53) 34% x Performing date 08/19/12% X Performing time 02:50:06% y Year, represented as two last numbers (00-99) 01% Y Year 2012% Z Timezone name CDT% % Symbol % %
  1. timeptr - is a pointer to a time value representing a calendar time, which is broken down into components with the structure:

struct tm { int tm_sec; /* biểu diễn giây, từ 0 tới 59 */ int tm_min; /* biểu diễn phút, từ 0 tới 59 */ int tm_hour; /* biểu diễn giờ, từ 0 tới 23 */ int tm_mday; /* biểu diễn ngày của tháng, từ 1 tới 31 */ int tm_mon; /* biểu diễn tháng, từ 0 tới 11 */ int tm_year; /* biểu diễn năm, bắt đầu từ 1900 */ int tm_wday; /* ngày trong tuần, từ 0 tới 6 */ int tm_yday; /* ngày trong năm, từ 0 tới 365 */ int tm_isdst; /* biểu diễn Daylight Saving Time */ }; 

Return value:

If the result string is smaller than the size of the characters (including the ending null character), the entire character copied to str (excluding the ending null character) is returned. If not, the function returns 0.

For example:

The following C program illustrates the usage of strftime () in C:

#include #include int main () { time_t rawtime; struct tm *info; char buffer[80]; time( &rawtime ); info = localtime( &rawtime ); strftime(buffer,80,"%x - %I:%M%p", info); printf("Date & time đã định dạng theo hàm strftime là: n|%s|n", buffer ); return(0); } 

Compiling and running the above C program will result:

 Date & time has been formatted according to strftime function: 
| 10/15/18 - 04:58 AM|

The time () function in C

The time_t time function (time_t * seconds) returns the time from Epoch (00:00:00 1/1/1970 according to UTC), estimated in seconds. If the parameter seconds is not NULL, the return value is also stored in the seconds variable.

Declaring the time () function in C:

Below is the declaration for time () in C:

time_t time(time_t *t) 

Parameters:

  1. seconds - This is the pointer to an object of type time_t, where the seconds value will be stored.

Return value:

The function returns the current Calendar time as a time_t object.

For example:

The following C program illustrates the usage of time () in C:

#include #include int main () { time_t seconds; seconds = time(NULL); printf("Số giờ (h) bắt đầu từ 1/1/1970 = %ld giờn", seconds/3600); return(0); } 

Compiling and running the above C program will result:

 The number of hours (h) starts from January 1, 1970 = 427660 hours 

 

5 ★ | 1 Vote