Skip to content

Commit 6815ec1

Browse files
committed
x
1 parent 23c3a8b commit 6815ec1

File tree

5 files changed

+8
-62
lines changed

5 files changed

+8
-62
lines changed

docs/fields.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Agile Data defines some basic types to make sure that values:
6767
A good example would be a `date` type::
6868

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

7272
$model->save();
7373

docs/persistence.rst

+1-3
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,8 @@ Dates and Time
349349

350350
.. todo:: this section might need cleanup
351351

352-
There are 4 date formats supported:
352+
There are 3 datetime formats supported:
353353

354-
- ts (or timestamp): Stores in database using UTC. Defaults into unix
355-
timestamp (int) in PHP.
356354
- date: Converts into YYYY-MM-DD using UTC timezone for SQL. Defaults
357355
to DateTime() class in PHP, but supports string input (parsed as date
358356
in a current timezone) or unix timestamp.

src/Model/FieldPropertiesTrait.php

-9
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,4 @@ trait FieldPropertiesTrait
106106
* @var bool|string
107107
*/
108108
public $required = false;
109-
110-
/**
111-
* Persisting timezone for type = 'date', 'datetime', 'time' fields.
112-
*
113-
* For example, 'IST', 'UTC', 'Europe/Riga' etc.
114-
*
115-
* @var string
116-
*/
117-
public $persist_timezone = 'UTC';
118109
}

src/Persistence.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,10 @@ protected function _typecastSaveField(Field $field, $value)
399399

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

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

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

448442
if ($field->type === 'datetime') {
449-
$value = \DateTime::createFromFormat('!' . $format, $value, new \DateTimeZone($field->persist_timezone));
443+
$value = \DateTime::createFromFormat('!' . $format, $value, new \DateTimeZone('UTC'));
450444
if ($value !== false) {
451445
$value->setTimezone(new \DateTimeZone(date_default_timezone_get()));
452446
}

tests/TypecastingTest.php

-37
Original file line numberDiff line numberDiff line change
@@ -377,43 +377,6 @@ public function testLoadBy(): void
377377
$this->assertTrue($m2->isLoaded());
378378
}
379379

380-
public function testTypecastTimezone(): void
381-
{
382-
$m = new Model($this->db, ['table' => 'event']);
383-
$dt = $m->addField('dt', ['type' => 'datetime', 'persist_timezone' => 'EEST']);
384-
$d = $m->addField('d', ['type' => 'date', 'persist_timezone' => 'EEST']);
385-
$t = $m->addField('t', ['type' => 'time', 'persist_timezone' => 'EEST']);
386-
387-
date_default_timezone_set('UTC');
388-
$s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
389-
$this->assertSame('2005-08-16 00:52:01.000000', $this->db->typecastSaveField($dt, $s));
390-
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
391-
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
392-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $this->db->typecastLoadField($dt, '2005-08-16 00:52:01'));
393-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
394-
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));
395-
396-
date_default_timezone_set('Asia/Tokyo');
397-
398-
$s = new \DateTime('Monday, 15-Aug-05 22:52:01 UTC');
399-
$this->assertSame('2005-08-16 00:52:01.000000', $this->db->typecastSaveField($dt, $s));
400-
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
401-
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
402-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 UTC'), $this->db->typecastLoadField($dt, '2005-08-16 00:52:01'));
403-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
404-
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));
405-
406-
date_default_timezone_set('America/Los_Angeles');
407-
408-
$s = new \DateTime('Monday, 15-Aug-05 22:52:01'); // uses servers default timezone
409-
$this->assertSame('2005-08-16 07:52:01.000000', $this->db->typecastSaveField($dt, $s));
410-
$this->assertSame('2005-08-15', $this->db->typecastSaveField($d, $s));
411-
$this->assertSame('22:52:01.000000', $this->db->typecastSaveField($t, $s));
412-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05 22:52:01 America/Los_Angeles'), $this->db->typecastLoadField($dt, '2005-08-16 07:52:01'));
413-
$this->assertEquals(new \DateTime('Monday, 15-Aug-05'), $this->db->typecastLoadField($d, '2005-08-15'));
414-
$this->assertEquals(new \DateTime('1970-01-01 22:52:01'), $this->db->typecastLoadField($t, '22:52:01'));
415-
}
416-
417380
public function testTimestamp(): void
418381
{
419382
$sql_time = '2016-10-25 11:44:08';

0 commit comments

Comments
 (0)