Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 9, 2022
1 parent 23c3a8b commit 6815ec1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Agile Data defines some basic types to make sure that values:
A good example would be a `date` type::

$model->addField('birth', ['type' => 'date']);
$model->set('birth', DateTime::createFromFormat('m/d/Y', '1/10/2014'));
$model->set('birth', new DateTime('2014-01-10'));

$model->save();

Expand Down
4 changes: 1 addition & 3 deletions docs/persistence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ Dates and Time

.. todo:: this section might need cleanup

There are 4 date formats supported:
There are 3 datetime formats supported:

- ts (or timestamp): Stores in database using UTC. Defaults into unix
timestamp (int) in PHP.
- date: Converts into YYYY-MM-DD using UTC timezone for SQL. Defaults
to DateTime() class in PHP, but supports string input (parsed as date
in a current timezone) or unix timestamp.
Expand Down
9 changes: 0 additions & 9 deletions src/Model/FieldPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,4 @@ trait FieldPropertiesTrait
* @var bool|string
*/
public $required = false;

/**
* Persisting timezone for type = 'date', 'datetime', 'time' fields.
*
* For example, 'IST', 'UTC', 'Europe/Riga' etc.
*
* @var string
*/
public $persist_timezone = 'UTC';
}
18 changes: 6 additions & 12 deletions src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,10 @@ protected function _typecastSaveField(Field $field, $value)

if ($field->type === 'datetime') {
$value = new \DateTime($value->format('Y-m-d H:i:s.u'), $value->getTimezone());
$value->setTimezone(new \DateTimeZone($field->persist_timezone));
$value->setTimezone(new \DateTimeZone('UTC'));
}

$formats = ['date' => 'Y-m-d', 'datetime' => 'Y-m-d H:i:s.u', 'time' => 'H:i:s.u'];
$format = $field->persist_format ?: $formats[$field->type];
$format = ['date' => 'Y-m-d', 'datetime' => 'Y-m-d H:i:s.u', 'time' => 'H:i:s.u'][$field->type];
$value = $value->format($format);

return $value;
Expand Down Expand Up @@ -435,18 +434,13 @@ protected function _typecastLoadField(Field $field, $value)
// native DBAL DT types have no microseconds support
if (in_array($field->type, ['datetime', 'date', 'time'], true)
&& str_starts_with(get_class($field->getTypeObject()), 'Doctrine\DBAL\Types\\')) {
if ($field->persist_format) {
$format = $field->persist_format;
} else {
$formats = ['date' => 'Y-m-d', 'datetime' => 'Y-m-d H:i:s', 'time' => 'H:i:s'];
$format = $formats[$field->type];
if (str_contains($value, '.')) { // time possibly with microseconds, otherwise invalid format
$format = preg_replace('~(?<=H:i:s)(?![. ]*u)~', '.u', $format);
}
$format = ['date' => 'Y-m-d', 'datetime' => 'Y-m-d H:i:s', 'time' => 'H:i:s'][$field->type];
if (str_contains($value, '.')) { // time possibly with microseconds, otherwise invalid format
$format = preg_replace('~(?<=H:i:s)(?![. ]*u)~', '.u', $format);
}

if ($field->type === 'datetime') {
$value = \DateTime::createFromFormat('!' . $format, $value, new \DateTimeZone($field->persist_timezone));
$value = \DateTime::createFromFormat('!' . $format, $value, new \DateTimeZone('UTC'));
if ($value !== false) {
$value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
}
Expand Down
37 changes: 0 additions & 37 deletions tests/TypecastingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,43 +377,6 @@ public function testLoadBy(): void
$this->assertTrue($m2->isLoaded());
}

public function testTypecastTimezone(): void
{
$m = new Model($this->db, ['table' => 'event']);
$dt = $m->addField('dt', ['type' => 'datetime', 'persist_timezone' => 'EEST']);
$d = $m->addField('d', ['type' => 'date', 'persist_timezone' => 'EEST']);
$t = $m->addField('t', ['type' => 'time', 'persist_timezone' => 'EEST']);

date_default_timezone_set('UTC');
$s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
$this->assertSame('2005-08-16 00:52:01.000000', $this->db->typecastSaveField($dt, $s));
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $this->db->typecastLoadField($dt, '2005-08-16 00:52:01'));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));

date_default_timezone_set('Asia/Tokyo');

$s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
$this->assertSame('2005-08-16 00:52:01.000000', $this->db->typecastSaveField($dt, $s));
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $this->db->typecastLoadField($dt, '2005-08-16 00:52:01'));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));

date_default_timezone_set('America/Los_Angeles');

$s = new \DateTime('Monday, 15-Aug-05 22:52:01'); // uses servers default timezone
$this->assertSame('2005-08-16 07:52:01.000000', $this->db->typecastSaveField($dt, $s));
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 America/Los_Angeles'), $this->db->typecastLoadField($dt, '2005-08-16 07:52:01'));
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));
}

public function testTimestamp(): void
{
$sql_time = '2016-10-25 11:44:08';
Expand Down

0 comments on commit 6815ec1

Please sign in to comment.