mmkkttiimmee() -- Time Function (libc)

Turn broken-down time into calendar time
#iinncclluuddee <ttiimmee.hh>
ttiimmee_tt mmkkttiimmee(_t_i_m_e_p_t_r)
ssttrruucctt ttmm *_t_i_m_e_p_t_r;

mmkkttiimmee() reads  broken-down time from  the structure pointed  to by _t_i_m_e_p_t_r
and converts it into calendar time of the type ttiimmee_tt. It does the opposite
of the  functions llooccaallttiimmee() and  ggmmttiimmee(), which turn  calendar time into
broken-down time.

mmkkttiimmee() manipulates the structure ttmm as follows:

11. It reads  the contents of the structure, but  ignores the fields ttmm_wwddaayy
   and ttmm_yyddaayy.

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

33. When calculation  is completed, the  values of the fields  within the ttmm
   structure  are reset  to within  their normal limits  to conform  to the
   newly calculated  calendar time.  The value of ttmm_mmddaayy  is not set until
   after the values of ttmm_mmoonn and ttmm_yyeeaarr.

44. The calendar time is returned.

If  the calendar  time  cannot be  calculated,  mmkkttiimmee returns  -1 cast  to
ttiimmee_tt.

_E_x_a_m_p_l_e
This example gets the date from the user and writes it into a ttmm structure.

#include <math.h>
#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");
        t.tm_mday = askint("day");
        t.tm_year = askint("year");
        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);
}

_S_e_e _A_l_s_o
cclloocckk(), ddiiffffttiimmee(), lliibbcc, ttiimmee [oovveerrvviieeww]
ANSI Standard, section 7.12.2.3
POSIX Standard, section 8.1

_N_o_t_e_s
The above  description may appear  to be needlessly  complex.  However, the
Committee intended that mmkkttiimmee()  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.
