### Eclipse Workspace Patch 1.0
#P almanac
Index: almanac.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/almanac/almanac.info,v
retrieving revision 1.3
diff -u -r1.3 almanac.info
--- almanac.info	30 Apr 2010 09:14:27 -0000	1.3
+++ almanac.info	28 Jan 2011 17:27:11 -0000
@@ -5,7 +5,3 @@
 php = 5.2
 files[] = almanac.module
 files[] = almanac.install
-dependencies[] = date_api
-
-
-
Index: almanac.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/almanac/almanac.install,v
retrieving revision 1.2
diff -u -r1.2 almanac.install
--- almanac.install	30 Apr 2010 09:14:27 -0000	1.2
+++ almanac.install	28 Jan 2011 17:27:11 -0000
@@ -2,11 +2,17 @@
 // $Id: almanac.install,v 1.2 2010/04/30 09:14:27 nhwebworker Exp $
 
 /**
-* Implements hook_install().
-*/
+ * @file
+ * Standard installation functions.
+ */
+
+/**
+ * Implements hook_install().
+ */
 function almanac_install() {
   drupal_set_message(st("Almanac settings are available under !link",
-    array( '!link' => l('Administer > Site configuration > almanac ',  'admin/config/almanac' ) )
+    array(
+      '!link' => l(st('Administer > Site configuration > Almanac'), 'admin/config/almanac')
+    )
   ));
 }
-?>
\ No newline at end of file
Index: almanac.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/almanac/almanac.module,v
retrieving revision 1.4
diff -u -r1.4 almanac.module
--- almanac.module	30 Apr 2010 09:14:27 -0000	1.4
+++ almanac.module	28 Jan 2011 17:27:13 -0000
@@ -4,44 +4,41 @@
 /**
  * @file
  * Almanac module provides basic sun and moon data for a given latitude and longitude..
- *
  */
 
-
 /**
- * Display help and module information
- * @param path which path of the site we're displaying help
- * @param arg array that holds the current path as would be returned from arg() function
- * @return help text for the path
+ * Implements hook_help().
  */
 function almanac_help($path, $arg) {
   $output = '';
   switch ($path) {
     case "admin/help#almanac":
-      $output = '<p>'.  t("Displays basic astronomical information for a given date") .'</p>';
-      $output .= '<p>'.  t("Use the settings page to enter location data. The path for the full page output is
-                           /almanac. The block can be configured via the normal block configurations page. ") .'</p>';
+      $output = t("<p>Displays basic astronomical information for a given date.</p>");
+      $output .= t("<p>Use the settings page to enter location data. The path for the full page output is /almanac. The block can be configured via the normal block configurations page.</p>");
       break;
   }
   return $output;
-} // function almanac_help/**
+}
 
 /**
- * Valid permissions for this module
- * @return array An array of valid permissions for the almanac module
+ * Implements hook_permission().
  */
 function almanac_permission() {
-  return array('administer almanac' =>array(
+  return array(
+    'administer almanac' => array(
       'title' => t('Administer Almanac'),
       'description' => t('Administer Almanac settings'),
     ),
-      'access almanac' =>array(
+    'access almanac' => array(
       'title' => t('Access Almanac Page'),
-      'description' => t('Whether or not to display almanac datae'),
+      'description' => t('Whether or not to display almanac data'),
     ),
   );
-} // function almanac_perm()
+}
 
+/**
+ * Implements hook_menu().
+ */
 function almanac_menu() {
   $items = array();
   $items['almanac'] = array(
@@ -57,11 +54,15 @@
     'page arguments' => array('almanac_admin'),
     'access arguments' => array('administer almanac'),
     'type' => MENU_NORMAL_ITEM,
-   );
-  
+  );
   return $items;
 }
 
+/**
+ * Custom form for the administration of the almanac module.
+ * @see almanac_admin_validate()
+ * @see system_settings_form()
+ */
 function almanac_admin() {
   $form['almanac_locationname'] = array(
     '#type' => 'textfield',
@@ -71,15 +72,15 @@
     '#maxlength' => 128,
     '#description' => t("The name of the location for which data will be computed."),
     '#required' => TRUE,
-  );  
+  );
   $form['almanac_zoneoffset'] = array(
     '#type' => 'select',
     '#title' => t('Location Time Zone'),
-    '#options' => date_timezone_names(),
-    '#default_value' => variable_get('almanac_zoneoffset','Europe/Bucharest'),
+    '#options' => system_time_zones(),
+    '#default_value' => variable_get('almanac_zoneoffset', date_default_timezone_get()),
     '#description' => t("Time zone of location."),
     '#required' => TRUE,
-  );  
+  );
   $form['almanac_latitude'] = array(
     '#type' => 'textfield',
     '#title' => t('Location Latitude'),
@@ -101,9 +102,11 @@
   $form['almanac_timestring'] = array(
     '#type' => 'radios',
     '#title' => t('Time display format'),
-    '#options' => array(t('24 hour display, e.g., 09:00, 19:00'), 
-        t('12 hour display, lower case am/pm, e.g., 9:00 am, 7:00 pm'),
-        t('12 hour display, upper case AM/PM, e.g., 9:00 AM, 7:00 PM')),        
+    '#options' => array(
+      t('24 hour display, e.g., 09:00, 19:00'),
+      t('12 hour display, lower case am/pm, e.g., 9:00 am, 7:00 pm'),
+      t('12 hour display, upper case AM/PM, e.g., 9:00 AM, 7:00 PM'),
+    ),
     '#default_value' => variable_get('almanac_timestring', 1),
     '#description' => t("Controls how sun and moon data within the output tables are displayed."),
   );
@@ -111,9 +114,8 @@
 }
 
 /**
- * Validate admin settings
+ * Validate admin settings form.
  */
