summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Util/TSimpleDateFormatter.php
blob: 021948e4908a2247eb32966c38599115062d041f (plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
 * TSimpleDateFormatter class file
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Util
 */

/**
 * TSimpleDateFormatter class.
 *
 * Formats and parses dates using the SimpleDateFormat pattern.
 * This pattern is compatible with the I18N and java's SimpleDateFormatter.
 * <code>
 * Pattern |      Description
 * ----------------------------------------------------
 * d       | Day of month 1 to 31, no padding
 * dd      | Day of monath 01 to 31, zero leading
 * M       | Month digit 1 to 12, no padding
 * MM      | Month digit 01 to 12, zero leading
 * yy      | 2 year digit, e.g., 96, 05
 * yyyy    | 4 year digit, e.g., 2005
 * ----------------------------------------------------
 * </code>
 *
 * Usage example, to format a date
 * <code>
 * $formatter = new TSimpleDateFormatter("dd/MM/yyy");
 * echo $formatter->format(time());
 * </code>
 *
 * To parse the date string into a date timestamp.
 * <code>
 * $formatter = new TSimpleDateFormatter("d-M-yyy");
 * echo $formatter->parse("24-6-2005");
 * </code>
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @package System.Util
 * @since 3.0
 */
class TSimpleDateFormatter
{
	/**
	 * Formatting pattern.
	 * @var string
	 */
	private $pattern;

	/**
	 * Charset, default is 'UTF-8'
	 * @var string
	 */
	private $charset = 'UTF-8';

	/**
	 * Constructor, create a new date time formatter.
	 * @param string formatting pattern.
	 * @param string pattern and value charset
	 */
	public function __construct($pattern, $charset='UTF-8')
	{
		$this->setPattern($pattern);
		$this->setCharset($charset);
	}

	/**
	 * @return string formatting pattern.
	 */
	public function getPattern()
	{
		return $this->pattern;
	}

	/**
	 * @param string formatting pattern.
	 */
	public function setPattern($pattern)
	{
		$this->pattern = $pattern;
	}

	/**
	 * @return string formatting charset.
	 */
	public function getCharset()
	{
		return $this->charset;
	}

	/**
	 * @param string formatting charset.
	 */
	public function setCharset($charset)
	{
		$this->charset = $charset;
	}

	/**
	 * Format the date according to the pattern.
	 * @param string|int the date to format, either integer or a string readable by strtotime.
	 * @return string formatted date.
	 */
	public function format($value)
	{
		$date = $this->getDate($value);
		$bits['yyyy'] = $date['year'];
		$bits['yy'] = substr("{$date['year']}", -2);

		$bits['MM'] = str_pad("{$date['mon']}", 2, '0', STR_PAD_LEFT);
		$bits['M'] = $date['mon'];

		$bits['dd'] = str_pad("{$date['mday']}", 2, '0', STR_PAD_LEFT);
		$bits['d'] = $date['mday'];

		$pattern = preg_replace('/M{3,4}/', 'MM', $this->pattern);
		return str_replace(array_keys($bits), $bits, $pattern);
	}

	public function getMonthPattern()
	{
		if(is_int(strpos($this->pattern, 'MMMM')))
			return 'MMMM';
		if(is_int(strpos($this->pattern, 'MMM')))
			return 'MMM';
		if(is_int(strpos($this->pattern, 'MM')))
			return 'MM';
		if(is_int(strpos($this->pattern, 'M')))
			return 'M';
		return false;
	}

	public function getDayPattern()
	{
		if(is_int(strpos($this->pattern, 'dd')))
			return 'dd';
		if(is_int(strpos($this->pattern, 'd')))
			return 'd';
		return false;
	}

	public function getYearPattern()
	{
		if(is_int(strpos($this->pattern, 'yyyy')))
			return 'yyyy';
		if(is_int(strpos($this->pattern, 'yy')))
			return 'yy';
		return false;
	}

	public function getDayMonthYearOrdering()
	{
		$ordering = array();
		if(is_int($day= strpos($this->pattern, 'd')))
			$ordering['day'] = $day;
		if(is_int($month= strpos($this->pattern, 'M')))
			$ordering['month'] = $month;
		if(is_int($year= strpos($this->pattern, 'yy')))
			$ordering['year'] = $year;
		asort($ordering);
		return array_keys($ordering);
	}

	/**
	 * Gets the time stamp from string or integer.
	 * @param string|int date to parse
	 * @return array date info array
	 */
	private function getDate($value)
	{
		$s = Prado::createComponent('System.Util.TDateTimeStamp');
		if(is_numeric($value))
			return $s->getDate($value);
		else
			return $s->parseDate($value);
	}

	/**
	 * @return boolean true if the given value matches with the date pattern.
	 */
	public function isValidDate($value)
	{
		if($value === null) {
			return false;
		} else {
			return $this->parse($value, false) !== null;
		}
	}

	/**
	 * Parse the string according to the pattern.
	 * @param string|int date string or integer to parse
	 * @return int date time stamp
	 * @throws TInvalidDataValueException if date string is malformed.
	 */
	public function parse($value,$defaultToCurrentTime=true)
	{
		if(is_int($value) || is_float($value))
			return $value;
		else if(!is_string($value))
			throw new TInvalidDataValueException('date_to_parse_must_be_string', $value);

		if(empty($this->pattern)) return time();

		$date = time();

		if($this->length(trim($value)) < 1)
			return $defaultToCurrentTime ? $date : null;

		$pattern = $this->pattern;

		$i_val = 0;
		$i_format = 0;
		$pattern_length = $this->length($pattern);
		$c = '';
		$token='';
		$x=null; $y=null;


		if($defaultToCurrentTime)
		{
			$year = "{$date['year']}";
			$month = $date['mon'];
			$day = $date['mday'];
		}
		else
		{
			$year = null;
			$month = null;
			$day = null;
		}

		while ($i_format < $pattern_length)
		{
			$c = $this->charAt($pattern,$i_format);
			$token='';
			while ($this->charEqual($pattern, $i_format, $c)
						&& ($i_format < $pattern_length))
			{
				$token .= $this->charAt($pattern, $i_format++);
			}

			if ($token=='yyyy' || $token=='yy' || $token=='y')
			{
				if ($token=='yyyy') { $x=4;$y=4; }
				if ($token=='yy')   { $x=2;$y=2; }
				if ($token=='y')    { $x=2;$y=4; }
				$year = $this->getInteger($value,$i_val,$x,$y);
				if($year === null)
					return null;
					//throw new TInvalidDataValueException('Invalid year', $value);
				$i_val += strlen($year);
				if(strlen($year) == 2)
				{
					$iYear = (int)$year;
					if($iYear > 70)
						$year = $iYear + 1900;
					else
						$year = $iYear + 2000;
				}
				$year = (int)$year;
			}
			elseif($token=='MM' || $token=='M')
			{
				$month=$this->getInteger($value,$i_val,
									$this->length($token),2);
				$iMonth = (int)$month;
				if($month === null || $iMonth < 1 || $iMonth > 12 )
					return null;
					//throw new TInvalidDataValueException('Invalid month', $value);
				$i_val += strlen($month);
				$month = $iMonth;
			}
			elseif ($token=='dd' || $token=='d')
			{
				$day = $this->getInteger($value,$i_val,
									$this->length($token), 2);
				$iDay = (int)$day;
				if($day === null || $iDay < 1 || $iDay >31)
					return null;
					//throw new TInvalidDataValueException('Invalid day', $value);
				$i_val += strlen($day);
				$day = $iDay;
			}
			else
			{
				if($this->substring($value, $i_val, $this->length($token)) != $token)
					return null;
					//throw new TInvalidDataValueException("Subpattern '{$this->pattern}' mismatch", $value);
				else
					$i_val += $this->length($token);
			}
		}
		if ($i_val != $this->length($value))
			return null;
			//throw new TInvalidDataValueException("Pattern '{$this->pattern}' mismatch", $value);
		if(!$defaultToCurrentTime && ($month === null || $day === null || $year === null))
			return null;
		else
		{
			if(empty($year)) {
				$year = date('Y');
			}
			$day = (int)$day <= 0 ? 1 : (int)$day;
			$month = (int)$month <= 0 ? 1 : (int)$month;
			$s = Prado::createComponent('System.Util.TDateTimeStamp');
			return $s->getTimeStamp(0, 0, 0, $month, $day, $year);
		}
	}

	/**
	 * Calculate the length of a string, may be consider iconv_strlen?
	 */
	private function length($string)
	{
		//use iconv_strlen or just strlen?
		return strlen($string);
	}

	/**
	 * Get the char at a position.
	 */
	private function charAt($string, $pos)
	{
		return $this->substring($string, $pos, 1);
	}

	/**
	 * Gets a portion of a string, uses iconv_substr.
	 */
	private function substring($string, $start, $length)
	{
		return iconv_substr($string, $start, $length);
	}

	/**
	 * Returns true if char at position equals a particular char.
	 */
	private function charEqual($string, $pos, $char)
	{
		return $this->charAt($string, $pos) == $char;
	}

	/**
	 * Gets integer from part of a string, allows integers of any length.
	 * @param string string to retrieve the integer from.
	 * @param int starting position
	 * @param int minimum integer length
	 * @param int maximum integer length
	 * @return string integer portion of the string, null otherwise
	 */
	private function getInteger($str,$i,$minlength,$maxlength)
	{
		//match for digits backwards
		for ($x = $maxlength; $x >= $minlength; $x--)
		{
			$token= $this->substring($str, $i,$x);
			if ($this->length($token) < $minlength)
				return null;
			if (preg_match('/^\d+$/', $token))
				return $token;
		}
		return null;
	}
}