/* * Adjust *start and *end to be the common part of * [*start, *end] and [oldest, latest] * * Return 0 on success, -1 otherwise * * Note, * 1. Due to the fact that xml_get_child_val > xmlGetProp * will return a copy of the value of relevant attribute, * before changing the start and end pointer, the original * string pointed to must be released to prevent memory * leaks. */ int timestamp_find_common(char **start, char **end, const char *oldest, const char *latest) { int res_d, res_t; if (timestamp_compare(*start, oldest, &res_d, &res_t) < 0) { return -1; } if (res_d < 0 || (res_d == 0 && res_t < 0)) { free(*start); *start = strdup(oldest); } if (timestamp_compare(*end, latest, &res_d, &res_t) < 0) { return -1; } if (res_d > 0 || (res_d == 0 && res_t > 0)) { free(*end); *end = strdup(latest); } return 0; }
int main() { timestamp_t t1, t2; t1.sec = t2.sec = 0; t1.nsec = t2.nsec = 0; t1.offset = t2.offset = 0; cmp_ok(timestamp_compare(&t1, &t2), "==", 0, "t1 == t2"); t1.sec = 1; cmp_ok(timestamp_compare(&t1, &t2), ">", 0, "t1 > t2"); cmp_ok(timestamp_compare(&t2, &t1), "<", 0, "t1 < t2"); t1.sec = 0; t1.nsec = 1; cmp_ok(timestamp_compare(&t1, &t2), ">", 0, "t1 > t2"); cmp_ok(timestamp_compare(&t2, &t1), "<", 0, "t2 < t1"); done_testing(); }
/* * Check if [start, end] has something in common with * [oldest, latest] * * Return 1 when common part exists, < 0 otherwise, * in particular: * * -2, unable to compare timestamp strings; * -3, the start TS is after the latest TS; * -4, the end TS is before the oldest TS; */ int timestamp_has_common(const char *start, const char *end, const char *oldest, const char *latest) { int res_d, res_t; if (timestamp_compare(start, latest, &res_d, &res_t) < 0) { return -2; } if (res_d > 0 || (res_d == 0 && res_t > 0)) { return -3; } if (timestamp_compare(end, oldest, &res_d, &res_t) < 0) { return -2; } if (res_d < 0 || (res_d == 0 && res_t < 0)) { return -4; } return 1; }