Update of /cvsroot/phpweather/phpweather
In directory usw-pr-cvs1:/tmp/cvs-serv3280

Modified Files:
        data_retrieval.php db_mysql.php db_none.php db_pgsql.php 
        index.php locale_common.php locale_en.php 
Log Message:
The timestamp is now given to the database object as a regular UNIX
timestamp. Also, this fixes some issues with the runway-parsing.


Index: data_retrieval.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- data_retrieval.php  2001/03/24 16:40:41     1.2
+++ data_retrieval.php  2001/04/02 19:34:20     1.3
@@ -108,17 +108,24 @@
       return false;
     }
     
-    if (list($metar, $timestamp) = $this->db->get_metar($this->get_station())) { /* 
found station */
-      
+    if ($data = $this->db->get_metar($this->get_station())) { /* found station */
+      $this->notice('Found the METAR in the database');
+      list($metar, $timestamp) = $data;
+      /* We set the METAR right away, and then count on
+       * get_metar_from_web() to set it to something else, if
+       * necessary.
+       */
+      $this->metar = $metar;
       if ($this->properties['always_use_db'] || $timestamp > time() - 3600) {
-       /* We have asked explicit for a cached metar, or the METAR is still fresh.
-        * Either way - we return the METAR we found in the database.*/
-       $this->notice('Using previously cached METAR for <code>' . 
$this->get_station() . '</code>');
-       $this->metar = $metar;
+       /* We have asked explicit for a cached metar, or the METAR is
+        * still fresh. Either way - we return the METAR we found in
+        * the database.
+        */
+       $this->notice('Using previously cached METAR for <code>' . 
+$this->get_station() . '</code>. The METAR expires in ' . ($timestamp + 3600 - 
+time()) . ' seconds.');
        return $metar;
       } else {
        /* The METAR is too old, so we fetch a new */
-       $this->notice('The METAR was too old for <code>' . $this->get_station() . 
'</code>');
+       $this->notice('The METAR for <code>' . $this->get_station() . '</code> was ' . 
+(time() - 3600 - $timestamp) . ' seconds too old.');
        return $this->get_metar_from_web(false);
       }
     } else {
@@ -131,7 +138,7 @@
   /**
    * Fetches a METAR for the Internet.
    *
-   * The METAR if fetched via HTTP from the National Weather Services public server 
at http://weather.noaa.gov.
+   * The METAR is fetched via HTTP from the National Weather Services public server 
+at http://weather.noaa.gov.
    *
    * @param    boolean Should the station be inserted into the database, or should we 
update an all ready existing entry?
    * @return   string  The raw METAR.
@@ -149,7 +156,8 @@
       list(, $date) = each($file);
       $date = trim($date);
       /* The next lines are the METAR it self. We make sure that we
-       * don't put a space infront of the first line */
+       * don't put a space infront of the first line
+       */
       list(, $line) = each($file);
       $metar = trim($line);
       /* Then the rest of the lines... */
@@ -159,47 +167,48 @@
       
       /* We don't want any double-spaces in our METAR */
       $metar = str_replace('  ', ' ', $metar);
-      
-      /* The date is in the form 2000/10/09 14:50 UTC. This seperates */
-      /* the different parts. */
-      $date_parts = explode(':', strtr($date, '/ ', '::'));
-      $date_unixtime = gmmktime($date_parts[3], $date_parts[4], 0, $date_parts[1], 
$date_parts[2], $date_parts[0]);
       
-      /* It might seam strange, that we make a local date, but MySQL
-       * expects the time to be local, and not UTC. The same applies
-       * for the other $date = date('Y/m/d H:i') statements. */
-      $date = date('Y/m/d H:i', $date_unixtime);
+      $date = explode(':', strtr($date, '/ ', '::'));
+      $timestamp = gmmktime($date[3], $date[4], 0, $date[1], $date[2], $date[0]);
       
       if (!ereg('[0-9]{6}Z', $metar)) {
        /* Some reports dont even have a time-part, so we insert the
         * current time. This might not be the time of the report, but
-        * it was broken anyway :-) */
-       $metar = gmdate('dHi', $date_unixtime) . 'Z ' . $metar;
+        * it was broken anyway :-)
+        */
+       $metar = gmdate('dHi', $timestamp) . 'Z ' . $metar;
       }
       
-      if ($date_unixtime < (time() - 3300)) {
-       /* The timestamp in the metar is more than 55 minutes old. We
+      if ($timestamp < (time() - 3300)) {
+       /* The timestamp in the METAR is more than 55 minutes old. We
         * adjust the timestamp, so that we won't try to fetch a new
         * METAR within the next 5 minutes. After 5 minutes, the
-        * timestamp will again be more than 1 hour old. */
-       $date = date('Y/m/d H:i', time() - 3300);
+        * timestamp will again be more than 1 hour old.
+        */
+       $timestamp = time() - 3300;
       }
     } else {
-      /* If we end up here, it means that there was no file, we then
-       * set the metar to and empty string and. We set the date to
-       * time() - 3000 to give the server 10 minutes of peace. If the
-       * file is unavailable, we don't want to stress the server. */
-      $metar = '';
-      $date = date('Y/m/d H:i', time() - 3000);
+      /* If we end up here, it means that there was no file. If the
+       * station was a new station, we set the metar to an empty
+       * string, else we just use the old METAR. We adjust the time
+       * stored in the database in both cases, so that the server
+       * isn't stressed to much.
+       */
+      if ($new_station) {
+       $metar = '';
+      } else {
+       $metar = $this->metar;
+      }
+      $timestamp = time() - 3000;
     }
     
     /* We then cache the METAR in our database */
     if ($new_station) {
       $this->notice('Inserting new METAR for <code>' . $this->get_station() . 
'</code>');
-      $this->db->insert_metar($station, $metar, $date);
+      $this->db->insert_metar($station, $metar, $timestamp);
     } else {
       $this->notice('Updating METAR for <code>' . $this->get_station() . '</code>');
-      $this->db->update_metar($station, $metar, $date);
+      $this->db->update_metar($station, $metar, $timestamp);
     }
     /* We update and return the METAR */
     $this->metar = $metar;

