Beispiel #1
0
/**
 * \brief   Convert an asctime(3) string to \c time_t
 *
 * Convert the asctime(3) string \p str to its \c time_t representation \p tp.
 *
 * \param   str     the string to be converted
 * \param   tp      the \c time_t conversion of \p str as a value-result
 *                  argument
 * \return
 * - \c 0   successful
 * - \c ~0  failure
 */
int u_asctime_to_tt(const char *str, time_t *tp)
{
    enum { BUFSZ = 64 };
    char wday[BUFSZ], mon[BUFSZ];
    unsigned int day, year, hour, min, sec;
    struct tm tm;
    int i;

    dbg_return_if (str == NULL, ~0);
    dbg_return_if (tp == NULL, ~0);
    dbg_return_if (strlen(str) >= BUFSZ, ~0);

    dbg_err_if((i = sscanf(str, "%s %s %u %u:%u:%u %u", wday,
                           mon, &day, &hour, &min, &sec, &year)) != 7);

    memset(&tm, 0, sizeof(struct tm));

    /* time */
    tm.tm_sec = sec;
    tm.tm_min = min;
    tm.tm_hour = hour;

    /* date */
    tm.tm_mday = day;
    tm.tm_mon = month_idx(mon);
    tm.tm_year = year - 1900;

    dbg_err_if(tm.tm_mon < 0);

    *tp = timegm(&tm);

    return 0;
err:
    return ~0;
}
/* day_of_year: set day of year from month & day */
int day_of_year(int year, char *month, int day)
{
    int i, leap, idxmonth;
    leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;

    if (year < 0 || !ismonth(month) || day <= 0 ||  day > daytab[leap][(idxmonth = month_idx(month))])
	return -1;
    
    for (i = 1; i < idxmonth; ++i) {
	day += daytab[leap][i];
    }
    return day;
}
Beispiel #3
0
/**
 * \brief   Convert an rfc850 time string to \c time_t
 *
 * Convert the rfc850 string \p str to its \c time_t representation \p tp.
 *
 * \param   str     the string to be converted
 * \param   tp      the \c time_t conversion of \p str as a value-result
 *                  argument
 * \return
 * - \c 0   successful
 * - \c ~0  failure
 */
int u_rfc850_to_tt(const char *str, time_t *tp)
{
    enum { BUFSZ = 64 };
    char wday[BUFSZ], mon[BUFSZ], tzone[BUFSZ];
    unsigned int day, year, hour, min, sec;
    struct tm tm;
    int i;
    char c;

    dbg_return_if (str == NULL, ~0);
    dbg_return_if (tp == NULL, ~0);
    dbg_return_if (strlen(str) >= BUFSZ, ~0);

    dbg_err_if((i = sscanf(str, "%[^,], %u%c%[^-]%c%u %u:%u:%u %s", wday,
                           &day, &c, mon, &c, &year, &hour, &min, &sec, tzone)) != 10);

    memset(&tm, 0, sizeof(struct tm));

    /* time */
    tm.tm_sec = sec;
    tm.tm_min = min;
    tm.tm_hour = hour;

    /* date */
    tm.tm_mday = day;
    tm.tm_mon = month_idx(mon);
    tm.tm_year = year - 1900;

    dbg_err_if(tm.tm_mon < 0);

#ifdef HAVE_TMZONE
    /* time zone */
    tm.tm_zone = tzone;
#endif

    *tp = timegm(&tm);

    return 0;
err:
    return ~0;
}