Beispiel #1
0
unsigned long long
crm_get_interval(const char *input)
{
    unsigned long long msec = 0;

    if (input == NULL) {
        return msec;

    } else if (input[0] != 'P') {
        long long tmp = crm_get_msec(input);

        if(tmp > 0) {
            msec = tmp;
        }

    } else {
        crm_time_t *interval = crm_time_parse_duration(input);

        msec = 1000 * crm_time_get_seconds(interval);
        crm_time_free(interval);
    }

    return msec;
}
Beispiel #2
0
char *
crm_time_as_string(crm_time_t * date_time, int flags)
{
    char *date_s = NULL;
    char *time_s = NULL;
    char *offset_s = NULL;
    char *result_s = NULL;
    crm_time_t *dt = NULL;
    crm_time_t *utc = NULL;

    if (date_time == NULL) {
        return strdup("");

    } else if (date_time->offset && (flags & crm_time_log_with_timezone) == 0) {
        crm_trace("UTC conversion");
        utc = crm_get_utc_time(date_time);
        dt = utc;
    } else {
        dt = date_time;
    }

    CRM_CHECK(dt != NULL, return NULL);
    if (flags & crm_time_log_duration) {
        uint h = 0, m = 0, s = 0;
        int offset = 0, max = 128;

        date_s = calloc(1, max+1);
        crm_time_get_sec(dt->seconds, &h, &m, &s);

        if (date_s == NULL) {
            goto done;
        }

        if(dt->years) {
            offset += snprintf(date_s+offset, max-offset, "%4d year%s ", dt->years, dt->years>1?"s":"");
        }
        if(dt->months) {
            offset += snprintf(date_s+offset, max-offset, "%2d month%s ", dt->months, dt->months>1?"s":"");
        }
        if(dt->days) {
            offset += snprintf(date_s+offset, max-offset, "%2d day%s ", dt->days, dt->days>1?"s":"");
        }
        if(dt->seconds) {
            offset += snprintf(date_s+offset, max-offset, "%d seconds ( ", dt->seconds);
            if(h) {
                offset += snprintf(date_s+offset, max-offset, "%d hour%s ", h, h>1?"s":"");
            }
            if(m) {
                offset += snprintf(date_s+offset, max-offset, "%d minute%s ", m, m>1?"s":"");
            }
            if(s) {
                offset += snprintf(date_s+offset, max-offset, "%d second%s ", s, s>1?"s":"");
            }
            offset += snprintf(date_s+offset, max-offset, ")");
        }
        goto done;
    }

    if (flags & crm_time_log_date) {
        date_s = calloc(1, 32);
        if (date_s == NULL) {
            goto done;

        } else if (flags & crm_time_seconds) {
            unsigned long long s = crm_time_get_seconds(date_time);

            snprintf(date_s, 31, "%lld", s); /* Durations may not be +ve */
            goto done;

        } else if (flags & crm_time_epoch) {
            unsigned long long s = crm_time_get_seconds_since_epoch(date_time);

            snprintf(date_s, 31, "%lld", s); /* Durations may not be +ve */
            goto done;

        } else if (flags & crm_time_weeks) {
            /* YYYY-Www-D */
            uint y, w, d;

            if (crm_time_get_isoweek(dt, &y, &w, &d)) {
                snprintf(date_s, 31, "%d-W%.2d-%d", y, w, d);
            }

        } else if (flags & crm_time_ordinal) {
            /* YYYY-DDD */
            uint y, d;

            if (crm_time_get_ordinal(dt, &y, &d)) {
                snprintf(date_s, 31, "%d-%.3d", y, d);
            }

        } else {
            /* YYYY-MM-DD */
            uint y, m, d;

            if (crm_time_get_gregorian(dt, &y, &m, &d)) {
                snprintf(date_s, 31, "%.4d-%.2d-%.2d", y, m, d);
            }
        }
    }

    if (flags & crm_time_log_timeofday) {
        uint h, m, s;

        time_s = calloc(1, 32);
        if (time_s == NULL) {
            goto cleanup;
        }

        if (crm_time_get_timeofday(dt, &h, &m, &s)) {
            snprintf(time_s, 31, "%.2d:%.2d:%.2d", h, m, s);
        }

        if (dt->offset != 0) {
            crm_time_get_sec(dt->offset, &h, &m, &s);
        }

        offset_s = calloc(1, 32);
        if ((flags & crm_time_log_with_timezone) == 0 || dt->offset == 0) {
            crm_trace("flags %6x %6x", flags, crm_time_log_with_timezone);
            snprintf(offset_s, 31, "Z");

        } else {
            snprintf(offset_s, 31, " %c%.2d:%.2d", dt->offset < 0 ? '-' : '+', h, m);
        }
    }

  done:
    result_s = calloc(1, 100);

    snprintf(result_s, 100, "%s%s%s%s",
             date_s ? date_s : "", (date_s != NULL && time_s != NULL) ? " " : "",
             time_s ? time_s : "", offset_s ? offset_s : "");

  cleanup:
    free(date_s);
    free(time_s);
    free(offset_s);
    crm_time_free(utc);

    return result_s;
}
Beispiel #3
0
    /* utc->months is an offset that can only be set for a duration
     * By definiton, the value is variable depending on the date to
     * which it is applied
     *
     * Force 30-day months so that something vaguely sane happens
     * for anyone that tries to use a month in this way
     */
    if (utc->months > 0) {
        in_seconds += 60 * 60 * 24 * 30 * utc->months;
    }

    if (utc->days > 0) {
        in_seconds += 60 * 60 * 24 * (utc->days - 1);
    }
    in_seconds += utc->seconds;

    crm_time_free(utc);
    return in_seconds;
}

#define EPOCH_SECONDS 62135596800ULL    /* Calculated using crm_time_get_seconds() */
long long
crm_time_get_seconds_since_epoch(crm_time_t * dt)
{
    return crm_time_get_seconds(dt) - EPOCH_SECONDS;
}