This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtil.php
83 lines (76 loc) · 2.13 KB
/
Util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
// vim: fenc=utf-8:ft=php:ai:si:ts=4:sw=4:et:
/**
* Hashmark_Util
*
* @filesource
* @copyright Copyright (c) 2008-2011 David Smith
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Hashmark
* @version $Id$
*/
/**
* MySQL DATETIME data type constants.
*/
define('HASHMARK_DATETIME_FORMAT', 'Y-m-d H:i:s');
define('HASHMARK_DATETIME_PREG_PATTERN', '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/');
define('HASHMARK_DATETIME_EMPTY', '0000-00-00 00:00:00');
define('HASHMARK_DATETIME_MIN', '1000-01-01 00:00:00');
define('HASHMARK_DATETIME_MAX', '9999-12-31 23:59:59');
/**
* Mix of static utility methods.
*
* @package Hashmark
* @subpackage Base
*/
class Hashmark_Util
{
/**
* Normalize date+time strings and UNIX timestamps to DATETIME format.
*
* @param mixed $str
* @return mixed Normalized string; otherwise false.
*/
public static function toDatetime($str)
{
switch (strlen(strval($str))) {
case 10: // UNIX timestamp.
return gmdate(HASHMARK_DATETIME_FORMAT, $str);
case 14: // YYYYMMDDHHMMSS
$new = preg_replace('/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})$/', '\\1-\\2-\\3 \\4:\\5:\\6', $str);
return ($new == $str ? false : $new);
default: // YYYY-MM-DD HH:MM:SS
if (preg_match(HASHMARK_DATETIME_PREG_PATTERN, $str)) {
return $str;
}
return false;
}
}
/**
* Reverse comparator based on string length.
*
* @param string $a
* @param string $b
* @return int
*/
public static function sortByStrlenReverse($a, $b)
{
$aLen = strlen($a);
$bLen = strlen($b);
if ($aLen < $bLen) {
return 1;
} else if ($aLen > $bLen) {
return -1;
}
return 0;
}
/**
* Return a pseudorandom SHA1 hash.
*
* @return string
*/
public static function randomSha1()
{
return sha1(uniqid(mt_rand(), true));
}
}