Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/**
4
 * DateTimeFormatInfo class file.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the BSD License.
8
 *
9
 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10
 *
11
 * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12
 * The latest version of PRADO can be obtained from:
13
 * {@link http://prado.sourceforge.net/}
14
 *
15
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
16
 * @version $Revision: 1.2 $  $Date: 2005/01/05 03:15:14 $
17
 * @package System.I18N.core
18
 */
19
 
20
/**
21
 * Get the CultureInfo class.
22
 */
23
require_once(dirname(__FILE__).'/CultureInfo.php');
24
 
25
 
26
/**
27
 * Defines how DateTime values are formatted and displayed, depending
28
 * on the culture.
29
 *
30
 * This class contains information, such as date patterns, time patterns,
31
 * and AM/PM designators.
32
 *
33
 * To create a DateTimeFormatInfo for a specific culture, create a
34
 * CultureInfo for that culture and retrieve the CultureInfo.DateTimeFormat
35
 * property. For example:
36
 * <code>
37
 * $culture = new CultureInfo('en_AU');
38
 * $dtfi = $culture->DateTimeFormat;
39
 * </code>
40
 *
41
 * To create a DateTimeFormatInfo for the invariant culture, use
42
 * <code>
43
 * DateTimeFormatInfo::getInstance($culture=null);
44
 * </code>
45
 * you may pass a CultureInfo parameter $culture to get the DateTimeFormatInfo
46
 * for a specific culture.
47
 *
48
 * DateTime values are formatted using standard or custom patterns stored in
49
 * the properties of a DateTimeFormatInfo.
50
 *
51
 * The standard patterns can be replaced with custom patterns by setting the
52
 * associated properties of DateTimeFormatInfo.
53
 *
54
 * The following table lists the standard format characters for each standard
55
 * pattern and the associated DateTimeFormatInfo property that can be set to
56
 * modify the standard pattern. The format characters are case-sensitive;
57
 * for example, 'g' and 'G' represent slightly different patterns.
58
 *
59
 * <code>
60
 *  Format Character    Associated Property     Example Format Pattern (en-US)
61
 *  --------------------------------------------------------------------------
62
 *  d                   ShortDatePattern        MM/dd/yyyy
63
 *  D                   LongDatePattern         dddd, dd MMMM yyyy
64
 *  F                   FullDateTimePattern     dddd, dd MMMM yyyy HH:mm:ss
65
 *  m, M                MonthDayPattern         MMMM dd
66
 *  r, R                RFC1123Pattern          ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
67
 *  s                   SortableDateTimePattern yyyy'-'MM'-'dd'T'HH':'mm':'ss
68
 *  t                   ShortTimePattern        HH:mm
69
 *  T                   LongTimePattern         HH:mm:ss
70
 *  Y                   YearMonthPattern        yyyy MMMM
71
 *  --------------------------------------------------------------------------
72
 * </code>
73
 *
74
 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
75
 * @version v1.0, last update on Fri Dec 03 22:30:31 EST 2004
76
 * @package System.I18N.core
77
 */
