3333 * @package ActiveRecord
3434 * @see http://php.net/manual/en/class.datetime.php
3535 */
36- class DateTime extends \DateTime
36+ class DateTime extends \DateTime implements DateTimeInterface
3737{
3838 /**
3939 * Default format used for format() and __toString()
@@ -113,11 +113,40 @@ public static function get_format($format=null)
113113 return $ format ;
114114 }
115115
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+
116131 public function __toString ()
117132 {
118133 return $ this ->format ();
119134 }
120135
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+
121150 private function flag_dirty ()
122151 {
123152 if ($ this ->model )
@@ -127,24 +156,49 @@ private function flag_dirty()
127156 public function setDate ($ year , $ month , $ day )
128157 {
129158 $ this ->flag_dirty ();
130- call_user_func_array ( array ( $ this , ' parent::setDate ' ), func_get_args () );
159+ return parent ::setDate ( $ year , $ month , $ day );
131160 }
132161
133- public function setISODate ($ year , $ week , $ day= null )
162+ public function setISODate ($ year , $ week , $ day = 1 )
134163 {
135164 $ this ->flag_dirty ();
136- call_user_func_array ( array ( $ this , ' parent::setISODate ' ), func_get_args () );
165+ return parent ::setISODate ( $ year , $ week , $ day );
137166 }
138167
139- public function setTime ($ hour , $ minute , $ second= null )
168+ public function setTime ($ hour , $ minute , $ second = 0 )
140169 {
141170 $ this ->flag_dirty ();
142- call_user_func_array ( array ( $ this , ' parent::setTime ' ), func_get_args () );
171+ return parent ::setTime ( $ hour , $ minute , $ second );
143172 }
144173
145174 public function setTimestamp ($ unixtimestamp )
146175 {
147176 $ this ->flag_dirty ();
148- call_user_func_array ( array ( $ this , ' parent::setTimestamp ' ), func_get_args () );
177+ return parent ::setTimestamp ( $ unixtimestamp );
149178 }
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