Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 299 lines (289 sloc) 8.018 kb
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
<?php
/**
* CDateTimeParser class file
*
* @author Wei Zhuo <weizhuo[at]gamil[dot]com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Tomasz Suchanek <tomasz[dot]suchanek[at]gmail[dot]com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CDateTimeParser converts a date/time string to a UNIX timestamp according to the specified pattern.
*
* The following pattern characters are recognized:
* <pre>
* Pattern | Description
* ----------------------------------------------------
* d | Day of month 1 to 31, no padding
* dd | Day of month 01 to 31, zero leading
* M | Month digit 1 to 12, no padding
* MM | Month digit 01 to 12, zero leading
* MMM | Short textual representation of month, three letters (since version 1.1.11)
* yy | 2 year digit, e.g., 96, 05
* yyyy | 4 year digit, e.g., 2005
* h | Hour in 0 to 23, no padding
* hh | Hour in 00 to 23, zero leading
* H | Hour in 0 to 23, no padding
* HH | Hour in 00 to 23, zero leading
* m | Minutes in 0 to 59, no padding
* mm | Minutes in 00 to 59, zero leading
* s | Seconds in 0 to 59, no padding
* ss | Seconds in 00 to 59, zero leading
* a | AM or PM, case-insensitive (since version 1.1.5)
* ? | matches any character (wildcard) (since version 1.1.11)
* ----------------------------------------------------
* </pre>
* All other characters must appear in the date string at the corresponding positions.
*
* For example, to parse a date string '21/10/2008', use the following:
* <pre>
* $timestamp=CDateTimeParser::parse('21/10/2008','dd/MM/yyyy');
* </pre>
*
* To format a timestamp to a date string, please use {@link CDateFormatter}.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.utils
* @since 1.0
*/
class CDateTimeParser
{
/**
* Converts a date string to a timestamp.
* @param string $value the date string to be parsed
* @param string $pattern the pattern that the date string is following
* @param array $defaults the default values for year, month, day, hour, minute and second.
* The default values will be used in case when the pattern doesn't specify the
* corresponding fields. For example, if the pattern is 'MM/dd/yyyy' and this
* parameter is array('minute'=>0, 'second'=>0), then the actual minute and second
* for the parsing result will take value 0, while the actual hour value will be
* the current hour obtained by date('H'). This parameter has been available since version 1.1.5.
* @return integer timestamp for the date string. False if parsing fails.
*/
public static function parse($value,$pattern='MM/dd/yyyy',$defaults=array())
{
$tokens=self::tokenize($pattern);
$i=0;
$n=strlen($value);
foreach($tokens as $token)
{
switch($token)
{
case 'yyyy':
{
if(($year=self::parseInteger($value,$i,4,4))===false)
return false;
$i+=4;
break;
}
case 'yy':
{
if(($year=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($year);
break;
}
case 'MMM':
{
if(($month=self::parseShortMonth($value,$i))===false)
return false;
$i+=3;
break;
}
case 'MM':
{
if(($month=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'M':
{
if(($month=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($month);
break;
}
case 'dd':
{
if(($day=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'd':
{
if(($day=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($day);
break;
}
case 'h':
case 'H':
{
if(($hour=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($hour);
break;
}
case 'hh':
case 'HH':
{
if(($hour=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'm':
{
if(($minute=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($minute);
break;
}
case 'mm':
{
if(($minute=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 's':
{
if(($second=self::parseInteger($value,$i,1,2))===false)
return false;
$i+=strlen($second);
break;
}
case 'ss':
{
if(($second=self::parseInteger($value,$i,2,2))===false)
return false;
$i+=2;
break;
}
case 'a':
{
if(($ampm=self::parseAmPm($value,$i))===false)
return false;
if(isset($hour))
{
if($hour==12 && $ampm==='am')
$hour=0;
else if($hour<12 && $ampm==='pm')
$hour+=12;
}
$i+=2;
break;
}
default:
{
$tn=strlen($token);
if($i>=$n || ($token{0}!='?' && substr($value,$i,$tn)!==$token))
return false;
$i+=$tn;
break;
}
}
}
if($i<$n)
return false;

if(!isset($year))
$year=isset($defaults['year']) ? $defaults['year'] : date('Y');
if(!isset($month))
$month=isset($defaults['month']) ? $defaults['month'] : date('n');
if(!isset($day))
$day=isset($defaults['day']) ? $defaults['day'] : date('j');

if(strlen($year)===2)
{
if($year>=70)
$year+=1900;
else
$year+=2000;
}
$year=(int)$year;
$month=(int)$month;
$day=(int)$day;

if(
!isset($hour) && !isset($minute) && !isset($second)
&& !isset($defaults['hour']) && !isset($defaults['minute']) && !isset($defaults['second'])
)
$hour=$minute=$second=0;
else
{
if(!isset($hour))
$hour=isset($defaults['hour']) ? $defaults['hour'] : date('H');
if(!isset($minute))
$minute=isset($defaults['minute']) ? $defaults['minute'] : date('i');
if(!isset($second))
$second=isset($defaults['second']) ? $defaults['second'] : date('s');
$hour=(int)$hour;
$minute=(int)$minute;
$second=(int)$second;
}

if(CTimestamp::isValidDate($year,$month,$day) && CTimestamp::isValidTime($hour,$minute,$second))
return CTimestamp::getTimestamp($hour,$minute,$second,$month,$day,$year);
else
return false;
}

/*
* @param string $pattern the pattern that the date string is following
*/
private static function tokenize($pattern)
{
if(!($n=strlen($pattern)))
return array();
$tokens=array();
for($c0=$pattern[0],$start=0,$i=1;$i<$n;++$i)
{
if(($c=$pattern[$i])!==$c0)
{
$tokens[]=substr($pattern,$start,$i-$start);
$c0=$c;
$start=$i;
}
}
$tokens[]=substr($pattern,$start,$n-$start);
return $tokens;
}

/**
* @param string $value the date string to be parsed
* @param integer $offset starting offset
* @param integer $minLength minimum length
* @param integer $maxLength maximum length
*/
protected static function parseInteger($value,$offset,$minLength,$maxLength)
{
for($len=$maxLength;$len>=$minLength;--$len)
{
$v=substr($value,$offset,$len);
if(ctype_digit($v) && strlen($v)>=$minLength)
return $v;
}
return false;
}

/**
* @param string $value the date string to be parsed
* @param integer $offset starting offset
*/
protected static function parseAmPm($value, $offset)
{
$v=strtolower(substr($value,$offset,2));
return $v==='am' || $v==='pm' ? $v : false;
}

/**
* @param string $value the date string to be parsed
* @param integer $offset starting offset
* @since 1.1.11
*/
protected static function parseShortMonth($value, $offset)
{
static $titles=array('jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
$v=array_search(strtolower(substr($value,$offset,3)), $titles);
return $v===false ? false : $v+1;
}
}
Something went wrong with that request. Please try again.