+/*
+ * Parse the office hour strings in the configuration file
+ * to fill out the hours array (in seconds)
+ */
+
+static ngx_uint_t ** parse_office_hours(ngx_array_t * office_hours)
+{
+
+ ngx_str_t *hours;
+ ngx_uint_t ** parsed_office_hours;
+ ngx_uint_t i, j;
+
+ parsed_office_hours = malloc(7 * sizeof(ngx_uint_t *));
+
+ hours = office_hours->elts;
+
+ /*
+ * On the configuration file, the leftmost element
+ * always applies to all remaining days, all others
+ * are read from right to left. So first we will apply
+ * the initial override, and then iterate based on the
+ * number of overrides
+ */
+
+ for (i = 0; i < WEEK_LENGTH + 1 - office_hours->nelts; ++i) {
+ parsed_office_hours[i] = parse_office_hours_string(hours[0]);
+ }
+
+ for (i = 1; i < office_hours->nelts; ++i) {
+ j = WEEK_LENGTH - office_hours->nelts + i;
+ parsed_office_hours[j] = parse_office_hours_string(hours[i]);
+ }
+
+ return parsed_office_hours;
+}
+
+/*
+ * Given a time string or the closed token, return a tuple
+ * of numbers representing opening and closing 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));
+
+ if(ngx_strcmp(office_hours.data, CLOSED_TOKEN) == 0) {
+ parsed_hours[0] = 0;
+ parsed_hours[1] = 0;
+ 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 * parse_number(office_hours, captures[2], captures[3]);
+ parsed_hours[0] = parsed_hours[0] + 60 * parse_number(office_hours, captures[4], captures[5]);
+
+ parsed_hours[1] = 60 * 60 * parse_number(office_hours, captures[6], captures[7]);
+ parsed_hours[1] = parsed_hours[1] + 60 * parse_number(office_hours, captures[8], captures[9]);
+
+ return parsed_hours;
+ }
+
+ /* Non-matching strings count as open */
+
+ parsed_hours[0] = 0;
+ parsed_hours[1] = 86400;
+ return parsed_hours;
+}
+
+/*
+ * Given an office hours array, it returns whether or not
+ * it is currently within office hours.
+ */
+
+static ngx_flag_t within_office_hours(ngx_uint_t ** office_hours)
+{
+
+ time_t now;
+ ngx_uint_t day_of_week, seconds_of_day;
+ ngx_uint_t * current_hours;
+
+ ngx_time_update();
+ now = ngx_time();
+ day_of_week = get_day_of_week(now);
+ seconds_of_day = get_seconds_of_day(now);
+ current_hours = office_hours[day_of_week];
+
+ return seconds_of_day >= current_hours[0] && seconds_of_day <= current_hours[1];
+}
+
+/*
+ * Calculate the day of the week given a timestamp
+ */
+
+static ngx_uint_t get_day_of_week(time_t time)
+{
+
+ /* Epoch was thursday, so add 3 so we start on monday */
+ return (time / 86400 + 3) % 7;
+}
+
+/*
+ * Calculate the number of seconds elapsed today
+ */
+
+static ngx_uint_t get_seconds_of_day(time_t time)
+{
+
+ return time - (time / 86400) * 86400;
+}
+
+/*
+ * Parses a string, returns 0 if match was not found
+ */
+
+static ngx_uint_t parse_number(ngx_str_t string, ngx_uint_t start, ngx_uint_t end)
+{
+
+ if (end - start == 0) {
+ return 0;
+ }
+
+ return ngx_atoi(&string.data[start], end - start);
+}
+
+/*
+ * Given the current office hours configuration it creates the custom
+ * HTML
+ */
+
+static ngx_str_t create_output_html(ngx_http_office_hours_conf_t * conf)
+{
+
+ char * output_buffer;
+ time_t now;
+ ngx_str_t output_html;
+ ngx_uint_t i, seconds_of_day;
+ ngx_uint_t ** parsed_office_hours;
+
+ size_t base_size = 1024;
+ size_t additional_info_size = conf->additional_information.len;
+ size_t buffer_size = base_size + additional_info_size + 1;
+ output_buffer = malloc(buffer_size * sizeof(char));
+
+ /* UH-OH we couldn't allocate the bufer */
+ if (!output_buffer) {
+ output_html.data = NULL;
+ output_html.len = 0;
+ return output_html;
+ }
+
+ parsed_office_hours = parse_office_hours(conf->office_hours);
+ now = ngx_time();
+ seconds_of_day = get_seconds_of_day(now);
+
+ int written = 0;
+
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) HEAD_HTML.data);
+
+ for (i = 0; i < 7; ++i) {
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", format_hours(parsed_office_hours[i], (char *) DAY_NAMES[i]));
+ }
+
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) OPEN_SERVER_TIME_HTML.data);
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", format_seconds(seconds_of_day));
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", (char *) CLOSE_SERVER_TIME_HTML.data);
+
+ if (conf->additional_information.len > 0) {
+ char additional_info[conf->additional_information.len + 1];
+ ngx_memcpy(additional_info, conf->additional_information.data, conf->additional_information.len);
+ additional_info[conf->additional_information.len] = '\0';
+ written += snprintf(output_buffer + written, buffer_size - written, "%s", additional_info);
+ }
+
+ snprintf(output_buffer + written, buffer_size - written, "%s", (char *) FOOT_HTML.data);
+
+ output_html.data = (unsigned char *) output_buffer;
+ output_html.len = strlen(output_buffer);
+
+ return output_html;
+}
+
+/*
+ * Given a tuple of seconds and a day name, outputs an HTML
+ * string containing the formatted data as a list item
+ */
+
+static char * format_hours(ngx_uint_t * hours, char * day)
+{
+
+ char * output_html;
+
+ output_html = malloc(64 * sizeof(char));
+ if (hours[0] == hours[1]) {
+ sprintf(output_html, "<li><strong>%s</strong>: CLOSED</li>",
+ day
+ );
+ return output_html;
+ }
+
+ sprintf(output_html, "<li><strong>%s</strong>: %s - %s</li>",
+ day,
+ (char *) format_seconds(hours[0]),
+ (char *) format_seconds(hours[1])
+ );
+
+ return output_html;
+}
+
+/*
+ * Given seconds of the day, it returns a string showing
+ * HH:MM in 24 hour format
+ */
+
+static char * format_seconds(ngx_uint_t seconds)
+{
+
+ char * output_html;
+ unsigned int hours, minutes;
+
+ output_html = malloc(6 * sizeof(char));
+
+ hours = seconds / 60 / 60;
+ minutes = (seconds / 60) % 60;
+
+ sprintf(output_html, "%s:%s",
+ left_pad(hours),
+ left_pad(minutes)
+ );
+
+ return output_html;
+}
+
+/*
+ * Returns a number as a string adding 0 if < 10
+ */
+
+static char * left_pad(unsigned int number)
+{
+
+ char * output_string;
+ char * padding;
+
+ padding = "";
+ output_string = malloc(4 * sizeof(char));
+
+ if (number < 10) {
+ padding = "0";
+ }
+
+ snprintf(output_string, 4, "%s%u", padding, number);
+
+ return output_string;
+}
+