Exemple #1
0
static int testTimeFields(const void *args)
{
    const struct testTimeFieldsData *data = args;
    struct tm actual;

    virTimeFieldsThen(data->when, &actual);

#define COMPARE(field)                                          \
    do {                                                        \
        if (data->fields.field != actual.field) {               \
            VIR_DEBUG("Expect " #field " %d got %d",            \
                      data->fields.field, actual.field);        \
            return -1;                                          \
        }                                                       \
    } while (0)

    /* tm_year value 0 is based off epoch 1900 */
    actual.tm_year += 1900;
    /* tm_mon is range 0-11, but we want 1-12 */
    actual.tm_mon += 1;

    COMPARE(tm_year);
    COMPARE(tm_mon);
    COMPARE(tm_mday);
    COMPARE(tm_hour);
    COMPARE(tm_min);
    COMPARE(tm_sec);

    return 0;
}
Exemple #2
0
/**
 * virTimeFieldsNowRaw:
 * @fields: filled with current time fields
 *
 * Retrieves the current time, in broken-down field format.
 * The time is always in UTC.
 *
 * Returns 0 on success, -1 on error with errno reported
 */
int virTimeFieldsNow(struct tm *fields)
{
    unsigned long long now;

    if (virTimeMillisNow(&now) < 0)
        return -1;

    return virTimeFieldsThen(now, fields);
}
Exemple #3
0
/**
 * virTimeStringThenRaw:
 * @when: the time to format in milliseconds
 * @buf: a buffer at least VIR_TIME_STRING_BUFLEN in length
 *
 * Initializes @buf to contain a formatted timestamp
 * corresponding to the time @when.
 *
 * Returns 0 on success, -1 on error
 */
int virTimeStringThenRaw(unsigned long long when, char *buf)
{
    struct tm fields;

    virTimeFieldsThen(when, &fields);

    fields.tm_year += 1900;
    fields.tm_mon += 1;

    if (snprintf(buf, VIR_TIME_STRING_BUFLEN,
                 "%4d-%02d-%02d %02d:%02d:%02d.%03d+0000",
                 fields.tm_year, fields.tm_mon, fields.tm_mday,
                 fields.tm_hour, fields.tm_min, fields.tm_sec,
                 (int) (when % 1000)) >= VIR_TIME_STRING_BUFLEN) {
        errno = ERANGE;
        return -1;
    }

    return 0;
}