From 04276e42e4b14c1bff8a8efd7422498781b02a42 Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Wed, 24 Aug 2016 22:10:45 +0200 Subject: [PATCH] from() perserves micro-seconds --- lib/DateTime/Shared.php | 2 +- tests/AbstractDateTimeTest.php | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/DateTime/Shared.php b/lib/DateTime/Shared.php index 213afa9..4b63e53 100644 --- a/lib/DateTime/Shared.php +++ b/lib/DateTime/Shared.php @@ -99,7 +99,7 @@ static public function from($source, $timezone = null) if ($source instanceof \DateTimeInterface) { - return new static($source->format(self::DB), $source->getTimezone()); + return new static($source->format('Y-m-d H:i:s.u'), $source->getTimezone()); } return new static($source, $timezone); diff --git a/tests/AbstractDateTimeTest.php b/tests/AbstractDateTimeTest.php index e0afb51..c2f0e64 100644 --- a/tests/AbstractDateTimeTest.php +++ b/tests/AbstractDateTimeTest.php @@ -118,6 +118,10 @@ public function test_from() $d = $this->from('2001-01-01 01:01:01'); $this->assertEquals(date_default_timezone_get(), $d->tz->name); $this->assertEquals('2001-01-01 01:01:01', $d->as_db); + + $r = new DateTime('2001-01-01 01:01:01.012345', 'Asia/Tokyo'); + $d = $this->from($r); + $this->assertSame('2001-01-01 01:01:01.012345', $d->format('Y-m-d H:i:s.u')); } public function test_change()