From ea5480dc2ba06e6d15fb39cd7deac81c16408569 Mon Sep 17 00:00:00 2001
From: oxyc <oxyc@799618.no-reply.drupal.org>
Date: Sun, 14 Apr 2013 13:24:30 +0300
Subject: [PATCH] Issue #1969956 by oxyc: Statically cache hot date functions

---
 office_hours.module |   29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/office_hours.module b/office_hours.module
index 7ddbbab..2195313 100644
--- a/office_hours.module
+++ b/office_hours.module
@@ -496,9 +496,12 @@ function office_hours_field_formatter_view($entity_type, $entity, $field, $insta
   $open = FALSE;
   $timezone = NULL; // @TODO
 
-  $currentDT = new DateObject('now');
-  if ($timezone) {
-    $currentDT->setTimezone(new DateTimeZone($timezone));
+  static $currentDT;
+  if (!isset($currentDT)) {
+    $currentDT = new DateObject('now');
+    if ($timezone) {
+      $currentDT->setTimezone(new DateTimeZone($timezone));
+    }
   }
   $today = (int)$currentDT->format('w');  // Convert to daynumber sun=0 - sat=6.
   $next = NULL;
@@ -519,12 +522,8 @@ function office_hours_field_formatter_view($entity_type, $entity, $field, $insta
     $days[$day]['times'][] = $times;
 
     // Calculate start and end times
-    $startDT = new DateObject(_office_hours_time_to_24hr($start), NULL, 'G:i');
-    $endDT = new DateObject(_office_hours_time_to_24hr($end), NULL, 'G:i');
-    $startDT->granularity = array('hour', 'minute', 'second');
-    $endDT->granularity = array('hour', 'minute', 'second');
-    $startDT = @$startDT->merge($currentDT);
-    $endDT = @$endDT->merge($currentDT);
+    $startDT = _office_hours_calculate_time($start, $currentDT);
+    $endDT = _office_hours_calculate_time($end, $currentDT);
 
     // Are we currently open? If not, when is the next time?
     // Remember: empty days are not in $items; they are present in $days.
@@ -621,6 +620,18 @@ function office_hours_field_formatter_view($entity_type, $entity, $field, $insta
   return $element;
 }
 
+function _office_hours_calculate_time($time, $currentDT) {
+  $cache = &drupal_static(__FUNCTION__);
+  if (!isset($cache)) {
+    $cache = array();
+  }
+  if (!isset($cache[$time])) {
+    $cache[$time] = new DateObject(_office_hours_time_to_24hr($time), NULL, 'G:i');
+    $cache[$time]->granularity = array('hour', 'minute', 'second');
+    $cache[$time] = @$cache[$time]->merge($currentDT);
+  }
+  return $cache[$time];
+}
 
 /**
  * Implements hook_field_widget_info().
-- 
1.7.5.4

