summaryrefslogtreecommitdiff
path: root/framework/I18N/core/DateTimeFormatInfo.php
blob: 04ed0e73b43b88157282f40a321e8b255d2f0e53 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php

/**
 * DateTimeFormatInfo class file.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the BSD License.
 *
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
 *
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
 * The latest version of PRADO can be obtained from:
 * {@link http://prado.sourceforge.net/}
 *
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version $Revision: 1.2 $  $Date: 2005/01/05 03:15:14 $
 * @package System.I18N.core
 */

/**
 * Get the CultureInfo class.
 */
require_once(dirname(__FILE__).'/CultureInfo.php');


/**
 * Defines how DateTime values are formatted and displayed, depending
 * on the culture.
 *
 * This class contains information, such as date patterns, time patterns,
 * and AM/PM designators.
 *
 * To create a DateTimeFormatInfo for a specific culture, create a
 * CultureInfo for that culture and retrieve the CultureInfo.DateTimeFormat
 * property. For example:
 * <code>
 * $culture = new CultureInfo('en_AU');
 * $dtfi = $culture->DateTimeFormat;
 * </code>
 *
 * To create a DateTimeFormatInfo for the invariant culture, use
 * <code>
 * DateTimeFormatInfo::getInstance($culture=null);
 * </code>
 * you may pass a CultureInfo parameter $culture to get the DateTimeFormatInfo
 * for a specific culture.
 *
 * DateTime values are formatted using standard or custom patterns stored in
 * the properties of a DateTimeFormatInfo.
 *
 * The standard patterns can be replaced with custom patterns by setting the
 * associated properties of DateTimeFormatInfo.
 *
 * The following table lists the standard format characters for each standard
 * pattern and the associated DateTimeFormatInfo property that can be set to
 * modify the standard pattern. The format characters are case-sensitive;
 * for example, 'g' and 'G' represent slightly different patterns.
 *
 * <code>
 *  Format Character    Associated Property     Example Format Pattern (en-US)
 *  --------------------------------------------------------------------------
 *  d                   ShortDatePattern        MM/dd/yyyy
 *  D                   LongDatePattern         dddd, dd MMMM yyyy
 *  F                   FullDateTimePattern     dddd, dd MMMM yyyy HH:mm:ss
 *  m, M                MonthDayPattern         MMMM dd
 *  r, R                RFC1123Pattern          ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
 *  s                   SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss
 *  t                   ShortTimePattern        HH:mm
 *  T                   LongTimePattern         HH:mm:ss
 *  Y                   YearMonthPattern        yyyy MMMM
 *  --------------------------------------------------------------------------
 * </code>
 *
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
 * @version v1.0, last update on Fri Dec 03 22:30:31 EST 2004
 * @package System.I18N.core
 */
class DateTimeFormatInfo
{
	/**
	 * ICU date time formatting data.
	 * @var array
	 */
	private $data = array();

	/**
	 * A list of properties that are accessable/writable.
	 * @var array
	 */
	protected $properties = array();

	/**
	 * Allow functions that begins with 'set' to be called directly
	 * as an attribute/property to retrieve the value.
	 * @return mixed
	 */
	function __get($name)
	{
		$getProperty = 'get'.$name;
		if(in_array($getProperty, $this->properties))
			return $this->$getProperty();
		else
			throw new Exception('Property '.$name.' does not exists.');
	}

	/**
	 * Allow functions that begins with 'set' to be called directly
	 * as an attribute/property to set the value.
	 */
	function __set($name, $value)
	{
		$setProperty = 'set'.$name;
		if(in_array($setProperty, $this->properties))
			$this->$setProperty($value);
		else
			throw new Exception('Property '.$name.' can not be set.');
	}

	/**
	 * Initializes a new writable instance of the DateTimeFormatInfo class
	 * that is dependent on the ICU data for date time formatting
	 * information. <b>N.B.</b>You should not initialize this class directly
	 * unless you know what you are doing. Please use use
	 * DateTimeFormatInfo::getInstance() to create an instance.
	 * @param array ICU data for date time formatting.
	 * @see getInstance()
	 */
	function __construct($data=array())
	{
		$this->properties = get_class_methods($this);

		if(empty($data))
			throw new Exception('Please provide the ICU data to initialize.');

		$this->data = $data;
	}

	/**
	 * Get the internal ICU data for date time formatting.
	 * @return array ICU date time formatting data.
	 */
	protected function getData()
	{
		return $this->data;
	}

	/**
	 * Gets the default DateTimeFormatInfo that is culture-independent
	 * (invariant).
	 * @return DateTimeFormatInfo default DateTimeFormatInfo.
	 */
    static function getInvariantInfo()
    {
        static $invariant;
		if(is_null($invariant))
        {
            $culture = CultureInfo::getInvariantCulture();
            $invariant = $culture->getDateTimeFormat();
        }
		return $invariant;
    }