Index: db_mysql.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db_mysql.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- db_mysql.php        2001/03/28 13:31:57     1.2
+++ db_mysql.php        2001/04/02 19:34:20     1.3
@@ -1,8 +1,8 @@
 <?php
 /**
- * This class is the 'mysql' database-type
+ * This class is the 'mysql' database-type.
  *
- * It implements all the methods necessary to insert, update and retrive METARs using 
a MySQL database.
+ * It implements all the methods necessary to insert, update and retrive METARs using 
+a MySQL database. You'll need access to a MySQL database to be able to use this 
+object.
  *
  * @author   Martin Geisler <[EMAIL PROTECTED]>
  * @version  $Id$
@@ -144,7 +144,7 @@
    * @see update_metar()
    */
   function insert_metar($station, $metar, $timestamp) {
-    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
timestamp = '$timestamp'");
+    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
+timestamp = FROM_UNIXTIME($timestamp)");
   }
 
 
@@ -158,7 +158,7 @@
    * @see insert_metar()
    */
   function update_metar($station, $metar, $timestamp) {
-    $this->query("UPDATE metars SET metar = '$metar', timestamp = '$timestamp' WHERE 
station = '$station'");
+    $this->query("UPDATE metars SET metar = '$metar', timestamp = 
+FROM_UNIXTIME($timestamp) WHERE station = '$station'");
   }
 
   /**

Index: db_none.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db_none.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -3 -r1.4 -r1.5
--- db_none.php 2001/04/02 15:55:40     1.4
+++ db_none.php 2001/04/02 19:34:20     1.5
@@ -29,6 +29,19 @@
   }
 
   /**
+   * Pretends to establish a connection to the database.
+   *
+   * Like all the other methods of this database object, this doesn't do anything 
+useful.
+   *
+   * @return  boolean  Always returns true, so that other functions that depends on a 
+connection to the database doesn't fail because of this.
+   * @access  public
+   */
+  function connect() {
+    return true;
+  }
+
+
+  /**
    * Pretends to insert a METAR into the database.
    *
    * @param   string   The ICAO of the station.

Index: db_pgsql.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/db_pgsql.php,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -3 -r1.1.1.1 -r1.2
--- db_pgsql.php        2001/03/22 18:04:10     1.1.1.1
+++ db_pgsql.php        2001/04/02 19:34:21     1.2
@@ -144,7 +144,7 @@
    * @see update_metar()
    */
   function insert_metar($station, $metar, $timestamp) {
-    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
timestamp = '$timestamp'");
+    $this->query("INSERT INTO metars SET station = '$station', metar = '$metar', 
+timestamp = FROM_UNIXTIME($timestamp)");
   }
 
 

Index: index.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/index.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- index.php   2001/03/22 18:09:19     1.2
+++ index.php   2001/04/02 19:34:21     1.3
@@ -16,6 +16,7 @@
 require('db_common.php');
 require('db_mysql.php');
 require('db_none.php');
+require('db_dba.php');
 require('db_layer.php');
 require('data_retrieval.php');
 require('metar_parser.php');
@@ -23,7 +24,7 @@
 require('locale_en.php');
 require('phpweather.php');
 
-//define('DEBUG', true);
+define('DEBUG', true);
 
 
 $properties = array('db_hostname' => 'localhost',
@@ -32,7 +33,9 @@
                    'db_password' => '',
                    /*If you set this to 'mysql', you'll need to fill
                       in the other 'db_*' properties. */
-                   'db_type'     => 'none', 
+                   'db_type'     => 'dba', 
+                   'db_handler'  => 'gdbm',
+                   'db_path'     => './phpweather.db',
                    'mark_begin'  => '<font color="red">',
                    'mark_end'    => '</font>',
                    'pref_units'  => 'both_imperial',
@@ -54,25 +57,24 @@
 $obj->get_metar();
 
 /* Checkpoint, we store the time */
-$time[] = array(microtime(), 'METAR-retrieval');
+$time[] = array(microtime(), 'METAR retrieval');
 
 $obj->decode_metar();
 
 
 /* Checkpoint, we store the time */
-$time[] = array(microtime(), 'decode');
+$time[] = array(microtime(), 'decoding');
 
 $obj->print_pretty();
 
-$obj->print_table();
-
 /* Checkpoint, we store the time */
-$time[] = array(microtime(), 'pretty-print.');
+$time[] = array(microtime(), 'pretty-print finished');
 
+/*
 echo "<pre>\n";
 print_r($obj->decode_metar());
 echo "</pre>\n";
-
+*/
 
 $max = count($time) - 1;
 for ($i = 0; $i < $max; $i++) {

Index: locale_common.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/locale_common.php,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -3 -r1.3 -r1.4
--- locale_common.php   2001/03/26 20:18:26     1.3
+++ locale_common.php   2001/04/02 19:34:21     1.4
@@ -378,29 +378,37 @@
       $approach = $this->strings['runway_right'];
     } else {
       $approach = '';
-      error("parse_runway_group(): \$approach not recognized: $approach"); 
+      $this->error("parse_runway_group(): \$approach not recognized: $approach"); 
     }
     
     if (!empty($min_meter)) {
       
-      $min_tendency_str = runway_tendency($min_tendency, 
$this->properties['mark_begin'], $this->properties['mark_end']);
-      $max_tendency_str = runway_tendency($max_tendency, 
$this->properties['mark_begin'], $this->properties['mark_end']);
-      
-      
-      $output = $this->strings['between'] .
-       $this->pref_units($this->properties['mark_begin'] . $min_meter .
-                         $this->properties['mark_end'] . $this->strings['meters'],
-                         $this->properties['mark_begin'] . $min_ft .
-                         $this->properties['mark_end'] . $this->strings['feet']) . 
-       $min_tendency_str . $this->strings['and'] .  
-       $this->pref_units($this->properties['mark_begin'] . $max_meter .
-                         $this->properties['mark_end'] . $this->strings['meters'],
-                         $this->properties['mark_begin'] . $max_ft .
-                         $this->properties['mark_end'] . $this->strings['feet']) .
-       $max_tendency_str . $this->strings['runway_for_runway'] . 
-       $this->properties['mark_begin'] . $nr . $approach . 
$this->properties['mark_end'];
-    } else {
+      if (!empty($min_tendency)) {
+       $min_tendency_str = $this->runway_tendency($min_tendency);
+      } else {
+       $min_tendency_str = '';
+      }
+    
+      if (!empty($max_tendency)) {
+       $max_tendency_str = $this->runway_tendency($max_tendency);
+      } else {
+       $max_tendency_str = '';
+      }
       
+    $output = $this->strings['runway_between'] .
+      $this->pref_units($this->properties['mark_begin'] . $min_meter .
+                       $this->properties['mark_end'] . $this->strings['meters'],
+                       $this->properties['mark_begin'] . $min_ft .
+                       $this->properties['mark_end'] . $this->strings['feet']) . 
+      $min_tendency_str . $this->strings['and'] .  
+      $this->pref_units($this->properties['mark_begin'] . $max_meter .
+                       $this->properties['mark_end'] . $this->strings['meters'],
+                       $this->properties['mark_begin'] . $max_ft .
+                       $this->properties['mark_end'] . $this->strings['feet']) .
+      $max_tendency_str . $this->strings['runway_for_runway'] . 
+      $this->properties['mark_begin'] . $nr . $approach . 
+$this->properties['mark_end'];
+  } else {
+    
       $tendency = $this->runway_tendency($tendency);
       
       $output = $this->pref_units($this->properties['mark_begin'] . $meter .
@@ -790,11 +798,19 @@
    **************************/
   if (!in_array('runway', $this->properties['exclude']) &&
       !empty($runway_group1)) {
+
+    $runway_str1 = $runway_str2 = $runway_str3 = $runway_str4 = '';
     
     $runway_str1 = $this->parse_runway_group($runway_group1);
-    $runway_str2 = $this->parse_runway_group($runway_group2);
-    $runway_str3 = $this->parse_runway_group($runway_group3);
-    $runway_str4 = $this->parse_runway_group($runway_group4);
+    if (!empty($runway_group2)) {
+      $runway_str2 = $this->parse_runway_group($runway_group2);
+      if (!empty($runway_group3)) {
+       $runway_str3 = $this->parse_runway_group($runway_group3);
+       if (!empty($runway_group4)) {
+         $runway_str4 = $this->parse_runway_group($runway_group4);
+       }
+      }
+    }
 
     
     $output['runway'] = $this->strings['runway_visibility'] .

Index: locale_en.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/locale_en.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- locale_en.php       2001/03/30 13:25:36     1.2
+++ locale_en.php       2001/04/02 19:34:21     1.3
@@ -92,7 +92,7 @@
     $this->strings['cloud_clear']     = 'The sky was %sclear%s.';
     $this->strings['cloud_height']    = ' clouds at a height of ';
     $this->strings['cloud_overcast']  = 'the sky was %sovercast%s from a height of ';
-    $this->strings['cloud_vertical_visibility'] = 'the %svertical visibility%s was';
+    $this->strings['cloud_vertical_visibility'] = 'the %svertical visibility%s was ';
     $this->strings['cloud_condition'] =
       array(
            'SKC' => 'clear',
@@ -137,12 +137,13 @@
            'SQ' => 'squalls ',
            'FC' => 'funnel cloud tornado waterspout ',
            'SS' => 'sandstorm/duststorm ');
-    $this->strings['visibility'] = 'The visibility was ';
+    $this->strings['visibility'] = 'The overall visibility was ';
     $this->strings['visibility_greater_than']  = 'greater than ';
     $this->strings['visibility_less_than']     = 'less than ';
     $this->strings['runway_upward_tendency']   = ' with an %supward%s tendency';
     $this->strings['runway_downward_tendency'] = ' with a %sdownward%s tendency';
     $this->strings['runway_no_tendency']       = ' with %sno distinct%s tendency';
+    $this->strings['runway_between']           = 'between ';
     $this->strings['runway_left']              = ' left';
     $this->strings['runway_central']           = ' central';
     $this->strings['runway_right']             = ' right';


_______________________________________________
PHPWeather-checkins mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/phpweather-checkins

Reply via email to