Esempio n. 1
0
gboolean
cron_range_satisfied(crm_time_t * now, xmlNode * cron_spec)
{
    const char *value = NULL;
    char *value_low = NULL;
    char *value_high = NULL;

    int value_low_i = 0;
    int value_high_i = 0;

    uint32_t h, m, s, y, d, w;

    CRM_CHECK(now != NULL, return FALSE);

    crm_time_get_timeofday(now, &h, &m, &s);

    cron_check("seconds", s);
    cron_check("minutes", m);
    cron_check("hours", h);

    crm_time_get_gregorian(now, &y, &m, &d);

    cron_check("monthdays", d);
    cron_check("months", m);
    cron_check("years", y);

    crm_time_get_ordinal(now, &y, &d);

    cron_check("yeardays", d);

    crm_time_get_isoweek(now, &y, &w, &d);

    cron_check("weekyears", y);
    cron_check("weeks", w);
    cron_check("weekdays", d);

    cron_check("moon", phase_of_the_moon(now));

    return TRUE;
}
Esempio n. 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;
}