    /**
     * Returns the DateTimeFormatInfo associated with the specified culture.
     * @param CultureInfo the culture that gets the DateTimeFormat property.
     * @return DateTimeFormatInfo DateTimeFormatInfo for the specified
     * culture.
     */
    static function getInstance($culture=null)
    {

        if ($culture instanceof CultureInfo)
            return $culture->getDateTimeFormat();
       	else if(is_string($culture))
       	{
       		$cultureInfo = CultureInfo::getInstance($culture);
       		return $cultureInfo->getDateTimeFormat();
       	}
       	else
       	{
			$cultureInfo = CultureInfo::getInvariantCulture();
            return $cultureInfo->getDateTimeFormat();
       	}
    }

	/**
	 * A one-dimensional array of type String containing
	 * the culture-specific abbreviated names of the days
	 * of the week. The array for InvariantInfo contains
	 * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat".
     * @return array abbreviated day names
	 */
	function getAbbreviatedDayNames()
	{
		return $this->data['dayNames']['format']['abbreviated'];
		//return $this->data['dayNames/format/abbreviated'];
	}

    /**
     * Set the abbreviated day names. The value should be
     * an array of string starting with Sunday and ends in Saturady.
     * For example,
     * <code>array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");</code>
     * @param array abbreviated day names.
     */
    function setAbbreviatedDayNames($value)
    {
    	$this->data['dayNames']['format']['abbreviated'] = $value;
    }

	/**
	 * A one-dimensional array of type String containing
	 * the culture-specific narrow names of the days
	 * of the week. The array for InvariantInfo contains
	 * "S", "M", "T", "W", "T", "F", and "S".
     * @return array narrow day names
	 */
	function getNarrowDayNames()
	{
		return $this->data['dayNames']['format']['narrow'];
	}

    /**
     * Set the narrow day names. The value should be
     * an array of string starting with Sunday and ends in Saturady.
     * For example,
     * <code>array("S", "M", "T", "W", "T", "F", "S");</code>
     * @param array narrow day names.
     */
	function setNarrowDayNames($value)
	{
		$this->data['dayNames']['format']['narrow'] = $value;
	}

	/**
	 * A one-dimensional array of type String containing the
	 * culture-specific full names of the days of the week.
	 * The array for InvariantInfo contains "Sunday", "Monday",
	 * "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday".
     * @return array day names
	 */
	function getDayNames()
	{
		return $this->data['dayNames']['format']['wide'];
	}


    /**
     * Set the day names. The value should be
     * an array of string starting with Sunday and ends in Saturady.
     * For example,
     * <code>array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
     * "Friday", "Saturday".);</code>
     * @param array day names.
     */
	function setDayNames($value)
	{
		$this->data['dayNames']['format']['wide'] = $value;
	}

	/**
	 * A one-dimensional array of type String containing the
	 * culture-specific narrow names of the months. The array
	 * for InvariantInfo contains "J", "F", "M", "A", "M", "J",
	 * "J", "A", "S", "O", "N", and "D".
	 * @return array narrow month names.
	 */
	function getNarrowMonthNames()
	{
		return $this->data['monthNames']['format']['narrow'];
	}

    /**
     * Set the narrow month names. The value should be
     * an array of string starting with J and ends in D.
     * For example,
     * <code>array("J","F","M","A","M","J","J","A","S","O","N","D");</code>
     * @param array month names.
     */
    function setNarrowMonthNames($value)
    {
        $this->data['monthNames']['format']['narrow'] = $value;
    }

	/**
	 * A one-dimensional array of type String containing the
	 * culture-specific abbreviated names of the months. The array
	 * for InvariantInfo contains "Jan", "Feb", "Mar", "Apr", "May",
	 * "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and "Dec".
	 * Returns wide names if abbreviated names doesn't exist.
	 * @return array abbreviated month names.
	 */
	function getAbbreviatedMonthNames()
	{
		if (isset($this->data['monthNames']['format']['abbreviated']))
			return $this->data['monthNames']['format']['abbreviated'];
		else
			return $this->data['monthNames']['format']['wide'];
	}

    /**
     * Set the abbreviated month names. The value should be
     * an array of string starting with Jan and ends in Dec.
     * For example,
     * <code>array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
     * "Jul", "Aug", "Sep","Oct","Nov","Dec");</code>
     * @param array month names.
     */
    function setAbbreviatedMonthNames($value)
    {
        $this->data['monthNames']['format']['abbreviated'] = $value;
    }

	/**
	 * A one-dimensional array of type String containing the
	 * culture-specific full names of the months. The array for
	 * InvariantInfo contains "January", "February", "March", "April",
	 * "May", "June", "July", "August", "September", "October", "November",
	 * and "December"
	 * @return array month names.
	 */
	function getMonthNames()
	{
		return $this->data['monthNames']['format']['wide'];
	}

