/
habaridatetime.php
327 lines (288 loc) · 7.86 KB
/
habaridatetime.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
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
<?php
/**
* @package Habari
*
*/
/**
* HabariDateTime class to wrap dates in.
*
* @property-read HabariDateTime $clone Returns a clonned object.
* @property-read string $sql Returns a unix timestamp for inserting into DB.
* @property-read int $int Returns a unix timestamp as integer.
*/
class HabariDateTime extends DateTime
{
private static $default_timezone;
private static $default_datetime_format = 'c';
private static $default_date_format;
private static $default_time_format;
/**
* Set default timezone to system default on init.
*
* @static
*/
public static function __static()
{
if ( Options::get( 'timezone' ) ) {
self::set_default_timezone( Options::get( 'timezone' ) );
}
self::$default_timezone = date_default_timezone_get();
self::$default_date_format = Options::get('dateformat');
self::$default_time_format = Options::get('timeformat');
if ( self::$default_date_format || self::$default_time_format ) {
self::set_default_datetime_format( self::$default_date_format . ' ' . self::$default_time_format );
}
}
/**
* Set default date/time format. The format is the same as the
* internal php {@link http://ca.php.net/date date() function}.
*
* @static
* @param string $format The date format.
*/
public static function set_default_datetime_format( $format )
{
self::$default_datetime_format = $format;
}
/**
* Get the default date/time format set.
*
* @static
* @see set_default_datetime_format()
* @return string The date format set.
*/
public static function get_default_datetime_format()
{
return self::$default_datetime_format;
}
/**
* Sets the timezone for Habari and PHP.
*
* @static
* @param string $timezone A timezone name, not an abbreviation, for example 'America/New York'
*/
public static function set_default_timezone( $timezone )
{
self::$default_timezone = $timezone;
date_default_timezone_set( self::$default_timezone );
}
/**
* Get the timezone for Habari and PHP.
* Defaults to system timezone if not set.
*
* @static
* @see set_default_timezone()
* @param string The deafult timezone.
*/
public static function get_default_timezone()
{
return self::$default_timezone;
}
/**
* Helper function to create a HabariDateTime object for the given
* time and timezone. If no time is given, defaults to 'now'. If no
* timezone given defaults to timezone set in {@link set_default_timezone()}
*
* @static
* @see DateTime::__construct()
* @param string $time String in a format accepted by
* {@link http://ca.php.net/strtotime strtotime()}, defaults to "now".
* @param string $timezone A timezone name, not an abbreviation.
*/
public static function date_create( $time = null, $timezone = null )
{
if ( $time instanceOf HabariDateTime ) {
return $time;
}
elseif ( $time instanceOf DateTime ) {
$time = $time->format('U');
}
elseif ( $time == null ) {
$time = 'now';
}
elseif ( is_numeric($time) ) {
$time = '@' . $time;
}
if ( $timezone === null ) {
$timezone = self::$default_timezone;
}
// passing the timezone to construct doesn't seem to do anything.
$datetime = new HabariDateTime($time);
$datetime->set_timezone($timezone);
return $datetime;
}
/**
* Set the date of this object
*
* @see DateTime::setDate()
* @param int $year Year of the date
* @param int $month Month of the date
* @param int $day Day of the date
*/
public function set_date( $year, $month, $day )
{
parent::setDate($year, $month, $day);
return $this;
}
/**
* Sets the ISO date
*
* @see DateTime::setISODate()
* @param int $year Year of the date
* @param int $month Month of the date
* @param int $day Day of the date
*/
public function set_isodate( $year, $week, $day = null )
{
parent::setISODate($year, $week, $day);
return $this;
}
/**
* Set the time of this object
*
* @see DateTime::setTime()
* @param int $hour Hour of the time
* @param int $minute Minute of the time
* @param int $second Second of the time
*/
public function set_time( $hour, $minute, $second = null )
{
parent::setTime($hour, $minute, $second);
return $this;
}
/**
* Set the timezone for this datetime object. Can be either string
* timezone identifier, or DateTimeZone object.
*
* @see DateTime::setTimezone()
* @param mixed The timezone to use.
* @return HabariDateTime $this object.
*/
public function set_timezone( $timezone )
{
if ( ! $timezone instanceof DateTimeZone ) {
$timezone = new DateTimeZone($timezone);
}
parent::setTimezone($timezone);
return $this;
}
/**
* Get the timezone identifier that is set for this datetime object.
*
* @return DateTimeZone The timezone object.
*/
public function get_timezone()
{
return parent::getTimezone();
}
/**
* Returns date formatted according to given format.
*
* @see DateTime::format()
* @param string $format Format accepted by {@link http://php.net/date date()}.
* @return string The formatted date, false on failure.
*/
public function format($format = null)
{
if ( $format === null ) {
$format = self::$default_datetime_format;
}
return parent::format($format);
}
public function text_format($format)
{
return preg_replace_callback('%\{(\w)\}%i', array($this, 'text_format_callback'), $format);
}
private function text_format_callback($matches)
{
return $this->format($matches[1]);
}
/**
* Alters the timestamp
*
* @param string $format A format accepted by {@link http://php.net/strtotime strtotime()}.
* @return HabariDateTime $this object.
*/
public function modify( $args )
{
parent::modify( $args );
return $this;
}
/**
* @see format()
*/
public function get($format = null)
{
return $this->format($format);
}
/**
* Echos date formatted according to given format.
*
* @see format()
* @param string $format Format accepted by {@link http://php.net/date date()}.
*/
public function out($format = null)
{
echo $this->format($format);
}
/**
* Magic method called when this object is cast to string. Returns the
* unix timestamp of this object.
*
* @return string The unix timestamp
*/
public function __toString()
{
return $this->format('U');
}
/**
* Magic method to get magic ponies... properties, I mean.
*/
public function __get($property)
{
switch ($property) {
case 'clone':
return clone $this;
case 'sql':
return $this->format('U');
break;
case 'int':
return intval( $this->format('U') );
break;
case 'time':
return $this->format( self::get_default_time_format() );
break;
case 'date':
return $this->format( self::get_default_date_format() );
break;
default:
$info = getdate($this->format('U'));
$info['mon0'] = substr('0' . $info['mon'], -2, 2);
$info['mday0'] = substr('0' . $info['mday'], -2, 2);
if(isset($info[$property])) {
return $info[$property];
}
return $this->$property;
}
}
public static function get_default_date_format ( ) {
return self::$default_date_format;
}
public static function get_default_time_format ( ) {
return self::$default_time_format;
}
/**
* Returns an associative array containing the date information for
* this HabariDateTime object, as per {@link http://php.net/getdate getdate()}
*
* @return array Associative array containing the date information
*/
public function getdate()
{
$info = getdate($this->format('U'));
$info['mon0'] = substr('0' . $info['mon'], -2, 2);
$info['mday0'] = substr('0' . $info['mday'], -2, 2);
return $info;
}
}
?>