COHERENT manpages

This page displays the COHERENT manpage for mktime() [Turn broken-down time into calendar time].

List of available manpages
Index


mktime() -- Time Function (libc)

Turn broken-down time into calendar time
#include <time.h>
time_t mktime(timeptr)
struct tm *timeptr;

mktime() reads  broken-down time from  the structure pointed  to by timeptr
and converts it into calendar time of the type time_t. It does the opposite
of the  functions localtime() and  gmtime(), which turn  calendar time into
broken-down time.

mktime() manipulates the structure tm as follows:

1. It reads  the contents of the structure, but  ignores the fields tm_wday
   and tm_yday.

2. The original values of the other  fields within the tm structure are not
   restricted.   This allows  you,  for example,  to  increment the  member
   tm_hour  to discover  the calendar  time  one hour  hence, even  if that
   forces the value of tm_hour to be greater than 23, its normal limit.

3. When calculation  is completed, the  values of the fields  within the tm
   structure  are reset  to within  their normal limits  to conform  to the
   newly calculated  calendar time.  The value of tm_mday  is not set until
   after the values of tm_mon and tm_year.

4. The calendar time is returned.

If  the calendar  time  cannot be  calculated,  mktime returns  -1 cast  to
time_t.

Example

This example gets the date from the user and writes it into a tm structure.

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BAD_TIME ((time_t)-1)

/* ask for a number and return it. */
int askint(msg)
char *msg;
{
    char buf[20];

    printf("Enter %s ", msg);
    fflush(stdout);

    if(gets(buf) == NULL)
        exit(EXIT_SUCCESS);
    return(atoi(buf));
}

main()
{
    struct tm t;

    for(;;) {
        t.tm_mon  = askint("month") - 1;
        t.tm_mday = askint("day");
        t.tm_year = askint("year") - 1900;
        t.tm_hour = t.tm_min = t.tm_sec = 1;

        if(BAD_TIME == mktime(&t)) {
            printf("Invalid date\n");
            continue;
        }

        printf("Day of week is %d\n", t.tm_wday);
        break;
    }
    return(EXIT_SUCCESS);
}

See Also

clock(),
difftime(),
libc,
time [overview]
ANSI Standard, §7.12.2.3
POSIX Standard, §8.1

Notes

The above  description may appear  to be needlessly  complex.  However, the
Committee intended that mktime()  be used to implement a portable mechanism
for  determining  time  and  for  controlling time-dependent  loops.   This
function is needed  because not every environment describes time internally
as a multiple of a known time unit.