-
 function almanac_admin_validate($form, &$form_state) {
   $longitude = $form_state['values']['almanac_longitude'];
   $latitude = $form_state['values']['almanac_latitude'];
@@ -123,76 +125,75 @@
   if (abs($longitude) > 180 || abs($latitude) > 90) {
     form_set_error('almanac_coordinates', t('You must enter a numeric value from -180 to 180 for longitude and -90 to 90 for latitude'));
   }
- 
-} //end function almanac_admin_validate
+
+}
 
 /* --------- Main module code starts here ---------- */
 
 /**
- * Generate HTML for the almanac block
- * @param op the operation from the URL
- * @param delta offset
- * @returns block HTML
+ * Implements hook_block_info().
  */
 function almanac_block_info() {
   $blocks = array();
-  $blocks['almanac'] = array (
+  $blocks['almanac'] = array(
     'info' => t('Almanac'),
   );
-  #exit;
   return $blocks;
 }
 
+/**
+ * Implements hook_block_view().
+ */
 function almanac_block_view() {
   // get UTC timestamp
-  $utc_time = date_make_date('now','UTC');
-  $utc_time_array = date_array($utc_time);
-  $utc_timestamp = $utc_time_array["0"];
+  $utc_timestamp = date('U');
+
   // get location, location latitude and longitude, location timezone, and location GMT offet
   $location = variable_get('almanac_locationname', 'Bucuresti');
   $latitude = variable_get('almanac_latitude', 44.4325);
   $raw_longitude = variable_get('almanac_longitude',  -26.103889);
-  $timezone = variable_get('almanac_zoneoffset', 'Europe/Bucharest');
-  $location_time = date_make_date('now', $timezone);
-  $loc_time = date_array($location_time);     
-  $gmtoffset = ($location_time -> getOffset());
+  $timezone = variable_get('almanac_zoneoffset', date_default_timezone_get());
+
+  // Create a DateTime object from the timestamp & set the timezone.
+  $location_time = date_create();
+  date_timezone_set($location_time, timezone_open($timezone));
+  $gmtoffset = $location_time->getOffset();
+
   // get server location GMT offset
-  $server_time = gettimeofday();    
-  $app_gmtoffset = -1 * $server_time['minuteswest']*60;
+  $server_time = gettimeofday();
+  $app_gmtoffset = -1 * $server_time['minuteswest'] * 60;
   /* for block output, just basic sunrise/set, moonrise/set is enough, at least for now.
-   * 
+   *
    * compute rise and sset GMT times, based on location, UTC timestamp, and GMT offset
    *
    * compute sunrise and sunset time in location time zone by adding GMT offset and server default
    * timezone offset from GMT. Get minutes west and then make negative to keep with convention of
-   * west of GMT is negative.    
+   * west of GMT is negative.
    *
    * php sunrise/sunset functions use east latitude is positive convention
    *
    */
   $longitude = -$raw_longitude;
   $sunrise_times = almanac_risetimes($utc_timestamp, $latitude, $longitude, $gmtoffset);
-  $sunset_times = almanac_settimes($utc_timestamp, $latitude, $longitude, $gmtoffset); 
-  $local_sunrise_time = $sunrise_times['daylight'] -$app_gmtoffset + $gmtoffset;
-  $local_sunset_time = $sunset_times['daylight'] -$app_gmtoffset + $gmtoffset;
+  $sunset_times = almanac_settimes($utc_timestamp, $latitude, $longitude, $gmtoffset);
+  $local_sunrise_time = $sunrise_times['daylight'] - $app_gmtoffset + $gmtoffset;
+  $local_sunset_time = $sunset_times['daylight'] - $app_gmtoffset + $gmtoffset;
   /*
    * Calculate moon rise and set. Use server date information as basis to calculate
    * modified julian date, so GMT offset of location needs to be restated in seconds.
    * Calculations use east longitude negative convention.
    */
-  $cdate = getdate(); 
+  $cdate = getdate();
   $year = $cdate['year'];
   $month = $cdate['mon'];
   $day = $cdate['mday'];
-  $hour = 0;    
   $longitude = $raw_longitude;
-  $zone_ut_adder= $gmtoffset/3600;
-  $moontimes = almanac_moontimes($year, $month, $day, $hour, $zone_ut_adder, $latitude, $longitude);
-  //
-   
+  $zone_ut_adder= $gmtoffset / 3600;
+  $moontimes = almanac_moontimes($year, $month, $day, $zone_ut_adder, $latitude, $longitude);
+
   $block_content = '';
-  $block_content .= theme_almanac_block($location,$location_time,$local_sunrise_time,$local_sunset_time,
-    $moontimes['rise'],$moontimes['set']);
+  $block_content .= theme_almanac_block($location, $location_time, $local_sunrise_time, $local_sunset_time,
+    $moontimes['rise'], $moontimes['set']);
   $block['subject'] = 'Current date and time';
   $block['content'] = $block_content;
   return $block;
@@ -200,35 +201,40 @@
 
 // almanac page
 function almanac_page() {
-  $utc_time = date_make_date('now','UTC');
-  $utc_time_array = date_array($utc_time);
-  $utc_timestamp = $utc_time_array["0"];
+  // Timestamp
+  $utc_timestamp = date('U');
+
   // get location, location latitude and longitude, location timezone, and location GMT offet
   $location = variable_get('almanac_locationname', 'Bucuresti');
   $latitude = variable_get('almanac_latitude', 44.4325);
   $raw_longitude = variable_get('almanac_longitude',  -26.103889);
-  $timezone = variable_get('almanac_zoneoffset', 'Europe/Bucharest');
-  $location_time = date_make_date('now', $timezone);
-  $loc_time = date_array($location_time);     
-  $gmtoffset = ($location_time -> getOffset());
+  $timezone = variable_get('almanac_zoneoffset', date_default_timezone_get());
+
+  // Create a DateTime object from the timestamp & set the timezone.
+  $location_time = date_create();
+  date_timezone_set($location_time, timezone_open($timezone));
+
+  $gmtoffset = $location_time->getOffset();
+
   // get server location GMT offset
-  $server_time = gettimeofday();    
-  $app_gmtoffset = -1 * $server_time['minuteswest']*60;  
-  /* 
+  $server_time = gettimeofday();
+  $app_gmtoffset = -1 * $server_time['minuteswest'] * 60;
+
+  /*
    * compute rise and sset GMT times, based on location, UTC timestamp, and GMT offset
    *
    * compute sunrise and sunset time in location time zone by adding GMT offset and server default
    * timezone offset from GMT. Get minutes west and then make negative to keep with convention of
-   * west of GMT is negative.    
+   * west of GMT is negative.
    *
    * php sunrise/sunset functions use east latitude is positive convention
    *
    */
   $longitude = -$raw_longitude;
   $sunrise_times = almanac_risetimes($utc_timestamp, $latitude, $longitude, $gmtoffset);
-  $sunset_times = almanac_settimes($utc_timestamp, $latitude, $longitude, $gmtoffset); 
+  $sunset_times = almanac_settimes($utc_timestamp, $latitude, $longitude, $gmtoffset);
   // use brute force calculations instead of array iterations, as size of array is predictable and
-  // it ends up with about same number of lines anyway  
+  // it ends up with about same number of lines anyway
   $sunrise_times['daylight'] =  $sunrise_times['daylight'] - $app_gmtoffset + $gmtoffset;
   $sunrise_times['civil'] =  $sunrise_times['civil'] - $app_gmtoffset + $gmtoffset;
   $sunrise_times['nautical'] =  $sunrise_times['nautical'] - $app_gmtoffset + $gmtoffset;
@@ -238,14 +244,14 @@
   $sunset_times['civil'] =  $sunset_times['civil'] - $app_gmtoffset + $gmtoffset;
   $sunset_times['nautical'] =  $sunset_times['nautical'] - $app_gmtoffset + $gmtoffset;
   $sunset_times['astronomical'] =  $sunset_times['astronomical'] - $app_gmtoffset + $gmtoffset;
-  
+
   /* calculate number of hours of daylight
-    
+
   $diff_seconds = $sunset_times['daylight'] - $sunrise_times['daylight'];
   $diff_hours = floor($diff_seconds/3600);
   $diff_seconds -= $diff_hours * 3600;
   $diff_minutes = floor($diff_seconds/60);
-  $diff_seconds -= $diff_minutes * 60;    
+  $diff_seconds -= $diff_minutes * 60;
   // $page_content .= t('Length of Day:' . $diff_hours . 'hrs' . $diff_minutes . 'min' . $diff_seconds . 'sec' . '<br />');
 
   /*
@@ -253,29 +259,28 @@
    * modified julian date, so GMT offset of location needs to be restated in seconds.
    * Calculations use east longitude negative convention.
    */
-  $cdate = getdate(); 
+  $cdate = getdate();
   $year = $cdate['year'];
   $month = $cdate['mon'];
   $day = $cdate['mday'];
-  $hour = 0;    
   $longitude = $raw_longitude;
   $zone_ut_adder= $gmtoffset/3600;
-  $moontimes = almanac_moontimes($year, $month, $day, $hour, $zone_ut_adder, $latitude, $longitude);
+  $moontimes = almanac_moontimes($year, $month, $day, $zone_ut_adder, $latitude, $longitude);
   $moonrise_time = $moontimes['rise'] - $app_gmtoffset + $gmtoffset;
   $moonset_time = $moontimes['set'] - $app_gmtoffset + $gmtoffset;
   //
   $page_content = '';
-  $page_content .= theme_almanac_page($location,$location_time,$sunrise_times,$sunset_times,
-    $moontimes['rise'],$moontimes['set']);
+  $page_content .= theme_almanac_page($location, $location_time, $sunrise_times,
+      $sunset_times, $moontimes['rise'], $moontimes['set']);
 
-return $page_content;
-}  
+  return $page_content;
+}
 
 /**
  * return array of timestamps for sunrise event at lat/long at specified time
  */
 function almanac_risetimes($inquiry_time, $latitude, $longitude, $gmtoffset) {
-  $official_zenith = 90 + 50/60;
+  $official_zenith = 90 + 50 / 60;
   $civil_zenith = 96;
   $nautical_zenith = 102;
   $astronomical_zenith = 108;
@@ -292,13 +297,13 @@
  * return array of timestamps for sunset at lat/long at specified time
  */
 function almanac_settimes($inquiry_time, $latitude, $longitude, $gmtoffset) {
-  $official_zenith = 90 + 50/60;
+  $official_zenith = 90 + 50 / 60;
   $civil_zenith = 96;
   $nautical_zenith = 102;
   $astronomical_zenith = 108;
   //drop use of gmtoffset in function use for now
   $set_times = array(
-    'daylight' => date_sunset($inquiry_time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $official_zenith), 
+    'daylight' => date_sunset($inquiry_time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $official_zenith),
     'civil' => date_sunset($inquiry_time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $civil_zenith),
     'nautical' => date_sunset($inquiry_time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $nautical_zenith),
     'astronomical' => date_sunset($inquiry_time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, $astronomical_zenith));
@@ -306,16 +311,16 @@
 }
 
 /**
-* return formatted array of relevant date/times. Currently not used, but left in just in case.
-*/
-function format_rise_set_times($time_array,$timezone) {
+ * return formatted array of relevant date/times. Currently not used, but left in just in case.
+ */
+function format_rise_set_times($time_array, $timezone) {
   $formatted_rise_set_times = array(
-    'daylight' => format_date($time_array['daylight'], $type = 'medium', $format = '', $timezone, $langcode = NULL), 
-    'civil' => format_date($time_array['civil'], $type = 'medium', $format = '', $timezone, $langcode = NULL),
-    'nautical' => format_date($time_array['nautical'], $type = 'medium', $format = '', $timezone, $langcode = NULL),
-    'astronomical' => format_date($time_array['astronomical'], $type = 'medium', $format = '', $timezone, $langcode = NULL));
+    'daylight' => format_date($time_array['daylight'], 'medium', '', $timezone),
+    'civil' => format_date($time_array['civil'], 'medium', '', $timezone),
+    'nautical' => format_date($time_array['nautical'], 'medium', '', $timezone),
+    'astronomical' => format_date($time_array['astronomical'], 'medium', '', $timezone));
   return $formatted_rise_set_times;
-}  
+}
 
 /*
  * Calculate moon rise and set times.  Use low resolution algorithm outlined in Montenbruck and Pflgler's
@@ -323,143 +328,148 @@
  * the angle of the moon above the horizon (y axis) at the location coordinates. The function crosses
  * the x-axis at the time of rising and setting.
  */
-function almanac_moontimes($year, $month, $day, $hour, $zone_ut_adder, $latitude, $longitude) {
-$hour=0;
-$zone_adjust = $zone_ut_adder/24;
-$adj_date = almanac_modjd($year, $month, $day, $hour) - $zone_adjust;
-$sine_lat = sin(deg2rad($latitude));
-$cos_lat = cos(deg2rad($latitude));
-$sine_h0 = sin(deg2rad(8/60));
-
-$xe = 0;
-$ye = 0;
-$z1 = 0;
-$z2 = 0;
-
-$utrise = 0;
-$utset = 0;
-$rise = 0;
-$sett = 0;
-$hour = 1;
-$zero2 = 0;
-$hour_zero = $hour-1;
+function almanac_moontimes($year, $month, $day, $zone_ut_adder, $latitude, $longitude) {
+  $hour = 0;
+  $zone_adjust = $zone_ut_adder / 24;
+  $adj_date = almanac_modjd($year, $month, $day, $hour) - $zone_adjust;
+  $sine_lat = sin(deg2rad($latitude));
+  $cos_lat = cos(deg2rad($latitude));
+  $sine_h0 = sin(deg2rad(8 / 60));
+
+  $xe = 0;
+  $ye = 0;
+  $z1 = 0;
+  $z2 = 0;
+
+  $utrise = 0;
+  $utset = 0;
+  $rise = 0;
+  $sett = 0;
+  $hour = 1;
+  $zero2 = 0;
+  $hour_zero = $hour - 1;
 
-/* calculate sine of altitude at time 0 */
-$ym = sinalt($adj_date, $hour_zero, $longitude, $cos_lat, $sine_lat) - $sine_h0;
+  // calculate sine of altitude at time 0
+  $ym = sinalt($adj_date, $hour_zero, $longitude, $cos_lat, $sine_lat) - $sine_h0;
 ( $ym > 0 ) ? $above=1 : $above = 0;
-/*
- * main execution loop. calculate the sine of the angle of the object over 3 hour period.
- * Fit a simple parabola to the 3 height points, as a second order polynomial can be easily 
- * calculated and will fit 3 points perfectly. If parabola has a root during the period,
- * then body has crossed the horizon and has either risen or set. Use the interpolated root value
- * as that time. 
- */
-do {
- $next_hour = $hour +1;
- $y0 = sinalt($adj_date, $hour, $longitude, $cos_lat, $sine_lat) - $sine_h0;
- $yp = sinalt($adj_date, $next_hour, $longitude, $cos_lat, $sine_lat) - $sine_h0;
- // y0 is difference between sine of elevation at current hour and at 0 hour, ym one hour behind, yp one hour ahead.
- $quadfit = quad($ym, $y0, $yp);
- $xe = $quadfit['xe'];
- $ye = $quadfit['ye'];
- $z1 = $quadfit['z1'];
- $z2 = $quadfit['z2'];
- $nz = $quadfit['nz'];
- switch ($nz) {
-   case 0: // nothing  - no root during period, so go to next perod
-     break;
-   case 1:                      // simple rise / set event
-     if ($ym < 0) {             // rise event
-       $utrise = $hour + $z1;
-       $rise = 1; 
-     }  
-     else {                     // set event
-       $utset = $hour + $z1;
-       $sett = 1; 
-     }
-       break;  
-   case 2 :                     // rises and sets within interval
-     if ($ye < 0) {             // minimum - so set then rise
-       $utrise = $hour + $z2;
-       $utset = $hour + $z1; 
-     }
-     else {                     // maximum - so rise then set
-       $utrise = $hour + $z1;
-       $utset = $hour + $z2;}
-       $rise = 1;
-       $sett = 1;
-       $zero2 = 1;
-     }
-     $ym = $yp;     //reuse the ordinate in the next interval
-     $hour = $hour + 2;
-}
-while ( ($hour < 25) && ($rise * $sett <> 1) );
-/* end main loop */
-$utrise = hours_minutes($utrise);
-$moonrise = mktime($utrise['hours'],$utrise['minutes'],0,$month,$day,$year);
-$utset = hours_minutes($utset);
-$moonset = mktime($utset['hours'],$utset['minutes'],0,$month,$day,$year);
-$moontimes = array('rise' => $moonrise, 'set' => $moonset);
-return $moontimes;
+
+  /*
+   * main execution loop. calculate the sine of the angle of the object over 3 hour period.
+   * Fit a simple parabola to the 3 height points, as a second order polynomial can be easily
+   * calculated and will fit 3 points perfectly. If parabola has a root during the period,
+   * then body has crossed the horizon and has either risen or set. Use the interpolated root value
+   * as that time.
+   */
+  do {
+    $next_hour = $hour + 1;
+    $y0 = sinalt($adj_date, $hour, $longitude, $cos_lat, $sine_lat) - $sine_h0;
+    $yp = sinalt($adj_date, $next_hour, $longitude, $cos_lat, $sine_lat) - $sine_h0;
+    // y0 is difference between sine of elevation at current hour and at 0 hour, ym one hour behind, yp one hour ahead.
+    $quadfit = quad($ym, $y0, $yp);
+    $xe = $quadfit['xe'];
+    $ye = $quadfit['ye'];
+    $z1 = $quadfit['z1'];
+    $z2 = $quadfit['z2'];
+    $nz = $quadfit['nz'];
+    switch ($nz) {
+      case 0: // nothing  - no root during period, so go to next perod
+        break;
+
+      case 1:                      // simple rise / set event
+      if ($ym < 0) {             // rise event
+        $utrise = $hour + $z1;
+        $rise = 1;
+      }
+      else {                     // set event
+        $utset = $hour + $z1;
+        $sett = 1;
+      }
+      break;
+
+      case 2 :                     // rises and sets within interval
+        if ($ye < 0) {             // minimum - so set then rise
+          $utrise = $hour + $z2;
+          $utset = $hour + $z1;
+        }
+        else {                     // maximum - so rise then set
+          $utrise = $hour + $z1;
+          $utset = $hour + $z2;}
+          $rise = 1;
+          $sett = 1;
+          $zero2 = 1;
+        }
+        $ym = $yp;     // reuse the ordinate in the next interval
+        $hour = $hour + 2;
+  }
+  /* end main loop */
+  while (($hour < 25) && ($rise * $sett <> 1));
+  $utrise = hours_minutes($utrise);
+  $moonrise = mktime($utrise['hours'], $utrise['minutes'], 0, $month, $day, $year);
+  $utset = hours_minutes($utset);
+  $moonset = mktime($utset['hours'], $utset['minutes'], 0, $month, $day, $year);
+  $moontimes = array('rise' => $moonrise, 'set' => $moonset);
+  return $moontimes;
 }
 
-/*
+/**
  * returns sine of the altitude of the moon given time and geographic location
  */
-function sinalt($mjd0, $hour, $glong, $cphi, $sphi) { 
-$instant = $mjd0 + ($hour/24) ;
-$t = ($instant - 51544.5)/36525; //mjd of instant in julian centuries J2000.0
-$moon_pos = almanac_mooncalc($t);
-$tau = 15 * (lmst($instant, $glong) - $moon_pos['ra']);   //hour angle of object
-$sinalt = $sphi * sin(deg2rad($moon_pos['dec'])) + $cphi * cos(deg2rad($moon_pos['dec'])) * cos(deg2rad($tau));
-return $sinalt;
+function sinalt($mjd0, $hour, $glong, $cphi, $sphi) {
+  $instant = $mjd0 + ($hour / 24) ;
+  $t = ($instant - 51544.5) / 36525; //mjd of instant in julian centuries J2000.0
+  $moon_pos = almanac_mooncalc($t);
+  $tau = 15 * (lmst($instant, $glong) - $moon_pos['ra']);   //hour angle of object
+  $sinalt = $sphi * sin(deg2rad($moon_pos['dec'])) + $cphi * cos(deg2rad($moon_pos['dec'])) * cos(deg2rad($tau));
+  return $sinalt;
 }
 
 /**
  * Returns ra and dec of Moon at given time. Algorithm detailed in Montenbruck and Pflegler (1989)
  */
 function almanac_mooncalc($t) {
-$two_pi = 2*M_PI;
-$arc = 206264.8062;
-$coseps = .91748;
-$sineps = .39778;
-$l0 = fpart(.606433 + 1336.855225 * $t);       //mean long Moon in revs
-$l = $two_pi * fpart(.374897 + 1325.55241 * $t);   //mean anomaly of Moon
-$ls = $two_pi * fpart(.993133 + 99.997361 * $t);   //mean anomaly of Sun
-$d = $two_pi * fpart(.827361 + 1236.853086 * $t);  //diff longitude sun and moon
-$f = $two_pi * fpart(.259086 + 1342.227825 * $t);  //mean arg latitude
-/*longitude and latitude perturbation terms. */
-$dl = 22640 * sin($l) - 4586 * sin($l - 2 * $d) + 2370 * sin(2 * $d) + 769 * sin(2 * $l)
-      - 668 * sin($ls) - 412 * sin(2 * $f) - 212 * sin(2 * $l - 2 * $d) - 206 * sin($l + $ls - 2 * $d)
-      + 192 * sin($l + 2 * $d) - 165 * sin($ls - 2 * $d) - 125 * sin($d) - 110 * sin($l + $ls)
-      + 148 * sin($l - $ls) - 55 * sin(2 * $f - 2 * $d);
-$s = $f + ($dl + 412 * sin(2 * $f) + 541 * sin($ls)) / $arc;
-$h = $f - 2 * $d;
-$n = -526 * sin($h) + 44 * sin($l + $h) - 31 * sin($h - $l) - 23 * sin($ls + $h)
-     + 11 * sin($h - $ls) - 25 * sin($f - 2 * $l) + 21 * sin($f - $l);
-$lmoon = $two_pi * fpart($l0 + $dl / 1296000);   //latitude in rads
-$bmoon = (18520 * sin($s) + $n) / $arc;          //longitude in rads
-/* convert to equatorial coords */
-$cb = cos($bmoon);
-$x = $cb * cos($lmoon);
-$v = $cb * sin($lmoon);
-$w = sin($bmoon);
-$y = $coseps * $v - $sineps * $w;
-$z = $sineps * $v + $coseps * $w;
-$rho = sqrt(1 - $z * $z);
-$dec = (360 / $two_pi) * atan($z/$rho);
-$ra = (48 / $two_pi) * atan($y / ($x + $rho));
-if ( $ra < 0 ) {$ra = $ra + 24;}
-$moon = array('ra' => $ra, 'dec' => $dec);
-return $moon;
+  $two_pi = 2*M_PI;
+  $arc = 206264.8062;
+  $coseps = 0.91748;
+  $sineps = 0.39778;
+  $l0 = fpart(0.606433 + 1336.855225 * $t);       //mean long Moon in revs
+  $l = $two_pi * fpart(0.374897 + 1325.55241 * $t);   //mean anomaly of Moon
+  $ls = $two_pi * fpart(0.993133 + 99.997361 * $t);   //mean anomaly of Sun
+  $d = $two_pi * fpart(0.827361 + 1236.853086 * $t);  //diff longitude sun and moon
+  $f = $two_pi * fpart(0.259086 + 1342.227825 * $t);  //mean arg latitude
+  /* longitude and latitude perturbation terms. */
+  $dl = 22640 * sin($l) - 4586 * sin($l - 2 * $d) + 2370 * sin(2 * $d) + 769 * sin(2 * $l)
+        - 668 * sin($ls) - 412 * sin(2 * $f) - 212 * sin(2 * $l - 2 * $d) - 206 * sin($l + $ls - 2 * $d)
+        + 192 * sin($l + 2 * $d) - 165 * sin($ls - 2 * $d) - 125 * sin($d) - 110 * sin($l + $ls)
+        + 148 * sin($l - $ls) - 55 * sin(2 * $f - 2 * $d);
+  $s = $f + ($dl + 412 * sin(2 * $f) + 541 * sin($ls)) / $arc;
+  $h = $f - 2 * $d;
+  $n = -526 * sin($h) + 44 * sin($l + $h) - 31 * sin($h - $l) - 23 * sin($ls + $h)
+       + 11 * sin($h - $ls) - 25 * sin($f - 2 * $l) + 21 * sin($f - $l);
+  $lmoon = $two_pi * fpart($l0 + $dl / 1296000);   //latitude in rads
+  $bmoon = (18520 * sin($s) + $n) / $arc;          //longitude in rads
+  /* convert to equatorial coords */
+  $cb = cos($bmoon);
+  $x = $cb * cos($lmoon);
+  $v = $cb * sin($lmoon);
+  $w = sin($bmoon);
+  $y = $coseps * $v - $sineps * $w;
+  $z = $sineps * $v + $coseps * $w;
+  $rho = sqrt(1 - $z * $z);
+  $dec = (360 / $two_pi) * atan($z/$rho);
+  $ra = (48 / $two_pi) * atan($y / ($x + $rho));
+  if ( $ra < 0 ) {
+    $ra = $ra + 24;
+  }
+  $moon = array('ra' => $ra, 'dec' => $dec);
+  return $moon;
 }
 
-/*---------------- other functions ----------------*/
+/* ---------------- other functions ---------------- */
 
 // returns fractional part of a number
 function fpart($xvalue) {
-$xfraction = abs($xvalue) - floor(abs($xvalue));
-return $xfraction;
+  $xfraction = abs($xvalue) - floor(abs($xvalue));
+  return $xfraction;
 }
 
 /*
@@ -467,23 +477,23 @@
  * which is base date of modified julian calendar system
  */
 function almanac_modjd($year, $month, $day, $hour) {
-$day1 = gregoriantojd($month, $day, $year);
-$day0 = gregoriantojd(11, 17, 1858);
-$modjd = $day1 - $day0 + $hour/24;
-return $modjd;
+  $day1 = gregoriantojd($month, $day, $year);
+  $day0 = gregoriantojd(11, 17, 1858);
+  $modjd = $day1 - $day0 + $hour/24;
+  return $modjd;
 }
 
 /**
  * returns the local siderial time for the modified julian date and longitude
  */
 function lmst($mjd, $glong) {
-$mjd0 = intval($mjd);
-$ut = ($mjd - $mjd0) * 24;
-$t = ($mjd0 - 51544.5) / 36525;
-$gmst = 6.697374558 + 1.0027379093 * $ut;
-$gmst = $gmst + (8640184.812866 + (.093104 - .0000062 * $t) * $t) * $t / 3600;
-$lmst = 24 * fpart(($gmst - $glong / 15) / 24);
-return $lmst;
+  $mjd0 = intval($mjd);
+  $ut = ($mjd - $mjd0) * 24;
+  $t = ($mjd0 - 51544.5) / 36525;
+  $gmst = 6.697374558 + 1.0027379093 * $ut;
+  $gmst = $gmst + (8640184.812866 + (0.093104 - 0.0000062 * $t) * $t) * $t / 3600;
+  $lmst = 24 * fpart(($gmst - $glong / 15) / 24);
+  return $lmst;
 }
 
 /**
@@ -492,69 +502,69 @@
  * plus number of found roots ($nz)
  */
 function quad($yminus, $y0, $yplus) {
-$nz = (int) 0;
-// determine coefficients a, b, c of y=a * x^2 + b * x +c
-// remember quadratic equation from school?
-$a = .5 * ($yminus + $yplus) - $y0;
-$b = .5 * ($yplus - $yminus);
-$c = $y0;
-// calculate extreme values
-$xe = (-1* $b) / (2 * $a);       
-$ye = ($a * $xe + $b) * $xe + $c;
-// compute discriminant
-$dis = $b * $b - 4 * $a * $c;
-// if roots exist, compute        
-if ( $dis >= 0 ) {
-  $dx = .5 * sqrt($dis) / abs($a);
-  $z1 = $xe - $dx; // root1
-  $z2 = $xe + $dx; // root2
-  (abs($z1) <= 1) ? ++$nz : $nz ;
-  (abs($z2) <= 1) ? ++$nz : $nz ;
-  ($z1 < -1) ? $z1 = $z2 : $z1 ;
-}
-$quad = array('xe' => $xe, 'ye' => $ye, 'z1' => $z1, 'z2' => $z2, 'nz' => $nz );
-return $quad;
+  $nz = (int) 0;
+  // determine coefficients a, b, c of y=a * x^2 + b * x +c
+  // remember quadratic equation from school?
+  $a = 0.5 * ($yminus + $yplus) - $y0;
+  $b = 0.5 * ($yplus - $yminus);
+  $c = $y0;
+  // calculate extreme values
+  $xe = (-1* $b) / (2 * $a);
+  $ye = ($a * $xe + $b) * $xe + $c;
+  // compute discriminant
+  $dis = $b * $b - 4 * $a * $c;
+  // if roots exist, compute
+  if ( $dis >= 0 ) {
+    $dx = 0.5 * sqrt($dis) / abs($a);
+    $z1 = $xe - $dx; // root1
+    $z2 = $xe + $dx; // root2
+    (abs($z1) <= 1) ? ++$nz : $nz ;
+    (abs($z2) <= 1) ? ++$nz : $nz ;
+    ($z1 < -1) ? $z1 = $z2 : $z1 ;
+  }
+  $quad = array('xe' => $xe, 'ye' => $ye, 'z1' => $z1, 'z2' => $z2, 'nz' => $nz );
+  return $quad;
 }
 
-/** 
- * converts decimal time to array o hours and minutes 
+/**
+ * converts decimal time to array o hours and minutes
  * returns number containing the time written in hours and minute rounded to the nearest minute
  */
 function hours_minutes($time) {
-$ut = intval($time * 60 + .5) / 60;   //round ut to nearest minute
-$hours_minutes['hours'] = intval($ut);
-$hours_minutes['minutes'] = intval(60 * ($ut - $hours_minutes['hours']) + .5);
-return $hours_minutes;
+  $ut = intval($time * 60 + 0.5) / 60;   //round ut to nearest minute
+  $hours_minutes['hours'] = intval($ut);
+  $hours_minutes['minutes'] = intval(60 * ($ut - $hours_minutes['hours']) + 0.5);
+  return $hours_minutes;
 }
 
-/*---------------- theme functions ----------------*/
+/* ---------------- theme functions ---------------- */
 
-/*
- * implementation of hook_theme
- * 
+/**
+ * Implements hook_theme().
  */
 function almanac_theme() {
   return array(
     'almanac_block' => array(
-      'arguments' => array('location' => NULL, 'location_time' => NULL, 'sunrise_time' => NULL, 
-      'sunset_time' => NULL, 'moonrise_time' => NULL, 'moonset_time' => NULL),
+      'variables' => array(
+        'location' => NULL, 'location_time' => NULL, 'sunrise_time' => NULL,
+        'sunset_time' => NULL, 'moonrise_time' => NULL, 'moonset_time' => NULL),
     ),
-     'almanac_page' => array(
-      'arguments' => array('location' => NULL, 'location_time' => NULL, 'sunrise_times_array' => NULL, 
-      'sunset_times_array' => NULL, 'moonrise_time' => NULL, 'moonset_time' => NULL),
+    'almanac_page' => array(
+      'variables' => array(
+        'location' => NULL, 'location_time' => NULL, 'sunrise_times_array' => NULL,
+        'sunset_times_array' => NULL, 'moonrise_time' => NULL, 'moonset_time' => NULL),
     ),
   );
 }
 
 /*
- * hook for block output
- * 
+ * Implements hook_theme().
  */
-function theme_almanac_block($location,$location_time,$sunrise_time,$sunset_time,$moonrise_time,$moonset_time) {
-  drupal_add_css(drupal_get_path('module', 'almanac') .'/almanac.css');
+function theme_almanac_block($location, $location_time, $sunrise_time, $sunset_time, $moonrise_time, $moonset_time) {
+  drupal_add_css(drupal_get_path('module', 'almanac') . '/almanac.css');
   $time_format = variable_get('almanac_timestring', 1);
   switch ($time_format) {
-    case 0: 
+    case 0:
       $time_string = 'H:i';
       break;
     case 1:
@@ -562,33 +572,37 @@
       break;
     case 2:
       $time_string = 'g:i A';
-      break;     
+      break;
   }
   $output = '';
   $output .= '<div id="almanac_block">';
-  $output.= '<h3 class="location">' . t($location) .' </h3>';
-  $formatted_ctime = date_format_date($location_time,'short');
+  $output .= '<h3 class="location">' . t($location) . '</h3>';
+
+  // Preserve the timezone as we call the native Drupal date function.
+  $tz = $location_time->getTimezone();
+  $formatted_ctime = format_date($location_time->format('U'), 'short', '', $tz->getName());
+
   $output .= '<p class="time">' . $formatted_ctime . '</p>';
   $sunrise_time = date($time_string, $sunrise_time);
   $sunset_time = date($time_string, $sunset_time);
   $moonrise_time = date($time_string, $moonrise_time);
   $moonset_time = date($time_string, $moonset_time);
   $output .= '<table>';
-  $output .= '<tr><th>&nbsp;</th><th>'. t('Rise') .'</th><th>' . t('Set') . '</th></tr>';
+  $output .= '<tr><th>&nbsp;</th><th>' . t('Rise') . '</th><th>' . t('Set') . '</th></tr>';
   $output .= '<tr><td>' . t('Sun') . '</td><td>' . $sunrise_time . '</td>';
-  $output .= '<td>' . $sunset_time . '</td></tr>';  
+  $output .= '<td>' . $sunset_time . '</td></tr>';
   $output .= '<tr><td>' . t('Moon') . '</td><td>' . $moonrise_time . '</td>';
-  $output .= '<td>' . $moonset_time . '</td></tr>';  
+  $output .= '<td>' . $moonset_time . '</td></tr>';
   $output .= '</table>';
   $output .= '</div>';
   return $output;
 }
 
-function theme_almanac_page($location,$location_time,$sunrise_times_array,$sunset_times_array,$moonrise_time,$moonset_time) {
-  drupal_add_css(drupal_get_path('module', 'almanac') .'/almanac.css');
+function theme_almanac_page($location, $location_time, $sunrise_times_array, $sunset_times_array, $moonrise_time, $moonset_time) {
+  drupal_add_css(drupal_get_path('module', 'almanac') . '/almanac.css');
   $time_format = variable_get('almanac_timestring', 1);
   switch ($time_format) {
-    case 0: 
+    case 0:
       $time_string = 'H:i';
       break;
     case 1:
@@ -596,14 +610,14 @@
       break;
     case 2:
       $time_string = 'g:i A';
-      break;     
-  }  
+      break;
+  }
   $output = '';
-  $output .= '<div id="almanac_page">';   
-  $output .= '<h2>' . t($location) .' </h2>';
+  $output .= '<div id="almanac_page">';
+  $output .= '<h2>' . t($location) . '</h2>';
   $output .= '<table>';
-  $output .= '<tr><th>&nbsp;</th><th>'. t('Rise') .'</th><th>' . t('Set') . '</th></tr>';
-  
+  $output .= '<tr><th>&nbsp;</th><th>' . t('Rise') . '</th><th>' . t('Set') . '</th></tr>';
+
   $formatted_sunrise_times['daylight'] =  date($time_string, $sunrise_times_array['daylight']);
   $formatted_sunrise_times['civil'] =  date($time_string, $sunrise_times_array['civil']);
   $formatted_sunrise_times['nautical'] =  date($time_string, $sunrise_times_array['nautical']);
@@ -613,10 +627,10 @@
   $formatted_sunset_times['civil'] =  date($time_string, $sunset_times_array['civil']);
   $formatted_sunset_times['nautical'] =  date($time_string, $sunset_times_array['nautical']);
   $formatted_sunset_times['astronomical'] =  date($time_string, $sunset_times_array['astronomical']);
-  
-  $moonrise_time = date($time_string,$moonrise_time);
-  $moonset_time = date($time_string,$moonset_time);
-  
+
+  $moonrise_time = date($time_string, $moonrise_time);
+  $moonset_time = date($time_string, $moonset_time);
+
   $output .= '<tr><td>' . t('Astronomical Twilight') . '</td><td>' . $formatted_sunrise_times['astronomical'] . '</td>';
   $output .= '<td>' . $formatted_sunset_times['astronomical'] . '</td></tr>';
   $output .= '<tr><td>' . t('Nautical Twilight') . '</td><td>' . $formatted_sunrise_times['nautical'] . '</td>';
@@ -626,7 +640,7 @@
   $output .= '<tr><td>' . t('Actual') . '</td><td>' . $formatted_sunrise_times['daylight'] . '</td>';
   $output .= '<td>' . $formatted_sunset_times['daylight'] . '</td></tr>';
   $output .= '<tr><td>' . t('Moon') . '</td><td>' . $moonrise_time . '</td>';
-  $output .= '<td>' . $moonset_time . '</td></tr>';  
+  $output .= '<td>' . $moonset_time . '</td></tr>';
   $output .= '</table>';
   $output .= '</div>';
   return $output;
