Ejemplo n.º 1
0
 /**
  * Decodes a textual representation for a duration
  *
  * @param $s input duration (numeric=seconds, hh:mm:ss=decode to seconds)
  */
 function set($s)
 {
     if (!$s) {
         return;
     }
     if (is_numeric($s)) {
         $this->value = $s;
         return;
     }
     if (is_duration($s)) {
         $this->value = parse_duration($s);
         return;
     }
     $a = explode(':', $s);
     if (count($a) == 3) {
         //handle "00:03:39.00"
         $this->value = $a[0] * 3600 + $a[1] * 60 + $a[2];
         return;
     }
     if (count($a) == 2) {
         //handle "04:29"
         $this->value = $a[0] * 60 + $a[1];
         return;
     }
     dtrace();
     die('Duration->set( ' . $s . ' ) FAIL');
     //$this->duration = $s;
 }
Ejemplo n.º 2
0
 public function set($key, $val = '', $expire_time = 3600)
 {
     if (strlen($key) > $this->maxlen) {
         throw new \Exception('Key length too long (len ' . strlen($key) . ', max ' . $this->maxlen . '): ' . $key);
     }
     if ($expire_time) {
         if (!is_duration($expire_time)) {
             throw new \Exception('bad expire time');
         }
         $expire_time = parse_duration($expire_time);
     }
     $this->connect();
     //       $key = str_replace(' ', '_', $key);
     $ret = $this->redis->setex($key, $expire_time, $val);
     if ($this->debug) {
         echo 'TempStoreRedis SET "' . $key . '" = "' . substr($val, 0, 200) . '"... (' . $expire_time . ' sec)' . ln();
     }
     /*
             if (!$ret)
                 throw new \Exception ('SET failed');
     */
     return $ret;
 }
Ejemplo n.º 3
0
 function setTimeout($n)
 {
     if (!is_duration($n)) {
         throw new \Exception('bad timeout: ' . $n);
     }
     $this->timeout = parse_duration($n);
 }
Ejemplo n.º 4
0
/**
 * Converts a timespan into human-readable text
 *
 * @param $secs number of seconds to present
 * @return returns a sting like: 4h10m3s
 */
function shortTimePeriod($secs)
{
    if (is_float($secs)) {
        $secs = ceil($secs);
    }
    if (is_duration($secs)) {
        $secs = parse_duration($secs);
    }
    $retval = '';
    //years
    $a = date('Y', $secs) - 1970;
    if ($a == 1) {
        $retval = $a . ' year, ';
    } else {
        if ($a > 0) {
            $retval = $a . ' years, ';
        }
    }
    $secs -= $a * 60 * 60 * 24 * 30 * 365;
    //months
    $a = date('n', $secs) - 1;
    if ($a == 1) {
        $retval .= $a . ' month, ';
    } else {
        if ($a > 0) {
            $retval .= $a . ' months, ';
        }
    }
    $secs -= $a * 60 * 60 * 24 * 30;
    //days
    $a = date('j', $secs) - 1;
    if ($a == 1) {
        $retval .= $a . ' day, ';
    } else {
        if ($a > 0) {
            $retval .= $a . ' days, ';
        }
    }
    $secs -= $a * 60 * 60 * 24;
    //hours
    $a = date('H', $secs) - 1;
    if ($a > 0) {
        $retval .= $a . 'h';
    }
    $secs -= $a * 60 * 60;
    //minutes
    $a = date('i', $secs) - 0;
    if ($a > 0) {
        $retval .= $a . 'm';
    }
    $secs -= $a * 60;
    //seconds
    $a = date('s', $secs) - 0;
    if ($a > 0) {
        $retval .= $a . 's';
    }
    if (substr($retval, -2) == ', ') {
        $retval = substr($retval, 0, -2);
    }
    if ($retval == '') {
        $retval = '0s';
    }
    return $retval;
}
Ejemplo n.º 5
0
if (seconds_to_hms(1596.595, true, 3, ',') != '0:26:36,595') {
    echo "FAIL 24\n";
}
if (seconds_to_hms(1596.595, true, 3, ',', true) != '00:26:36,595') {
    echo "FAIL 25\n";
}
if (!is_duration('2d')) {
    echo "FAIL 30\n";
}
if (is_duration('abc')) {
    echo "FAIL 31\n";
}
if (is_duration('1a2d')) {
    echo "FAIL 32\n";
}
if (!is_duration(500)) {
    echo "FAIL 33\n";
}
if (sql_date(ts('2011-05-08')) != '2011-05-08') {
    echo "FAIL 40\n";
}
if (sql_date(ts('20110508')) != '2011-05-08') {
    echo "FAIL 41\n";
}
if (sql_date(ts('5/8/2011')) != '2011-05-08') {
    echo "FAIL 42\n";
}
if (sql_date(ts('05/08/2011')) != '2011-05-08') {
    echo "FAIL 43\n";
}
if (!is_hms('12:44:11.21')) {