Handling Time in C with the <time.h> library

time.h is a header file defined in the C Standard Library that contains time and date structures and function declarations to provide standardized access to time/date manipulation and formatting.

It defines four variable types, two macro and various functions for manipulating date and time.

Below are the variable types defined in the library:

size_t This is the unsigned integral type and is the result of the sizeof keyword.
clock_t This is a type suitable for storing the processor time.
time_t This is a type suitable for storing the calendar time.
struct tm This is a structure used to hold the time and date.

struct tm for Broken-Down Time

Declared in <time.h>

struct tm {
  int  tm_sec;    /* Seconds          [0, 60] */
  int  tm_min;    /* Minutes          [0, 59] */
  int  tm_hour;   /* Hour             [0, 23] */
  int  tm_mday;   /* Day of the month [1, 31] */
  int  tm_mon;    /* Month            [0, 11]  (January = 0) */
  int  tm_year;   /* Year minus 1900 */
  int  tm_wday;   /* Day of the week  [0, 6]   (Sunday = 0) */
  int  tm_yday;   /* Day of the year  [0, 365] (Jan/01 = 0) */
  int  tm_isdst;  /* Daylight savings flag */

  long        tm_gmtoff; /* Seconds East of UTC */
  const char *tm_zone;   /* Timezone abbreviation */
};


        


      

These are the macros defined in the header time.h

NULL This macro is the value of a null pointer constant.
CLOCKS_PER_SEC This macro represents the number of processor clocks per second.

And here are the functions for handling time:

char *asctime(const struct tm *timeptr) Returns a pointer to a string which represents the day and time of the structure timeptr.
clock_t clock(void) Returns the processor clock time used since the beginning of an implementation defined era (normally the beginning of the program).
char *ctime(const time_t *timer) Returns a string representing the localtime based on the argument timer.
double difftime(time_t time1, time_t time2) Returns the difference of seconds between time1 and time2 (time1-time2).
struct tm *gmtime(const time_t *timer) The value of timer is broken up into the structure tm and expressed in Coordinated Universal Time (UTC) also known as Greenwich Mean Time (GMT).
struct tm *localtime(const time_t *timer) The value of timer is broken up into the structure tm and expressed in the local time zone.
time_t mktime(struct tm *timeptr) Converts the structure pointed to by timeptr into a time_t value according to the local time zone.
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) Formats the time represented in the structure timeptr according to the formatting rules defined in format and stored into str.
time_t time(time_t *timer) Calculates the current calender time and encodes it into time_t format.
size_t wcsftime( wchar_t* str, size_t count, const wchar_t* format, const struct tm* time ) Converts a tm object to custom wide string textual representation.