Skip to content

Commit

Permalink
Ensure floating point arguments do not yield microsecond fractions > …
Browse files Browse the repository at this point in the history
…6 digits
  • Loading branch information
thekid committed Aug 17, 2024
1 parent 754b663 commit 22c9231
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/php/util/Date.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ public function __construct($in= null, ?TimeZone $timezone= null) {
$this->handle= date_create('now', $timezone ? $timezone->getHandle() : null);
} else if ($in instanceof DateTime) {
$this->handle= $in;
} else if (is_int($in) || is_float($in) || (string)(int)$in === $in) {
} else if (is_int($in) || (string)(int)$in === $in) {

// Specially mark timestamps for parsing (we assume here that strings
// containing only digits are timestamps)
$this->handle= date_create('@'.$in);
$timezone && date_timezone_set($this->handle, $timezone->getHandle());
} else if (is_float($in)) {

// Timestamps with microseconds are defined as `"@" "-"? [0-9]+ "." [0-9]{0,6}`,
// see https://www.php.net/manual/en/datetime.formats.php#datetime.formats.relative
$this->handle= date_create('@'.sprintf('%.6f', $in);
$timezone && date_timezone_set($this->handle, $timezone->getHandle());
} else {
if (false === ($this->handle= date_create($in ?? 'now', $timezone ? $timezone->getHandle() : null))) {
throw new IllegalArgumentException('Given argument is neither a timestamp nor a well-formed timestring: '.Objects::stringOf($in));
Expand Down

0 comments on commit 22c9231

Please sign in to comment.