YouTip LogoYouTip

C Standard Library Time H

Introduction

\n

time.h header file defines four variable types, two macros, and various functions for manipulating dates and times.

\n

<time.h> is a header file in the C standard library that provides functions and types for handling and manipulating dates and times. The functions in this header are used to get the current time, set time, format time, calculate time differences, and more.

\n\n

Constants & Macros

\n\n \n \n \n \n
Constant/MacroDescription
CLOCKS_PER_SECNumber of clock ticks per second.
NULLNull pointer constant.
TIME_UTCRepresents UTC time (C11).
\n\n

Library Variables

\n

The following are the variable types defined in the time.h header:

\n\n \n \n \n \n \n
No.Variable & Description
1size_t is an unsigned integer type, which is the result of the sizeof operator.
2clock_t is a type suitable for storing processor time.
3time_t is a type suitable for storing calendar time.
4struct tm is a structure used to store time and date.
\n

The tm structure is defined as follows:

\n
struct tm { int tm_sec; /* seconds, range from 0 to 59*/ int tm_min; /* minutes, range from 0 to 59*/ int tm_hour; /* hours, range from 0 to 23*/ int tm_mday; /* day of the month, range from 1 to 31*/ int tm_mon; /* month, range from 0 to 11*/ int tm_year; /* years since 1900*/ int tm_wday; /* day of the week, range from 0 to 6*/ int tm_yday; /* day of the year, range from 0 to 365*/ int tm_isdst; /* daylight saving time*/};
\n\n

Library Macros

\n

The following are the macros defined in the time.h header:

\n\n \n \n \n
No.Macro & Description
1NULL This macro holds the value of a null pointer constant.
2CLOCKS_PER_SEC This macro represents the number of processor clock ticks per second.
\n\n

Library Functions

\n

The following are the functions defined in the time.h header:

\n\n \n \n \n \n \n \n \n \n \n \n \n
No.Function & Description
1char *asctime(const struct tm *timeptr) Returns a pointer to a string representing the date and time stored in the timeptr structure.
2clock_t clock(void) Returns the amount of processor time used by the program since its execution (generally from the start).
3char *ctime(const time_t *timer) Returns a string representing local time, based on the timer argument.
4double difftime(time_t time1, time_t time2) Returns the difference in seconds between time1 and time2 (time1-time2).
5struct tm *gmtime(const time_t *timer) Breaks down the timer value into a tm structure and represents it in Coordinated Universal Time (UTC), also known as Greenwich Mean Time (GMT).
6struct tm *localtime(const time_t *timer) Breaks down the timer value into a tm structure and represents it in the local timezone.
7time_t mktime(struct tm *timeptr) Converts the structure pointed to by timeptr into a time_t value according to the local timezone.
8size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) Formats the time represented by the timeptr structure according to the rules defined in format, and stores it in str.
9time_t time(time_t *timer) Calculates the current calendar time and encodes it as a time_t object.
10int timespec_get(struct timespec *ts, int base); Gets the current time (C11).
\n\n

Examples

\n

The following are examples using some functions from <time.h>.

\n\n

Get Current Time:

\n

Example

\n
#include \n\n#include 
\n\n

Format Time:

\n

Example

\n
#include \n\n#include 
\n\n

Calculate Time Difference:

\n

Example

\n
#include \n\n#include 
\n\n

Using struct tm:

\n

Example

\n
#include \n\n#include 
\n\n

Notes

\n
    \n
  • When using localtime and gmtime, the returned pointer points to a statically allocated struct tm. Each call will overwrite the previous contents.
  • \n
  • When using the mktime function, ensure that all fields within the struct tm are valid.
  • \n
  • When using the strftime function for time formatting, ensure the destination buffer is large enough to avoid buffer overflow.
  • \n
\n

By understanding and using the functions provided by <time.h>, you can conveniently manipulate dates and times, thereby writing more feature-rich and efficient C programs.

← C Function CeilAv Event Suspend β†’