33
33
* @package ActiveRecord
34
34
* @see http://php.net/manual/en/class.datetime.php
35
35
*/
36
- class DateTime extends \DateTime
36
+ class DateTime extends \DateTime implements DateTimeInterface
37
37
{
38
38
/**
39
39
* Default format used for format() and __toString()
@@ -113,11 +113,40 @@ public static function get_format($format=null)
113
113
return $ format ;
114
114
}
115
115
116
+ /**
117
+ * This needs to be overriden so it returns an instance of this class instead of PHP's \DateTime.
118
+ * See http://php.net/manual/en/datetime.createfromformat.php
119
+ */
120
+ public static function createFromFormat ($ format , $ time , $ tz = null )
121
+ {
122
+ $ phpDate = $ tz ? parent ::createFromFormat ($ format , $ time , $ tz ) : parent ::createFromFormat ($ format , $ time );
123
+ if (!$ phpDate )
124
+ return false ;
125
+ // convert to this class using the timestamp
126
+ $ ourDate = new static (null , $ phpDate ->getTimezone ());
127
+ $ ourDate ->setTimestamp ($ phpDate ->getTimestamp ());
128
+ return $ ourDate ;
129
+ }
130
+
116
131
public function __toString ()
117
132
{
118
133
return $ this ->format ();
119
134
}
120
135
136
+ /**
137
+ * Handle PHP object `clone`.
138
+ *
139
+ * This makes sure that the object doesn't still flag an attached model as
140
+ * dirty after cloning the DateTime object and making modifications to it.
141
+ *
142
+ * @return void
143
+ */
144
+ public function __clone ()
145
+ {
146
+ $ this ->model = null ;
147
+ $ this ->attribute_name = null ;
148
+ }
149
+
121
150
private function flag_dirty ()
122
151
{
123
152
if ($ this ->model )
@@ -127,24 +156,49 @@ private function flag_dirty()
127
156
public function setDate ($ year , $ month , $ day )
128
157
{
129
158
$ this ->flag_dirty ();
130
- call_user_func_array ( array ( $ this , ' parent::setDate ' ), func_get_args () );
159
+ return parent ::setDate ( $ year , $ month , $ day );
131
160
}
132
161
133
- public function setISODate ($ year , $ week , $ day= null )
162
+ public function setISODate ($ year , $ week , $ day = 1 )
134
163
{
135
164
$ this ->flag_dirty ();
136
- call_user_func_array ( array ( $ this , ' parent::setISODate ' ), func_get_args () );
165
+ return parent ::setISODate ( $ year , $ week , $ day );
137
166
}
138
167
139
- public function setTime ($ hour , $ minute , $ second= null )
168
+ public function setTime ($ hour , $ minute , $ second = 0 )
140
169
{
141
170
$ this ->flag_dirty ();
142
- call_user_func_array ( array ( $ this , ' parent::setTime ' ), func_get_args () );
171
+ return parent ::setTime ( $ hour , $ minute , $ second );
143
172
}
144
173
145
174
public function setTimestamp ($ unixtimestamp )
146
175
{
147
176
$ this ->flag_dirty ();
148
- call_user_func_array ( array ( $ this , ' parent::setTimestamp ' ), func_get_args () );
177
+ return parent ::setTimestamp ( $ unixtimestamp );
149
178
}
150
- }
179
+
180
+ public function setTimezone ($ timezone )
181
+ {
182
+ $ this ->flag_dirty ();
183
+ return parent ::setTimezone ($ timezone );
184
+ }
185
+
186
+ public function modify ($ modify )
187
+ {
188
+ $ this ->flag_dirty ();
189
+ return parent ::modify ($ modify );
190
+ }
191
+
192
+ public function add ($ interval )
193
+ {
194
+ $ this ->flag_dirty ();
195
+ return parent ::add ($ interval );
196
+ }
197
+
198
+ public function sub ($ interval )
199
+ {
200
+ $ this ->flag_dirty ();
201
+ return parent ::sub ($ interval );
202
+ }
203
+
204
+ }
0 commit comments