switch_utils: Add date time range string compare function switch_fulldate_cmp. It usable in XML dialplan with date-time. String format example : 2009-10-10 14:33:22~2009-11-10 17:32:31.

This commit is contained in:
Marc Olivier Chouinard 2010-09-17 00:21:19 -04:00
parent 4ab8fa13e1
commit c9fcce0869
3 changed files with 145 additions and 0 deletions

View File

@ -683,6 +683,10 @@ SWITCH_DECLARE(int) switch_inet_pton(int af, const char *src, void *dst);
SWITCH_DECLARE(int) switch_number_cmp(const char *exp, int val);
SWITCH_DECLARE(int) switch_tod_cmp(const char *exp, int val);
SWITCH_DECLARE(int) switch_fulldate_cmp(const char *exp, switch_time_t *ts);
SWITCH_DECLARE(void) switch_split_date(const char *exp, int *year, int *month, int *day);
SWITCH_DECLARE(void) switch_split_time(const char *exp, int *hour, int *min, int *sec);
/*!
\brief Split a user@domain string as user and domain
\param in the input string

View File

@ -2090,6 +2090,137 @@ SWITCH_DECLARE(char *) switch_url_decode(char *s)
return s;
}
SWITCH_DECLARE(void) switch_split_time(const char *exp, int *hour, int *min, int *sec)
{
char *dup = strdup(exp);
char *shour = NULL;
char *smin = NULL;
char *ssec = NULL;
switch_assert(dup);
shour = dup;
if ((smin=strchr(dup, ':'))) {
*smin++ = '\0';
if ((ssec=strchr(smin, ':'))) {
*ssec++ = '\0';
} else {
ssec = "00";
}
if (hour && shour) {
*hour = atol(shour);
}
if (min && smin) {
*min = atol(smin);
}
if (sec && ssec) {
*sec = atol(ssec);
}
}
switch_safe_free(dup);
return;
}
SWITCH_DECLARE(void) switch_split_date(const char *exp, int *year, int *month, int *day)
{
char *dup = strdup(exp);
char *syear = NULL;
char *smonth = NULL;
char *sday = NULL;
switch_assert(dup);
syear = dup;
if ((smonth=strchr(dup, '-'))) {
*smonth++ = '\0';
if ((sday=strchr(smonth, '-'))) {
*sday++ = '\0';
if (year && syear) {
*year = atol(syear);
}
if (month && smonth) {
*month = atol(smonth);
}
if (day && sday) {
*day = atol(sday);
}
}
}
switch_safe_free(dup);
return;
}
/* Ex exp value "2009-10-10 14:33:22~2009-11-10 17:32:31" */
SWITCH_DECLARE(int) switch_fulldate_cmp(const char *exp, switch_time_t *ts)
{
char *dup = strdup(exp);
char *sStart;
char *sEnd;
char *sDate;
char *sTime;
switch_time_t tsStart;
switch_time_t tsEnd;
switch_assert(dup);
sStart = dup;
if ((sEnd=strchr(dup, '~'))) {
*sEnd++ = '\0';
sDate = sStart;
if ((sTime=strchr(sStart, ' '))) {
struct tm tmTmp;
int year, month, day;
int hour, min, sec;
*sTime++ = '\0';
switch_split_date(sDate, &year, &month, &day);
switch_split_time(sTime, &hour, &min, &sec);
tmTmp.tm_year = year;
tmTmp.tm_mon = month;
tmTmp.tm_mday = day;
tmTmp.tm_hour = hour;
tmTmp.tm_min = min;
tmTmp.tm_sec = sec;
tmTmp.tm_isdst = 0;
tsStart = mktime(&tmTmp);
sDate = sEnd;
if ((sTime=strchr(sEnd, ' '))) {
struct tm tmTmp;
int year, month, day;
int hour, min, sec;
*sTime++ = '\0';
switch_split_date(sDate, &year, &month, &day);
switch_split_time(sTime, &hour, &min, &sec);
tmTmp.tm_year = year;
tmTmp.tm_mon = month;
tmTmp.tm_mday = day;
tmTmp.tm_hour = hour;
tmTmp.tm_min = min;
tmTmp.tm_sec = sec;
tmTmp.tm_isdst = 0;
tsEnd = mktime(&tmTmp);
if (tsStart <= *ts && tsEnd > *ts) {
switch_safe_free(dup);
return 1;
}
}
}
}
switch_safe_free(dup);
return 0;
}
/* Written by Marc Espie, public domain */
#define SWITCH_CTYPE_NUM_CHARS 256

View File

@ -2652,6 +2652,7 @@ SWITCH_DECLARE(switch_xml_t) switch_xml_cut(switch_xml_t xml)
SWITCH_DECLARE(int) switch_xml_std_datetime_check(switch_xml_t xcond) {
const char *xdt = switch_xml_attr(xcond, "date-time");
const char *xyear = switch_xml_attr(xcond, "year");
const char *xyday = switch_xml_attr(xcond, "yday");
const char *xmon = switch_xml_attr(xcond, "mon");
@ -2670,6 +2671,15 @@ SWITCH_DECLARE(int) switch_xml_std_datetime_check(switch_xml_t xcond) {
switch_time_exp_lt(&tm, ts);
if (time_match && xdt) {
char tmpdate[80];
switch_size_t retsize;
switch_strftime(tmpdate, &retsize, sizeof(tmpdate), "%Y-%m-%d %H:%M:%S", &tm);
time_match = switch_fulldate_cmp(xdt, &ts);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
"XML DateTime Check: date time[%s] =~ %s (%s)\n", tmpdate, xdt, time_match ? "PASS" : "FAIL");
}
if (time_match && xyear) {
int test = tm.tm_year + 1900;
time_match = switch_number_cmp(xyear, test);