Re: DateTime microseconds discussion

From: Date: Tue, 08 Nov 2016 19:23:41 +0000
Subject: Re: DateTime microseconds discussion
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-96777@lists.php.net to get a copy of this message
On 11/08/2016 01:46 PM, Derick Rethans wrote:
On Tue, 8 Nov 2016, Arjen Schol wrote:
Hi, Support for microseconds was added late in the 7.1 RC cycle, however is has some issues:
Some *additional* support for microseconds was added in the PHP 7.1 cycle, mostly to support bug fixes that have been around for a long time.
1. There is no easy way to set microseconds to 0, you have to call setTime see https://3v4l.org/YUhFF A setMicroseconds method would be handy and/or support to relative strings to set microseconds to 0 (just like midnight does for H:i:s). Or am I missing something?
- You can't set just the seconds or minute portion alone either, through setTime(). Microseconds are just an extension of the time portion. - Using "midnight" also sets the microseconds to 0:
new DateTime('now midnight') obviously won't work...
[PHP: 7.1.0-dev ] derick@whisky:~ $ cat /tmp/midnight.php <?php $a = new DateTimeImmutable(); var_dump( $a ); var_dump( $a->modify( 'midnight' ) ); ?> [PHP: 7.1.0-dev ] derick@whisky:~ $ php -n /tmp/midnight.php object(DateTimeImmutable)#1 (3) { ["date"]=> string(26) "2016-11-08 12:01:20.023680" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } object(DateTimeImmutable)#2 (3) { ["date"]=> string(26) "2016-11-08 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
2. Microsecond support is useful, by not by default I think. Why not introduce a 3rd parameter $with_microseconds/$set_microseconds which defaults to false?
A 3rd parameter to which function?
With 3rd argument I meant the constructor: public DateTime::__construct ([ string $time = "now" [, DateTimeZone $timezone = NULL, [ $initialize_microseconds = false ]]] ) When microseconds are NOT specified in $time, don't set them unless $initialize_microseconds is true.
Or A second class DateTimeWithMicroseconds? Maybe not so fancy naming, but it's very clear what will happen.
But that would be a duplicate. The already existing DateTime and DateTimeImmutable classes already support microseconds. Adding an extra class for them, would just be confusing.
3. The 4th parameter to setTime() should not cause BC breakage.
Added additional new arguments with a default value, is not a BC break. cheers, Derick


« previous php.internals (#96777) next »