Esempio n. 1
0
 public function string_to_datetime($string)
 {
     $date = date_create(str_replace('.000000', '', $string));
     $errors = \DateTime::getLastErrors();
     if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
         return null;
     }
     return $date;
 }
Esempio n. 2
0
 /**
  * Converts a string representation of a datetime into a DateTime object.
  *
  * @param string $string A datetime in the form accepted by date_create()
  * @return DateTime
  */
 public function string_to_datetime($string)
 {
     $date = date_create($string);
     $errors = \DateTime::getLastErrors();
     if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
         return null;
     }
     return new DateTime($date->format('Y-m-d H:i:s'));
 }
Esempio n. 3
0
 /**
  * Converts a string representation of a datetime into a DateTime object.
  *
  * @param string $string A datetime in the form accepted by date_create()
  * @return object The date_class set in Config
  */
 public function string_to_datetime($string)
 {
     $date = date_create($string);
     $errors = \DateTime::getLastErrors();
     if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
         return null;
     }
     $date_class = Config::instance()->get_date_class();
     return $date_class::createFromFormat(static::DATETIME_TRANSLATE_FORMAT, $date->format(static::DATETIME_TRANSLATE_FORMAT), $date->getTimezone());
 }
 public function testToString()
 {
     $this->assertEquals(date(DateTime::getFormat()), "" . $this->date);
 }
 public function test_datetime_values_get_converted_to_strings()
 {
     $now = new DateTime();
     $a = $this->_a(array('only' => 'created_at'), new Author(array('created_at' => $now)));
     $this->assert_equals($now->format(ActiveRecord\Serialization::$DATETIME_FORMAT), $a['created_at']);
 }
Esempio n. 6
0
 /**
  * Return a date time formatted into the database's datetime format.
  *
  * @param DateTime $datetime The DateTime object
  * @return string
  */
 public function datetimeToString($datetime)
 {
     return $datetime->format(static::$datetime_format);
 }
 public function test_to_string()
 {
     $this->assert_equals(date(DateTime::get_format()), "" . $this->date);
 }
Esempio n. 8
0
 public function test_clone()
 {
     $model = $this->get_model();
     $model_attribute = 'some_date';
     $datetime = new DateTime();
     $datetime->attribute_of($model, $model_attribute);
     $cloned_datetime = clone $datetime;
     // Assert initial state
     $this->assert_false($model->attribute_is_dirty($model_attribute));
     $cloned_datetime->add(new DateInterval('PT1S'));
     // Assert that modifying the cloned object didn't flag the model
     $this->assert_false($model->attribute_is_dirty($model_attribute));
     $datetime->add(new DateInterval('PT1S'));
     // Assert that modifying the model-attached object did flag the model
     $this->assert_true($model->attribute_is_dirty($model_attribute));
     // Assert that the dates are equal but not the same instance
     $this->assert_equals($datetime, $cloned_datetime);
     $this->assert_not_same($datetime, $cloned_datetime);
 }
Esempio n. 9
0
 /**
  * Converts a string representation of a datetime into a DateTime object.
  *
  * @param string $string A datetime in the form accepted by date_create()
  * @return DateTime
  */
 public function string_to_datetime($string)
 {
     $date = date_create($string);
     $errors = \DateTime::getLastErrors();
     if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
         return null;
     }
     //		return new DateTime($date->format(static::$datetime_format));
     //== My change ==//
     $date_format = Config::instance()->get_date_format();
     return new DateTime($date->format($date_format));
     //== End my change ==//
 }
Esempio n. 10
0
 /**
  * Assign a value to an attribute.
  *
  * @param string $name Name of the attribute
  * @param mixed &$value Value of the attribute
  * @return mixed the attribute value
  */
 public function assignAttribute($name, $value)
 {
     $table = static::table();
     if (!\is_object($value)) {
         if (\array_key_exists($name, $table->columns)) {
             $value = $table->columns[$name]->cast($value, static::connection());
         } else {
             $col = $table->getColumnByInflectedName($name);
             if (!\is_null($col)) {
                 $value = $col->cast($value, static::connection());
             }
         }
     }
     // convert php's \DateTime to ours
     if ($value instanceof \DateTime) {
         $value = new DateTime($value->format('Y-m-d H:i:s T'), $value->getTimezone());
     }
     // make sure DateTime values know what model they belong to so
     // dirty stuff works when calling set methods on the DateTime object
     if ($value instanceof DateTime) {
         $value->attributeOf($this, $name);
     }
     $this->attributes[$name] = $value;
     $this->flagDirty($name);
     return $value;
 }
Esempio n. 11
0
 public function test_datetime_values_get_converted_to_strings()
 {
     $now = new DateTime();
     $a = $this->_a(array('only' => 'created_at'), new Author(array('created_at' => $now)));
     $this->assert_equals($now->format(ActiveRecord\Config::instance()->get_date_format()), $a['created_at']);
 }
Esempio n. 12
0
 public function testCreateFromFormatWithoutTz()
 {
     $d = DateTime::createFromFormat('H:i:s Y-d-m', '03:04:05 2000-02-01');
     $this->assertEquals(new DateTime('2000-01-02 03:04:05'), $d);
 }