40 #if defined(HAVE_STRUCT_TM_TM_GMTOFF) 41 # define GMTOFF(tm) ((tm)->tm_gmtoff) 44 # define GMTOFF(tm) (-timezone+daylight) 47 #define HOUR_SECONDS (60 * 60) 48 #define DAY_SECONDS (HOUR_SECONDS * 24) 60 static crm_time_t *parse_date(
const char *date_str);
73 utc->years = dt->years;
75 utc->seconds = dt->seconds;
82 utc->months = dt->months;
99 if (date_time == NULL) {
104 dt = parse_date(date_time);
136 return (t != NULL) && (t->years || t->months || t->days || t->seconds
137 || t->offset || t->duration);
171 int YY = (year - 1) % 100;
172 int C = (year - 1) - YY;
174 int jan1 = 1 + (((((C / 100) % 4) * 5) + G) % 7);
176 crm_trace(
"YY=%d, C=%d, G=%d", YY, C, G);
177 crm_trace(
"January 1 %.4d: %d", year, jan1);
202 static int month_days[13] = {
203 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29
217 if ((month < 1) || (month > 12)) {
223 return month_days[month - 1];
229 gboolean is_leap = FALSE;
234 if (year % 100 == 0 && year % 400 != 0) {
241 get_ordinal_days(uint32_t y, uint32_t m, uint32_t d)
245 for (lpc = 1; lpc < m; lpc++) {
259 (prefix? prefix :
""), (prefix?
": " :
""), date_s);
262 (prefix? prefix :
""), (prefix?
": " :
""), date_s);
268 crm_time_get_sec(
int sec, uint * h, uint * m, uint * s)
270 uint hours, minutes, seconds;
281 minutes = seconds / 60;
282 seconds -= 60 * minutes;
284 crm_trace(
"%d == %.2d:%.2d:%.2d", sec, hours, minutes, seconds);
294 crm_time_get_sec(dt->seconds, h, m, s);
303 crm_time_get_sec(dt->seconds, h, m, &s);
312 long long in_seconds = 0;
318 utc = crm_get_utc_time(dt);
323 for (lpc = 1; lpc < utc->years; lpc++) {
324 int dmax = year_days(lpc);
336 if (utc->months > 0) {
343 in_seconds += utc->seconds;
349 #define EPOCH_SECONDS 62135596800ULL 363 for (months = 1; months <= 12 && days > 0; months++) {
373 }
else if (dt->months) {
384 crm_trace(
"%.4d-%.3d -> %.4d-%.2d-%.2d", dt->years, dt->days, dt->years, months, days);
410 h = dt->days + jan1 - 1;
411 *d = 1 + ((h - 1) % 7);
414 if (dt->days <= (8 - jan1) && jan1 > 4) {
416 year_num = dt->years - 1;
420 year_num = dt->years;
424 if (year_num == dt->years) {
425 int dmax = year_days(year_num);
426 int correction = 4 - *d;
428 if ((dmax - dt->days) < correction) {
429 crm_trace(
"year++, jan1=%d, i=%d vs. %d", jan1, dmax - dt->days, correction);
430 year_num = dt->years + 1;
436 if (year_num == dt->years) {
437 int j = dt->days + (7 - *d) + (jan1 - 1);
446 crm_trace(
"Converted %.4d-%.3d to %.4d-W%.2d-%d", dt->years, dt->days, *y, *w, *d);
453 crm_duration_as_string(
crm_time_t *dt,
char *result)
458 offset += snprintf(result + offset,
DATE_MAX - offset,
"%4d year%s ",
462 offset += snprintf(result + offset,
DATE_MAX - offset,
"%2d month%s ",
466 offset += snprintf(result + offset,
DATE_MAX - offset,
"%2d day%s ",
470 if (((offset == 0) || (dt->seconds != 0))
471 && (dt->seconds > -60) && (dt->seconds < 60)) {
472 offset += snprintf(result + offset,
DATE_MAX - offset,
"%d second%s",
474 }
else if (dt->seconds) {
475 uint h = 0, m = 0, s = 0;
477 offset += snprintf(result + offset,
DATE_MAX - offset,
"%d seconds (",
479 crm_time_get_sec(dt->seconds, &h, &m, &s);
481 offset += snprintf(result + offset,
DATE_MAX - offset,
"%u hour%s%s",
485 offset += snprintf(result + offset,
DATE_MAX - offset,
"%u minute%s%s",
489 offset += snprintf(result + offset,
DATE_MAX - offset,
"%u second%s",
492 offset += snprintf(result + offset,
DATE_MAX - offset,
")");
502 char *result_copy = NULL;
506 if (date_time && date_time->offset
509 utc = crm_get_utc_time(date_time);
516 strcpy(result,
"<undefined time>");
523 crm_duration_as_string(date_time, result);
545 offset += snprintf(result + offset,
DATE_MAX - offset,
546 "%u-W%.2u-%u", y, w, d);
553 offset += snprintf(result + offset,
DATE_MAX - offset,
561 offset += snprintf(result + offset,
DATE_MAX - offset,
562 "%.4u-%.2u-%.2u", y, m, d);
568 uint h = 0, m = 0, s = 0;
571 offset += snprintf(result + offset,
DATE_MAX - offset,
" ");
575 offset += snprintf(result + offset,
DATE_MAX - offset,
576 "%.2u:%.2u:%.2u", h, m, s);
580 crm_time_get_sec(dt->offset, &h, &m, &s);
581 offset += snprintf(result + offset,
DATE_MAX - offset,
583 ((dt->offset < 0)?
'-' :
'+'), h, m);
585 offset += snprintf(result + offset,
DATE_MAX - offset,
"Z");
592 result_copy = strdup(result);
609 crm_time_parse_sec(
const char *time_str,
int *result)
619 rc = sscanf(time_str,
"%d:%d:%d", &hour, &minute, &second);
621 rc = sscanf(time_str,
"%2d%2d%2d", &hour, &minute, &second);
624 crm_err(
"%s is not a valid ISO 8601 time specification", time_str);
629 crm_trace(
"Got valid time: %.2d:%.2d:%.2d", hour, minute, second);
631 if ((hour == 24) && (minute == 0) && (second == 0)) {
633 }
else if (hour >= 24) {
634 crm_err(
"%s is not a valid ISO 8601 time specification " 635 "because %d is not a valid hour", time_str, hour);
640 crm_err(
"%s is not a valid ISO 8601 time specification " 641 "because %d is not a valid minute", time_str, minute);
646 crm_err(
"%s is not a valid ISO 8601 time specification " 647 "because %d is not a valid second", time_str, second);
652 *result = (hour *
HOUR_SECONDS) + (minute * 60) + second;
657 crm_time_parse_offset(
const char *offset_str,
int *offset)
661 if (offset_str == NULL) {
663 #if defined(HAVE_STRUCT_TM_TM_GMTOFF) 664 time_t now = time(NULL);
665 struct tm *now_tm = localtime(&now);
670 if (h_offset < 0 && m_offset < 0) {
671 m_offset = 0 - m_offset;
677 if (offset_str[0] ==
'Z') {
683 if ((offset_str[0] ==
'+') || (offset_str[0] ==
'-')
684 || isdigit((
int)offset_str[0])) {
686 gboolean negate = FALSE;
688 if (offset_str[0] ==
'+') {
690 }
else if (offset_str[0] ==
'-') {
694 if (crm_time_parse_sec(offset_str, offset) == FALSE) {
698 *offset = 0 - *offset;
715 crm_time_parse(
const char *time_str,
crm_time_t *a_time)
718 char *offset_s = NULL;
723 if (crm_time_parse_sec(time_str, &(a_time->seconds)) == FALSE) {
726 offset_s = strstr(time_str,
"Z");
727 if (offset_s == NULL) {
728 offset_s = strstr(time_str,
" ");
730 while (isspace(offset_s[0])) {
737 if (crm_time_parse_offset(offset_s, &(a_time->offset)) == FALSE) {
740 crm_time_get_sec(a_time->offset, &h, &m, &s);
741 crm_trace(
"Got tz: %c%2.d:%.2d", ((a_time->offset < 0)?
'-' :
'+'), h, m);
760 parse_date(
const char *date_str)
762 const char *time_s = NULL;
771 if (pcmk__str_empty(date_str)) {
772 crm_err(
"No ISO 8601 date/time specification given");
776 if ((date_str[0] ==
'T') || (date_str[2] ==
':')) {
779 if (date_str[0] ==
'T') {
780 time_s = date_str + 1;
789 if (!strncasecmp(
"epoch", date_str, 5)
790 && ((date_str[5] ==
'\0') || (date_str[5] ==
'/') || isspace(date_str[5]))) {
798 rc = sscanf(date_str,
"%d-%d-%d", &year, &month, &day);
801 rc = sscanf(date_str,
"%4d%2d%2d", &year, &month, &day);
805 crm_err(
"'%s' is not a valid ISO 8601 date/time specification " 806 "because '%d' is not a valid month", date_str, month);
809 crm_err(
"'%s' is not a valid ISO 8601 date/time specification " 810 "because '%d' is not a valid day of the month",
815 dt->days = get_ordinal_days(year, month, day);
816 crm_trace(
"Parsed Gregorian date '%.4d-%.3d' from date string '%s'",
817 year, dt->days, date_str);
823 rc = sscanf(date_str,
"%d-%d", &year, &day);
825 if (day > year_days(year)) {
826 crm_err(
"'%s' is not a valid ISO 8601 date/time specification " 827 "because '%d' is not a valid day of the year (max %d)",
828 date_str, day, year_days(year));
831 crm_trace(
"Parsed ordinal year %d and days %d from date string '%s'",
832 year, day, date_str);
839 rc = sscanf(date_str,
"%d-W%d-%d", &year, &week, &day);
842 crm_err(
"'%s' is not a valid ISO 8601 date/time specification " 843 "because '%d' is not a valid week of the year (max %d)",
846 }
else if (day < 1 || day > 7) {
847 crm_err(
"'%s' is not a valid ISO 8601 date/time specification " 848 "because '%d' is not a valid day of the week",
864 crm_trace(
"Got year %d (Jan 1 = %d), week %d, and day %d from date string '%s'",
865 year, jan1, week, day, date_str);
881 crm_err(
"'%s' is not a valid ISO 8601 date/time specification", date_str);
886 if (time_s == NULL) {
887 time_s = date_str + strspn(date_str,
"0123456789-W");
888 if ((time_s[0] ==
' ') || (time_s[0] ==
'T')) {
894 if ((time_s != NULL) && (crm_time_parse(time_s, dt) == FALSE)) {
900 crm_err(
"'%s' is not a valid ISO 8601 date/time specification",
917 parse_int(
const char *str,
int field_width,
int upper_bound,
int *result)
921 int intermediate = 0;
922 gboolean fraction = FALSE;
923 gboolean negate = FALSE;
930 if (str[offset] ==
'T') {
934 if (str[offset] ==
'.' || str[offset] ==
',') {
938 }
else if (str[offset] ==
'-') {
941 }
else if (str[offset] ==
'+' || str[offset] ==
':') {
945 for (; (fraction || lpc < field_width) && isdigit((
int)str[offset]); lpc++) {
947 intermediate = (str[offset] -
'0') / (10 ^ lpc);
950 intermediate = str[offset] -
'0';
952 *result += intermediate;
956 *result = (int)(*result * upper_bound);
958 }
else if (upper_bound > 0 && *result > upper_bound) {
959 *result = upper_bound;
962 *result = 0 - *result;
965 crm_trace(
"Found int: %d. Stopped at str[%d]='%c'", *result, lpc, str[lpc]);
985 gboolean is_time = FALSE;
988 if (pcmk__str_empty(period_s)) {
989 crm_err(
"No ISO 8601 time duration given");
992 if (period_s[0] !=
'P') {
993 crm_err(
"'%s' is not a valid ISO 8601 time duration " 994 "because it does not start with a 'P'", period_s);
997 if ((period_s[1] ==
'\0') || isspace(period_s[1])) {
998 crm_err(
"'%s' is not a valid ISO 8601 time duration " 999 "because nothing follows 'P'", period_s);
1004 diff->duration = TRUE;
1006 for (
const char *current = period_s + 1;
1007 current[0] && (current[0] !=
'/') && !isspace(current[0]);
1012 if (current[0] ==
'T') {
1022 rc = parse_int(current, 10, 0, &an_int);
1024 crm_err(
"'%s' is not a valid ISO 8601 time duration " 1025 "because no integer at '%s'", period_s, current);
1031 switch (current[0]) {
1033 diff->years = an_int;
1038 diff->seconds += an_int * 60;
1040 diff->months = an_int;
1044 diff->days += an_int * 7;
1047 diff->days += an_int;
1053 diff->seconds += an_int;
1056 crm_err(
"'%s' is not a valid ISO 8601 time duration " 1057 "because no units after %d", period_s, an_int);
1060 crm_err(
"'%s' is not a valid ISO 8601 time duration " 1061 "because '%c' is not a valid time unit",
1062 period_s, current[0]);
1068 crm_err(
"'%s' is not a valid ISO 8601 time duration " 1069 "because no amounts and units given", period_s);
1093 const char *original = period_str;
1096 if (pcmk__str_empty(period_str)) {
1097 crm_err(
"No ISO 8601 time period given");
1105 if (period_str[0] ==
'P') {
1107 if (period->
diff == NULL) {
1111 period->
start = parse_date(period_str);
1112 if (period->
start == NULL) {
1117 period_str = strstr(original,
"/");
1120 if (period_str[0] ==
'P') {
1121 if (period->
diff != NULL) {
1122 crm_err(
"'%s' is not a valid ISO 8601 time period " 1123 "because it has two durations",
1128 if (period->
diff == NULL) {
1132 period->
end = parse_date(period_str);
1133 if (period->
end == NULL) {
1138 }
else if (period->
diff != NULL) {
1144 crm_err(
"'%s' is not a valid ISO 8601 time period " 1145 "because it has no duration or ending time",
1150 if (period->
start == NULL) {
1153 }
else if (period->
end == NULL) {
1158 crm_err(
"'%s' is not a valid ISO 8601 time period " 1159 "because the start is invalid", period_str);
1163 crm_err(
"'%s' is not a valid ISO 8601 time period " 1164 "because the end is invalid", period_str);
1199 target->years = source->years;
1200 target->days = source->days;
1201 target->months = source->months;
1202 target->seconds = source->seconds;
1203 target->offset = source->offset;
1223 target->duration = FALSE;
1225 if (source->tm_year > 0) {
1227 target->years = 1900 + source->tm_year;
1230 if (source->tm_yday >= 0) {
1232 target->days = 1 + source->tm_yday;
1235 if (source->tm_hour >= 0) {
1238 if (source->tm_min >= 0) {
1239 target->seconds += 60 * source->tm_min;
1241 if (source->tm_sec >= 0) {
1242 target->seconds += source->tm_sec;
1248 crm_trace(
"Offset (s): %ld, offset (hh:mm): %.2d:%.2d",
GMTOFF(source), h_offset, m_offset);
1251 target->offset += 60 * m_offset;
1257 ha_set_tm_time(
target, localtime(source));
1275 if ((dt == NULL) || (value == NULL)) {
1282 utc = crm_get_utc_time(value);
1288 answer->years += utc->years;
1303 if ((dt == NULL) || (value == NULL)) {
1308 utc = crm_get_utc_time(value);
1313 answer = crm_get_utc_time(dt);
1314 if (answer == NULL) {
1318 answer->duration = TRUE;
1320 answer->years -= utc->years;
1321 if(utc->months != 0) {
1337 if ((dt == NULL) || (value == NULL)) {
1342 utc = crm_get_utc_time(value);
1348 answer->years -= utc->years;
1349 if(utc->months != 0) {
1369 && (dt->days > 0) && (dt->days <= year_days(dt->years))
1370 && (dt->seconds >= 0) && (dt->seconds <
DAY_SECONDS);
1373 #define do_cmp_field(l, r, field) \ 1375 if(l->field > r->field) { \ 1376 crm_trace("%s: %d > %d", \ 1377 #field, l->field, r->field); \ 1379 } else if(l->field < r->field) { \ 1380 crm_trace("%s: %d < %d", \ 1381 #field, l->field, r->field); \ 1393 if ((t1 == NULL) && (t2 == NULL)) {
1395 }
else if (t1 == NULL) {
1397 }
else if (t2 == NULL) {
1421 crm_trace(
"Adding %d seconds to %d (max=%d)",
1423 a_time->seconds += extra;
1428 if (a_time->seconds < 0) {
1439 int lower_bound = 1;
1442 crm_trace(
"Adding %d days to %.4d-%.3d", extra, a_time->years, a_time->days);
1444 a_time->days += extra;
1445 while (a_time->days > ydays) {
1447 a_time->days -= ydays;
1451 if(a_time->duration) {
1455 while (a_time->days < lower_bound) {
1465 uint32_t y, m, d, dmax;
1468 crm_trace(
"Adding %d months to %.4d-%.2d-%.2d", extra, y, m, d);
1471 for (lpc = extra; lpc > 0; lpc--) {
1479 for (lpc = -extra; lpc > 0; lpc--) {
1494 crm_trace(
"Calculated %.4d-%.2d-%.2d", y, m, d);
1497 a_time->days = get_ordinal_days(y, m, d);
1500 crm_trace(
"Got %.4d-%.2d-%.2d", y, m, d);
1524 a_time->years += extra;
1531 .tm_year = source->years - 1900,
1532 .tm_mday = source->days,
1533 .tm_sec = source->seconds % 60,
1534 .tm_min = ( source->seconds / 60 ) % 60,
1539 .tm_gmtoff = source->offset
1565 .months = dt->months,
1567 .seconds = dt->seconds,
1568 .offset = dt->offset,
1569 .duration = dt->duration
1581 .years = hr_dt->
years,
1583 .days = hr_dt->
days,
1608 struct timeval tv_now;
1611 if (gettimeofday(&tv_now, NULL) == 0) {
1617 dt = parse_date(date_time);
1634 int max = 128, scanned_pos = 0, printed_pos = 0, fmt_pos = 0,
1635 date_len = 0, nano_digits = 0;
1636 char nano_s[10], date_s[max+1], nanofmt_s[5] =
"%", *tmp_fmt_s;
1644 ha_get_tm_time(&tm, &dt);
1645 sprintf(nano_s,
"%06d000", hr_dt->
useconds);
1647 while ((format[scanned_pos]) !=
'\0') {
1648 mark_s = strchr(&format[scanned_pos],
'%');
1652 fmt_pos = mark_s - format;
1653 while ((format[fmt_pos+fmt_len] !=
'\0') &&
1654 (format[fmt_pos+fmt_len] >=
'0') &&
1655 (format[fmt_pos+fmt_len] <=
'9')) {
1658 scanned_pos = fmt_pos + fmt_len + 1;
1659 if (format[fmt_pos+fmt_len] ==
'N') {
1660 nano_digits = atoi(&format[fmt_pos+1]);
1661 nano_digits = (nano_digits > 6)?6:nano_digits;
1662 nano_digits = (nano_digits < 0)?0:nano_digits;
1663 sprintf(&nanofmt_s[1],
".%ds", nano_digits);
1665 if (format[scanned_pos] !=
'\0') {
1668 fmt_pos = scanned_pos;
1671 scanned_pos = strlen(format);
1672 fmt_pos = scanned_pos;
1674 tmp_fmt_s =
strndup(&format[printed_pos], fmt_pos - printed_pos);
1675 #ifdef GCC_FORMAT_NONLITERAL_CHECKING_ENABLED 1676 #pragma GCC diagnostic push 1677 #pragma GCC diagnostic ignored "-Wformat-nonliteral" 1679 date_len += strftime(&date_s[date_len], max-date_len, tmp_fmt_s, &tm);
1680 #ifdef GCC_FORMAT_NONLITERAL_CHECKING_ENABLED 1681 #pragma GCC diagnostic pop 1683 printed_pos = scanned_pos;
1686 #ifdef GCC_FORMAT_NONLITERAL_CHECKING_ENABLED 1687 #pragma GCC diagnostic push 1688 #pragma GCC diagnostic ignored "-Wformat-nonliteral" 1690 date_len += snprintf(&date_s[date_len], max-date_len,
1692 #ifdef GCC_FORMAT_NONLITERAL_CHECKING_ENABLED 1693 #pragma GCC diagnostic pop 1699 return (date_len == 0)?NULL:strdup(date_s);
1717 char *since_epoch = NULL;
1720 time_t a_time = time(NULL);
1722 if (a_time == (time_t) -1) {
1725 since_epoch = ctime(&a_time);
1728 since_epoch = ctime(when);
1731 if (since_epoch == NULL) {
#define CRM_CHECK(expr, failure_action)
crm_time_t * crm_time_new_undefined()
Allocate memory for an uninitialized time object.
crm_time_t * pcmk_copy_time(crm_time_t *source)
bool crm_time_leapyear(int year)
crm_time_t * crm_time_subtract(crm_time_t *dt, crm_time_t *value)
void pcmk__time_hr_free(pcmk__time_hr_t *hr_dt)
void crm_time_set_timet(crm_time_t *target, time_t *source)
crm_time_t * crm_time_add(crm_time_t *dt, crm_time_t *value)
#define crm_time_log_timeofday
crm_time_t * crm_time_new(const char *date_time)
int crm_time_compare(crm_time_t *a, crm_time_t *b)
struct crm_time_s crm_time_t
void crm_time_add_minutes(crm_time_t *a_time, int extra)
void crm_time_add_weeks(crm_time_t *a_time, int extra)
void crm_time_free(crm_time_t *dt)
#define do_cmp_field(l, r, field)
#define do_crm_log_alias(level, file, function, line, fmt, args...)
Log a message as if it came from a different code location.
pcmk__time_hr_t * pcmk__time_timeval_hr_convert(pcmk__time_hr_t *target, struct timeval *tv)
#define crm_time_log_duration
char * strndup(const char *str, size_t len)
void crm_time_log_alias(int log_level, const char *file, const char *function, int line, const char *prefix, crm_time_t *date_time, int flags)
pcmk__time_hr_t * pcmk__time_hr_new(const char *date_time)
struct pcmk__time_us pcmk__time_hr_t
crm_time_period_t * crm_time_parse_period(const char *period_str)
Parse a time period from an ISO 8601 interval specification.
int crm_time_get_gregorian(crm_time_t *dt, uint *y, uint *m, uint *d)
void crm_time_add_seconds(crm_time_t *a_time, int extra)
Add a given number of seconds to a date/time or duration.
bool crm_time_check(crm_time_t *dt)
Check whether a time object represents a sensible date/time.
long long crm_time_get_seconds(crm_time_t *dt)
void crm_time_add_days(crm_time_t *a_time, int extra)
#define crm_trace(fmt, args...)
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
int crm_time_get_isoweek(crm_time_t *dt, uint *y, uint *w, uint *d)
crm_time_t * crm_time_parse_duration(const char *period_s)
Parse a time duration from an ISO 8601 duration specification.
void crm_time_add_hours(crm_time_t *a_time, int extra)
#define crm_time_log(level, prefix, dt, flags)
int crm_time_days_in_month(int month, int year)
Return number of days in given month of given year.
#define crm_time_log_with_timezone
int crm_time_get_timezone(crm_time_t *dt, uint *h, uint *m)
char * pcmk__time_format_hr(const char *format, pcmk__time_hr_t *hr_dt)
void crm_time_add_years(crm_time_t *a_time, int extra)
bool crm_time_is_defined(const crm_time_t *t)
Check whether a time object has been initialized yet.
#define HAVE_STRUCT_TM_TM_GMTOFF
long long crm_time_get_seconds_since_epoch(crm_time_t *dt)
#define crm_err(fmt, args...)
void crm_time_add_months(crm_time_t *a_time, int extra)
int crm_time_weeks_in_year(int year)
int crm_time_january1_weekday(int year)
const char * pcmk__epoch2str(time_t *when)
#define pcmk__plural_s(i)
void pcmk__time_set_hr_dt(crm_time_t *target, pcmk__time_hr_t *hr_dt)
crm_time_t * crm_time_calculate_duration(crm_time_t *dt, crm_time_t *value)
int crm_time_get_ordinal(crm_time_t *dt, uint *y, uint *d)
char * crm_strip_trailing_newline(char *str)
char * crm_time_as_string(crm_time_t *date_time, int flags)
void crm_time_free_period(crm_time_period_t *period)
Free a dynamically allocated time period object.
#define crm_time_log_date
pcmk__time_hr_t * pcmk__time_hr_convert(pcmk__time_hr_t *target, crm_time_t *dt)
int crm_time_get_timeofday(crm_time_t *dt, uint *h, uint *m, uint *s)
void crm_time_set(crm_time_t *target, crm_time_t *source)