summaryrefslogtreecommitdiff
path: root/manager/library/date
diff options
context:
space:
mode:
authorMarkus Warg <markus@mawaunix.mawa.sls>2010-03-31 16:43:49 +0200
committerMarkus Warg <markus@mawaunix.mawa.sls>2010-03-31 16:43:49 +0200
commit4f4c5ce3ccf0370f926d75e700e8b0bd2208f3f6 (patch)
tree82cb017ac68772ea36c0f522ec019bf14fbdc77a /manager/library/date
parent8398c9048d34a1f51212ae770998fc082fc93b69 (diff)
downloadcacert-mgr-4f4c5ce3ccf0370f926d75e700e8b0bd2208f3f6.tar.gz
cacert-mgr-4f4c5ce3ccf0370f926d75e700e8b0bd2208f3f6.tar.xz
cacert-mgr-4f4c5ce3ccf0370f926d75e700e8b0bd2208f3f6.zip
initial setup of framework code
enabled features * login * crt login * top / left menu * logging * db layer
Diffstat (limited to 'manager/library/date')
-rw-r--r--manager/library/date/HumanReadableTime.php140
-rw-r--r--manager/library/date/exception.HumanReadableTimeException.php37
-rw-r--r--manager/library/date/testHumanReadableTime.php16
3 files changed, 193 insertions, 0 deletions
diff --git a/manager/library/date/HumanReadableTime.php b/manager/library/date/HumanReadableTime.php
new file mode 100644
index 0000000..ee75703
--- /dev/null
+++ b/manager/library/date/HumanReadableTime.php
@@ -0,0 +1,140 @@
+<?php
+
+/**
+ * class that provides methods to convert human readable time / interval length
+ * expressions into other formats
+ *
+ * @author markus
+ * $Id: HumanReadableTime.php 92 2010-03-10 11:43:15Z markus $
+ */
+
+require_once(LIBRARY_PATH . '/date/exception.HumanReadableTimeException.php');
+
+class HumanReadableTime {
+ /**
+ * normalize an HRT string, convert from HRT to seconds and then convert back to
+ * HRT
+ * @param string $hrt
+ * @param string $maxunit
+ * @return string
+ */
+ public static function NormalizeHRT($hrt, $maxunit = 'w') {
+ return self::Seconds2HR(self::HR2Seconds($hrt), $maxunit);
+ }
+
+ /**
+ * convert string / interger which contains an interval length to
+ * human readable format (1w2d7h)
+ *
+ * if $maxunit is set, it defines the biggest unit in output (i.e. $maxunit = 'h' will
+ * allow only hms)
+ *
+ * @param string|integer $seconds
+ * @param string $maxunit
+ * @return string
+ */
+ public static function Seconds2HR($seconds, $maxunit = 'w') {
+ $maxunit = trim(strtolower($maxunit));
+ $allowed = array('w' => 0, 'd' => 0, 'h' => 0, 'm' => 0, 's' => 0);
+ if (!in_array($maxunit, array_keys($allowed), true))
+ throw new HumanReadableTimeException('illegal value for maxunit: "' . $maxunit . '"');
+ foreach ($allowed as $key => $value) {
+ if ($maxunit == $key)
+ break;
+ unset($allowed[$key]);
+ }
+
+ $seconds = intval($seconds);
+ $hrt = '';
+ foreach ($allowed as $key => $value) {
+ switch ($key) {
+ case 'w':
+ $tmp = intval($seconds / (7*86400));
+ if ($tmp > 0)
+ $seconds %= (7*86400);
+ $allowed[$key] += $tmp;
+ break;
+ case 'd':
+ $tmp = intval($seconds / (86400));
+ if ($tmp > 0)
+ $seconds %= (86400);
+ $allowed[$key] += $tmp;
+ break;
+ case 'h':
+ $tmp = intval($seconds / (3600));
+ if ($tmp > 0)
+ $seconds %= (3600);
+ $allowed[$key] += $tmp;
+ break;
+ case 'm':
+ $tmp = intval($seconds / (60));
+ if ($tmp > 0)
+ $seconds %= (60);
+ $allowed[$key] += $tmp;
+ break;
+ case 's':
+ $allowed[$key] += $seconds;
+ break;
+ }
+ }
+
+ $hrt = '';
+ foreach ($allowed as $key => $value) {
+ if ($value > 0)
+ $hrt .= sprintf('%d%s', $value, $key);
+ }
+ return $hrt;
+ }
+
+ /**
+ * parse a string of 3h2m7s and return the number of seconds as integer
+ * add "s" to the end of the number if $addsecond is set to true
+ * @param string $hr
+ * @param boolean $addsecond
+ * @return integer|string
+ */
+ public static function HR2Seconds($hr, $addsecond = false) {
+ $hr = trim($hr);
+ if ($hr == '') {
+ if ($addsecond === true)
+ return '0s';
+ else
+ return 0;
+ }
+
+ $hr = strtolower($hr);
+
+ $matches = array();
+ if (preg_match_all('/([0-9]*)([wdhms])/', $hr, $matches, PREG_SET_ORDER) > 0) {
+ $interval = 0;
+ for ($i = 0; $i < count($matches); $i++) {
+ switch ($matches[$i][2]) {
+ case 'w':
+ $interval += $matches[$i][1] * 7 * 86400;
+ break;
+ case 'd':
+ $interval += $matches[$i][1] * 86400;
+ break;
+ case 'h':
+ $interval += $matches[$i][1] * 3600;
+ break;
+ case 'm':
+ $interval += $matches[$i][1] * 60;
+ break;
+ case 's':
+ $interval += $matches[$i][1];
+ break;
+ }
+ }
+ if ($addsecond === true)
+ return sprintf('%ds', $interval);
+ else
+ return $interval;
+ }
+
+ if ($addsecond === true)
+ return '0s';
+ else
+ return 0;
+ }
+}
diff --git a/manager/library/date/exception.HumanReadableTimeException.php b/manager/library/date/exception.HumanReadableTimeException.php
new file mode 100644
index 0000000..2698d58
--- /dev/null
+++ b/manager/library/date/exception.HumanReadableTimeException.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @package SLS
+ * @subpackage CONFIG.EXCEPTION
+ */
+
+/**
+ * required files
+ * @ignore
+ */
+require_once(LIBRARY_PATH . '/exception/exception.Base.php');
+
+/**
+ * Exceptions thrown in the DNSSEC library classes
+ *
+ * @package SLS
+ * @subpackage CONFIG.EXCEPTION
+ * @author Markus Warg <mw@it-sls.de>
+ * @since 2009-02-25 13:05
+ * @version $Id: exception.HumanReadableTimeException.php 91 2010-03-10 10:36:25Z markus $
+ */
+class HumanReadableTimeException extends BaseException {
+ /**
+ * make new object
+ *
+ * @access public
+ * @param string $message
+ * @param int $code
+ * @param string $extra
+ */
+ /*
+ public function __construct($message,$code = 0,$extra = '') {
+ parent::__construct($message,$code, $extra);
+ }
+ */
+}
+?> \ No newline at end of file
diff --git a/manager/library/date/testHumanReadableTime.php b/manager/library/date/testHumanReadableTime.php
new file mode 100644
index 0000000..177d650
--- /dev/null
+++ b/manager/library/date/testHumanReadableTime.php
@@ -0,0 +1,16 @@
+<?php
+defined('LIBARARY_PATH')
+ || define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/..'));
+
+require_once('HumanReadableTime.php');
+
+$hrf = HumanReadableTime::HR2Seconds($argv[1], true);
+print 'Seconds: ' . $hrf . "\n";
+
+print 'Default: ' . HumanReadableTime::Seconds2HR($hrf) . "\n";
+print 'Week: ' . HumanReadableTime::Seconds2HR($hrf, 'w') . "\n";
+print 'Day: ' . HumanReadableTime::Seconds2HR($hrf, 'd') . "\n";
+print 'Hour: ' . HumanReadableTime::Seconds2HR($hrf, 'h') . "\n";
+print 'Minute: ' . HumanReadableTime::Seconds2HR($hrf, 'm') . "\n";
+print 'Second: ' . HumanReadableTime::Seconds2HR($hrf, 's') . "\n";
+