    /**
     * Set the month names. The value should be
     * an array of string starting with Janurary and ends in December.
     * For example,
     * <code>array("January", "February", "March", "April", "May", "June",
     * "July", "August", "September","October","November","December");</code>
     * @param array month names.
     */
    function setMonthNames($value)
    {
    	$this->data['monthNames']['format']['wide'] = $value;
    }

	/**
	 * A string containing the name of the era.
	 * @param int era The integer representing the era.
	 * @return string the era name.
	 */
	function getEra($era)
	{
		$eraName = $this->data['eras']['abbreviated'];
		return $eraName[$era];
	}

	/**
	 * The string designator for hours that are "ante meridiem" (before noon).
	 * The default for InvariantInfo is "AM".
	 * @return string AM designator.
	 */
	function getAMDesignator()
	{
		$result = $this->getAMPMMarkers();
		return $result[0];
	}

    /**
     * Set the AM Designator. For example, 'AM'.
     * @param string AM designator.
     */
    function setAMDesignator($value)
    {
        $markers = $this->getAMPMMarkers();
        $markers[0] = $value;
        $this->setAMPMMarkers($markers);
    }

	/**
	 * The string designator for hours that are "post meridiem" (after noon).
	 * The default for InvariantInfo is "PM".
	 * @return string PM designator.
	 */
	function getPMDesignator()
	{
		$result = $this->getAMPMMarkers();
		return $result[1];
	}

    /**
     * Set the PM Designator. For example, 'PM'.
     * @param string PM designator.
     */
    function setPMDesignator($value)
    {
        $markers = $this->getAMPMMarkers();
        $markers[1] = $value;
        $this->setAMPMMarkers($markers);
    }

    /**
     * Get the AM and PM markers array.
     * Default InvariantInfo for AM and PM is <code>array('AM','PM');</code>
     * @return array AM and PM markers
     */
    function getAMPMMarkers()
	{
		return $this->data['AmPmMarkers'];
	}

    /**
     * Set the AM and PM markers array.
     * For example <code>array('AM','PM');</code>
     * @param array AM and PM markers
     */
    function setAMPMMarkers($value)
    {
        $this->data['AmPmMarkers'] = $value;
    }

	/**
	 * Returns the full time pattern "HH:mm:ss z" (default).
     * This is culture sensitive.
     * @return string pattern "HH:mm:ss z".
	 */
	function getFullTimePattern()
	{
		return $this->data['DateTimePatterns'][0];
	}

	/**
	 * Returns the long time pattern "HH:mm:ss z" (default).
     * This is culture sensitive.
     * @return string pattern "HH:mm:ss z".
	 */
	function getLongTimePattern()
	{
		return $this->data['DateTimePatterns'][1];
	}

	/**
	 * Returns the medium time pattern "HH:mm:ss" (default).
     * This is culture sensitive.
     * @return string pattern "HH:mm:ss".
	 */
	function getMediumTimePattern()
	{
		return $this->data['DateTimePatterns'][2];
	}

	/**
	 * Returns the short time pattern "HH:mm" (default).
     * This is culture sensitive.
     * @return string pattern "HH:mm".
	 */
	function getShortTimePattern()
	{
		return $this->data['DateTimePatterns'][3];
	}

	/**
	 * Returns the full date pattern "EEEE, yyyy MMMM dd" (default).
     * This is culture sensitive.
     * @return string pattern "EEEE, yyyy MMMM dd".
	 */
	function getFullDatePattern()
	{
		return $this->data['DateTimePatterns'][4];
	}

	/**
	 * Returns the long date pattern "yyyy MMMM d" (default).
     * This is culture sensitive.
     * @return string pattern "yyyy MMMM d".
	 */
	function getLongDatePattern()
	{
		return $this->data['DateTimePatterns'][5];
	}

	/**
	 * Returns the medium date pattern "yyyy MMMM d" (default).
     * This is culture sensitive.
     * @return string pattern "yyyy MMM d".
	 */
	function getMediumDatePattern()
	{
		return $this->data['DateTimePatterns'][6];
	}

	/**
	 * Returns the short date pattern "yy/MM/dd" (default).
     * This is culture sensitive.
     * @return string pattern "yy/MM/dd".
	 */
	function getShortDatePattern()
	{
		return $this->data['DateTimePatterns'][7];
	}

    /**
     * Returns the date time order pattern, "{1} {0}" (default).
     * This is culture sensitive.
     * @return string pattern "{1} {0}".
     */
    function getDateTimeOrderPattern()
    {
        return $this->data['DateTimePatterns'][8];
    }

	/**
	 * Formats the date and time in a culture sensitive paterrn.
     * The default is "Date Time".
     * @return string date and time formated
	 */
	function formatDateTime($date, $time)
	{
		$pattern = $this->getDateTimeOrderPattern();
		return str_replace(array('{0}','{1}'), array($time, $date), $pattern);
	}

}
?>