]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
switch_utils: Add date time range string compare function switch_fulldate_cmp. It...
authorMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 17 Sep 2010 04:21:19 +0000 (00:21 -0400)
committerMarc Olivier Chouinard <mochouinard@moctel.com>
Fri, 17 Sep 2010 04:21:19 +0000 (00:21 -0400)
src/include/switch_utils.h
src/switch_utils.c
src/switch_xml.c

index 4eac906f59e592053a13746eb2a2c428fa62b697..a52676dddae59eb4a85252db684c471fe25ad7af 100644 (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
index cb4679d53ad9c421e553f9f2e694d9835de11317..6df515906e6c7084c7c448ff028d0320a48b89bb 100644 (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
index 77c3a5e20e539cc852717571e607bb8e3b97f89b..3e46fc0ae23a817b1ebfb4fe24e7f266c9e6b69d 100644 (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);