]> git.r.bdr.sh - rbdr/ngx_http_office_hours_filter_module/commitdiff
Parse time ranges
authorBen Beltran <redacted>
Thu, 27 Dec 2018 21:56:48 +0000 (14:56 -0700)
committerBen Beltran <redacted>
Thu, 27 Dec 2018 21:56:48 +0000 (14:56 -0700)
ngx_http_office_hours_filter_module.c

index 773b6d2dc7defa5a71a1b1a2856bec4189866c2d..a3a5af826f628e0e5981d64d246bd7e74fb22cbe 100644 (file)
@@ -24,6 +24,7 @@
 
 const ngx_uint_t WEEK_LENGTH = 7;
 const char * CLOSED_TOKEN = "closed";
+const ngx_str_t TIME_REGEX = ngx_string("([0-9]{1,2})(?:\\:([0-9]{2}))?\\-([0-9]{1,2})(?:\\:([0-9]{2}))?");
 
 /* Main Configuration Structure */
 
@@ -54,6 +55,9 @@ static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours);
 static ngx_uint_t get_day_of_week(time_t time);
 static ngx_uint_t get_seconds_of_day(time_t time);
 
+/* Compiled Regex */
+ngx_regex_compile_t rc;
+
 /*
  * Module Definitions
  */
@@ -242,6 +246,12 @@ static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
     ngx_http_next_body_filter = ngx_http_top_body_filter;
     ngx_http_top_body_filter = ngx_http_office_hours_body_filter;
 
+    rc.pattern = TIME_REGEX;
+    rc.pool = cf->pool;
+    if (ngx_regex_compile(&rc) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
     return NGX_OK;
 }
 
@@ -289,6 +299,8 @@ static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours)
 static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours)
 {
 
+    int captures[(1 + 4) * 3];
+    ngx_int_t n;
     ngx_uint_t * parsed_hours;
 
     parsed_hours = malloc(2 * sizeof(ngx_uint_t));
@@ -299,6 +311,22 @@ static ngx_uint_t * parse_office_hours_string(ngx_str_t office_hours)
         return parsed_hours;
     }
 
+    n = ngx_regex_exec(rc.regex, &office_hours, captures, (1 + 4) * 3);
+
+    if (n >= 0) {
+        /* Opening Hours */
+
+        parsed_hours[0] = 60 * 60 * ngx_atoi(&office_hours.data[captures[2]], captures[3] - captures[2]);
+        parsed_hours[0] = parsed_hours[0] + 60 * ngx_atoi(&office_hours.data[captures[4]], captures[5] - captures[4]);
+
+        parsed_hours[1] = 60 * 60 * ngx_atoi(&office_hours.data[captures[6]], captures[7] - captures[6]);
+        parsed_hours[1] = parsed_hours[1] + 60 * ngx_atoi(&office_hours.data[captures[8]], captures[9] - captures[8]);
+
+        return parsed_hours;
+    }
+
+    /* Non-matching strings count as open */
+
     parsed_hours[0] = 0;
     parsed_hours[1] = 86400;
     return parsed_hours;