Beispiel #1
0
long long
crm_time_get_seconds(crm_time_t * dt)
{
    int lpc;
    crm_time_t *utc = NULL;
    long long in_seconds = 0;

    utc = crm_get_utc_time(dt);

    for (lpc = 1; lpc < utc->years; lpc++) {
        int dmax = year_days(lpc);

        in_seconds += 60 * 60 * 24 * dmax;
    }

    /* 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;
}
Beispiel #2
0
int
crm_time_get_isoweek(crm_time_t * dt, uint * y, uint * w, uint * d)
{
    /*
     * Monday 29 December 2008 is written "2009-W01-1"
     * Sunday 3 January 2010 is written "2009-W53-7"
     */
    int year_num = 0;
    int jan1 = crm_time_january1_weekday(dt->years);
    int h = -1;

    CRM_CHECK(dt->days > 0, return FALSE);

/* 6. Find the Weekday for Y M D */
    h = dt->days + jan1 - 1;
    *d = 1 + ((h - 1) % 7);

/* 7. Find if Y M D falls in YearNumber Y-1, WeekNumber 52 or 53 */
    if (dt->days <= (8 - jan1) && jan1 > 4) {
        crm_trace("year--, jan1=%d", jan1);
        year_num = dt->years - 1;
        *w = crm_time_weeks_in_year(year_num);

    } else {
        year_num = dt->years;
    }

/* 8. Find if Y M D falls in YearNumber Y+1, WeekNumber 1 */
    if (year_num == dt->years) {
        int dmax = year_days(year_num);
        int correction = 4 - *d;

        if ((dmax - dt->days) < correction) {
            crm_trace("year++, jan1=%d, i=%d vs. %d", jan1, dmax - dt->days, correction);
            year_num = dt->years + 1;
            *w = 1;
        }
    }

/* 9. Find if Y M D falls in YearNumber Y, WeekNumber 1 through 53 */
    if (year_num == dt->years) {
        int j = dt->days + (7 - *d) + (jan1 - 1);

        *w = j / 7;
        if (jan1 > 4) {
            *w -= 1;
        }
    }

    *y = year_num;
    crm_trace("Converted %.4d-%.3d to %.4d-W%.2d-%d", dt->years, dt->days, *y, *w, *d);
    return TRUE;
}
date_t plusab(date_t *date, day_t days)
    { /* todo: eliminate loops, handle year <== 1752 */
    /* Normalize the days to be less then 1 year */
       while(days < 0){
         date->year -= 1;
         days += year_days(date->year);
       };
       while(days > year_days(date->year)){
         days -= year_days(date->year);
         date->year += 1;
       };
       date->month_day += days;
    /* Normalize the days to be the same month */
       while(date->month_day > month_days(date->year, date->month)){
          date->month_day -= month_days(date->year, date->month);
          date->month += 1;
          if(date->month > year_months){
             date->month -= year_months;
             date->year += 1;
          }
       }
       date->week_day = week_day(*date);
       return *date;
    }
Beispiel #4
0
crm_time_t *
parse_date(const char *date_str)
{
    char *time_s;
    crm_time_t *dt = NULL;

    int year = 0;
    int month = 0;
    int week = 0;
    int day = 0;
    int rc = 0;

    CRM_CHECK(date_str != NULL, return NULL);
    CRM_CHECK(strlen(date_str) > 0, return NULL);

    if (date_str[0] == 'T' || date_str[2] == ':') {
        /* Just a time supplied - Infer current date */
        dt = crm_time_new(NULL);
        dt = crm_time_parse(date_str, dt);
        goto done;

    } else {
        dt = calloc(1, sizeof(crm_time_t));
    }

    if (safe_str_eq("epoch", date_str)) {
        dt->days = 1;
        dt->years = 1970;
        crm_time_log(LOG_TRACE, "Unpacked", dt, crm_time_log_date | crm_time_log_timeofday);
        return dt;
    }

    /* YYYY-MM-DD */
    rc = sscanf(date_str, "%d-%d-%d", &year, &month, &day);
    if (rc == 1) {
        /* YYYYMMDD */
        rc = sscanf(date_str, "%4d%2d%2d", &year, &month, &day);
    }
    if (rc == 3) {
        if (month > 12) {
            crm_err("Invalid month: %d", month);
        } else if (day > 31) {
            crm_err("Invalid day: %d", day);
        } else {
            dt->years = year;
            dt->days = get_ordinal_days(year, month, day);
            crm_trace("Got gergorian date: %.4d-%.3d", year, dt->days);
        }
        goto done;
    }

    /* YYYY-DDD */
    rc = sscanf(date_str, "%d-%d", &year, &day);
    if (rc == 2) {
        crm_trace("Got ordinal date");
        if (day > year_days(year)) {
            crm_err("Invalid day: %d (max=%d)", day, year_days(year));
        } else {
            dt->days = day;
            dt->years = year;
        }
        goto done;
    }

    /* YYYY-Www-D */
    rc = sscanf(date_str, "%d-W%d-%d", &year, &week, &day);
    if (rc == 3) {
        crm_trace("Got week date");
        if (week > crm_time_weeks_in_year(year)) {
            crm_err("Invalid week: %d (max=%d)", week, crm_time_weeks_in_year(year));
        } else if (day < 1 || day > 7) {
            crm_err("Invalid day: %d", day);
        } else {
            /*
             * http://en.wikipedia.org/wiki/ISO_week_date
             *
             * Monday 29 December 2008 is written "2009-W01-1"
             * Sunday 3 January 2010 is written "2009-W53-7"
             *
             * Saturday 27 September 2008 is written "2008-W37-6"
             *
             * http://en.wikipedia.org/wiki/ISO_week_date
             * If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01.
             * If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year.
             */
            int jan1 = crm_time_january1_weekday(year);

            crm_trace("Jan 1 = %d", jan1);

            dt->years = year;
            crm_time_add_days(dt, (week - 1) * 7);

            if (jan1 <= 4) {
                crm_time_add_days(dt, 1 - jan1);
            } else {
                crm_time_add_days(dt, 8 - jan1);
            }

            crm_time_add_days(dt, day);
        }
        goto done;
    }

    crm_err("Couldn't parse %s", date_str);
  done:

    time_s = strstr(date_str, " ");
    if (time_s == NULL) {
        time_s = strstr(date_str, "T");
    }

    if (dt && time_s) {
        time_s++;
        crm_time_parse(time_s, dt);
    }

    crm_time_log(LOG_TRACE, "Unpacked", dt, crm_time_log_date | crm_time_log_timeofday);

    CRM_CHECK(crm_time_check(dt), return NULL);

    return dt;
}