78
class DateTimeFormatInfo
79
{
80
	/**
81
	 * ICU date time formatting data.
82
	 * @var array
83
	 */
84
	private $data = array();
85
 
86
	/**
87
	 * A list of properties that are accessable/writable.
88
	 * @var array
89
	 */
90
	protected $properties = array();
91
 
92
	/**
93
	 * Allow functions that begins with 'set' to be called directly
94
	 * as an attribute/property to retrieve the value.
95
	 * @return mixed
96
	 */
97
	function __get($name)
98
	{
99
		$getProperty = 'get'.$name;
100
		if(in_array($getProperty, $this->properties))
101
			return $this->$getProperty();
102
		else
103
			throw new Exception('Property '.$name.' does not exists.');
104
	}
105
 
106
	/**
107
	 * Allow functions that begins with 'set' to be called directly
108
	 * as an attribute/property to set the value.
109
	 */
110
	function __set($name, $value)
111
	{
112
		$setProperty = 'set'.$name;
113
		if(in_array($setProperty, $this->properties))
114
			$this->$setProperty($value);
115
		else
116
			throw new Exception('Property '.$name.' can not be set.');
117
	}
118
 
119
	/**
120
	 * Initializes a new writable instance of the DateTimeFormatInfo class
121
	 * that is dependent on the ICU data for date time formatting
122
	 * information. <b>N.B.</b>You should not initialize this class directly
123
	 * unless you know what you are doing. Please use use
124
	 * DateTimeFormatInfo::getInstance() to create an instance.
125
	 * @param array ICU data for date time formatting.
126
	 * @see getInstance()
127
	 */
128
	function __construct($data=array())
129
	{
130
		$this->properties = get_class_methods($this);
131
 
132
		if(empty($data))
133
			throw new Exception('Please provide the ICU data to initialize.');
134
 
135
		$this->data = $data;
136
	}
137
 
138
	/**
139
	 * Get the internal ICU data for date time formatting.
140
	 * @return array ICU date time formatting data.
141
	 */
142
	protected function getData()
143
	{
144
		return $this->data;
145
	}
146
 
147
	/**
148
	 * Gets the default DateTimeFormatInfo that is culture-independent
149
	 * (invariant).
150
	 * @return DateTimeFormatInfo default DateTimeFormatInfo.
151
	 */
152
    static function getInvariantInfo()
153
    {
154
        static $invariant;
155
		if($invariant === null)
156
        {
157
            $culture = CultureInfo::getInvariantCulture();
158
            $invariant = $culture->getDateTimeFormat();
159
        }
160
		return $invariant;
161
    }
162
 
163
    /**
164
     * Returns the DateTimeFormatInfo associated with the specified culture.
165
     * @param CultureInfo the culture that gets the DateTimeFormat property.
166
     * @return DateTimeFormatInfo DateTimeFormatInfo for the specified
167
     * culture.
168
     */
169
    static function getInstance($culture=null)
170
    {
171
 
172
        if ($culture instanceof CultureInfo)
173
            return $culture->getDateTimeFormat();
174
       	else if(is_string($culture))
175
       	{
176
       		$cultureInfo = CultureInfo::getInstance($culture);
177
       		return $cultureInfo->getDateTimeFormat();
178
       	}
179
       	else
180
       	{
181
			$cultureInfo = CultureInfo::getInvariantCulture();
182
            return $cultureInfo->getDateTimeFormat();
183
       	}
184
    }
185
 
186
	/**
187
	 * A one-dimensional array of type String containing
188
	 * the culture-specific abbreviated names of the days
189
	 * of the week. The array for InvariantInfo contains
190
	 * "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", and "Sat".
191
     * @return array abbreviated day names
192
	 */
193
	function getAbbreviatedDayNames()
194
	{
195
		return $this->data['dayNames']['format']['abbreviated'];
196
		//return $this->data['dayNames/format/abbreviated'];
197
	}
198
 
199
    /**
200
     * Set the abbreviated day names. The value should be
201
     * an array of string starting with Sunday and ends in Saturady.
202
     * For example,
203
     * <code>array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");</code>
204
     * @param array abbreviated day names.
205
     */
206
    function setAbbreviatedDayNames($value)
207
    {
208
    	$this->data['dayNames']['format']['abbreviated'] = $value;
209
    }
210
 
211
	/**
212
	 * A one-dimensional array of type String containing
213
	 * the culture-specific narrow names of the days
214
	 * of the week. The array for InvariantInfo contains
215
	 * "S", "M", "T", "W", "T", "F", and "S".
216
     * @return array narrow day names
217
	 */
218
	function getNarrowDayNames()
219
	{
220
		return $this->data['dayNames']['format']['narrow'];
221
	}
222
 
223
    /**
224
     * Set the narrow day names. The value should be
225
     * an array of string starting with Sunday and ends in Saturady.
226
     * For example,
227
     * <code>array("S", "M", "T", "W", "T", "F", "S");</code>
228
     * @param array narrow day names.
229
     */
230
	function setNarrowDayNames($value)
231
	{
232
		$this->data['dayNames']['format']['narrow'] = $value;
233
	}
234
 
235
	/**
236
	 * A one-dimensional array of type String containing the
237
	 * culture-specific full names of the days of the week.
238
	 * The array for InvariantInfo contains "Sunday", "Monday",
239
	 * "Tuesday", "Wednesday", "Thursday", "Friday", and "Saturday".
240
     * @return array day names
241
	 */
242
	function getDayNames()
243
	{
244
		return $this->data['dayNames']['format']['wide'];
245
	}
246
 
247
 
248
    /**
249
     * Set the day names. The value should be
250
     * an array of string starting with Sunday and ends in Saturady.
251
     * For example,
252
     * <code>array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
253
     * "Friday", "Saturday".);</code>
254
     * @param array day names.
255
     */
256
	function setDayNames($value)
257
	{
258
		$this->data['dayNames']['format']['wide'] = $value;
259
	}
260
 
261
	/**
262
	 * A one-dimensional array of type String containing the
263
	 * culture-specific narrow names of the months. The array
264
	 * for InvariantInfo contains "J", "F", "M", "A", "M", "J",
265
	 * "J", "A", "S", "O", "N", and "D".
266
	 * @return array narrow month names.
267
	 */
268
	function getNarrowMonthNames()
269
	{
270
		return $this->data['monthNames']['format']['narrow'];
271
	}
272
 
273
    /**
274
     * Set the narrow month names. The value should be
275
     * an array of string starting with J and ends in D.
276
     * For example,
277
     * <code>array("J","F","M","A","M","J","J","A","S","O","N","D");</code>
278
     * @param array month names.
279
     */
280
    function setNarrowMonthNames($value)
281
    {
282
        $this->data['monthNames']['format']['narrow'] = $value;
283
    }
284
 
285
	/**
286
	 * A one-dimensional array of type String containing the
287
	 * culture-specific abbreviated names of the months. The array
288
	 * for InvariantInfo contains "Jan", "Feb", "Mar", "Apr", "May",
289
	 * "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and "Dec".
290
	 * Returns wide names if abbreviated names doesn't exist.
291
	 * @return array abbreviated month names.
292
	 */
293
	function getAbbreviatedMonthNames()
294
	{
295
		if (isset($this->data['monthNames']['format']['abbreviated']))
296
			return $this->data['monthNames']['format']['abbreviated'];
297
		else
298
			return $this->data['monthNames']['format']['wide'];
299
	}
300
 
301
    /**
302
     * Set the abbreviated month names. The value should be
303
     * an array of string starting with Jan and ends in Dec.
304
     * For example,
305
     * <code>array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
306
     * "Jul", "Aug", "Sep","Oct","Nov","Dec");</code>
307
     * @param array month names.
308
     */
309
    function setAbbreviatedMonthNames($value)
310
    {
311
        $this->data['monthNames']['format']['abbreviated'] = $value;
312
    }
313
 
314
	/**
315
	 * A one-dimensional array of type String containing the
316
	 * culture-specific full names of the months. The array for
317
	 * InvariantInfo contains "January", "February", "March", "April",
318
	 * "May", "June", "July", "August", "September", "October", "November",
319
	 * and "December"
320
	 * @return array month names.
321
	 */
322
	function getMonthNames()
323
	{
324
		return $this->data['monthNames']['format']['wide'];
325
	}
326
 
327
    /**
328
     * Set the month names. The value should be
329
     * an array of string starting with Janurary and ends in December.
330
     * For example,
331
     * <code>array("January", "February", "March", "April", "May", "June",
332
     * "July", "August", "September","October","November","December");</code>
333
     * @param array month names.
334
     */
335
    function setMonthNames($value)
336
    {
337
    	$this->data['monthNames']['format']['wide'] = $value;
338
    }
339
 
340
	/**
341
	 * A string containing the name of the era.
342
	 * @param int era The integer representing the era.
343
	 * @return string the era name.
344
	 */
345
	function getEra($era)
346
	{
347
		$eraName = $this->data['eras']['abbreviated'];
348
		return $eraName[$era];
349
	}
350
 
351
	/**
352
	 * The string designator for hours that are "ante meridiem" (before noon).
353
	 * The default for InvariantInfo is "AM".
354
	 * @return string AM designator.
355
	 */
356
	function getAMDesignator()
357
	{
358
		$result = $this->getAMPMMarkers();
359
		return $result[0];
360
	}
361
 
362
    /**
363
     * Set the AM Designator. For example, 'AM'.
364
     * @param string AM designator.
365
     */
366
    function setAMDesignator($value)
367
    {
368
        $markers = $this->getAMPMMarkers();
369
        $markers[0] = $value;
370
        $this->setAMPMMarkers($markers);
371
    }
372
 
373
	/**
374
	 * The string designator for hours that are "post meridiem" (after noon).
375
	 * The default for InvariantInfo is "PM".
376
	 * @return string PM designator.
377
	 */
378
	function getPMDesignator()
379
	{
380
		$result = $this->getAMPMMarkers();
381
		return $result[1];
382
	}
383
 
384
    /**
385
     * Set the PM Designator. For example, 'PM'.
386
     * @param string PM designator.
387
     */
388
    function setPMDesignator($value)
389
    {
390
        $markers = $this->getAMPMMarkers();
391
        $markers[1] = $value;
392
        $this->setAMPMMarkers($markers);
393
    }
394
 
395
    /**
396
     * Get the AM and PM markers array.
397
     * Default InvariantInfo for AM and PM is <code>array('AM','PM');</code>
398
     * @return array AM and PM markers
399
     */
400
    function getAMPMMarkers()
401
	{
402
		return $this->data['AmPmMarkers'];
403
	}
404
 
405
    /**
406
     * Set the AM and PM markers array.
407
     * For example <code>array('AM','PM');</code>
408
     * @param array AM and PM markers
409
     */
410
    function setAMPMMarkers($value)
411
    {
412
        $this->data['AmPmMarkers'] = $value;
413
    }
414
 
415
	/**
416
	 * Returns the full time pattern "HH:mm:ss z" (default).
417
     * This is culture sensitive.
418
     * @return string pattern "HH:mm:ss z".
419
	 */
420
	function getFullTimePattern()
421
	{
422
		return $this->data['DateTimePatterns'][0];
423
	}
424
 
425
	/**
426
	 * Returns the long time pattern "HH:mm:ss z" (default).
427
     * This is culture sensitive.
428
     * @return string pattern "HH:mm:ss z".
429
	 */
430
	function getLongTimePattern()
431
	{
432
		return $this->data['DateTimePatterns'][1];
433
	}
434
 
435
	/**
436
	 * Returns the medium time pattern "HH:mm:ss" (default).
437
     * This is culture sensitive.
438
     * @return string pattern "HH:mm:ss".
439
	 */
440
	function getMediumTimePattern()
441
	{
442
		return $this->data['DateTimePatterns'][2];
443
	}
444
 
445
	/**
446
	 * Returns the short time pattern "HH:mm" (default).
447
     * This is culture sensitive.
448
     * @return string pattern "HH:mm".
449
	 */
450
	function getShortTimePattern()
451
	{
452
		return $this->data['DateTimePatterns'][3];
453
	}
454
 
455
	/**
456
	 * Returns the full date pattern "EEEE, yyyy MMMM dd" (default).
457
     * This is culture sensitive.
458
     * @return string pattern "EEEE, yyyy MMMM dd".
459
	 */
460
	function getFullDatePattern()
461
	{
462
		return $this->data['DateTimePatterns'][4];
463
	}
464
 
465
	/**
466
	 * Returns the long date pattern "yyyy MMMM d" (default).
467
     * This is culture sensitive.
468
     * @return string pattern "yyyy MMMM d".
469
	 */
470
	function getLongDatePattern()
471
	{
472
		return $this->data['DateTimePatterns'][5];
473
	}
474
 
475
	/**
476
	 * Returns the medium date pattern "yyyy MMMM d" (default).
477
     * This is culture sensitive.
478
     * @return string pattern "yyyy MMM d".
479
	 */
480
	function getMediumDatePattern()
481
	{
482
		return $this->data['DateTimePatterns'][6];
483
	}
484
 
485
	/**
486
	 * Returns the short date pattern "yy/MM/dd" (default).
487
     * This is culture sensitive.
488
     * @return string pattern "yy/MM/dd".
489
	 */
490
	function getShortDatePattern()
491
	{
492
		return $this->data['DateTimePatterns'][7];
493
	}
494
 
495
    /**
496
     * Returns the date time order pattern, "{1} {0}" (default).
497
     * This is culture sensitive.
498
     * @return string pattern "{1} {0}".
499
     */
500
    function getDateTimeOrderPattern()
501
    {
502
        return $this->data['DateTimePatterns'][8];
503
    }
504
 
505
	/**
506
	 * Formats the date and time in a culture sensitive paterrn.
507
     * The default is "Date Time".
508
     * @return string date and time formated
509
	 */
510
	function formatDateTime($date, $time)
511
	{
512
		$pattern = $this->getDateTimeOrderPattern();
513
		return str_replace(array('{0}','{1}'), array($time, $date), $pattern);
514
	}
515
 
516
}