la datetime est dans le timezone par défaut

This commit is contained in:
Jephté Clain 2025-06-24 04:13:09 +04:00
parent 2c7020f44d
commit b71e879823
3 changed files with 43 additions and 10 deletions

View File

@ -45,7 +45,7 @@ abstract class CapacitorStorage {
const GENLIC_DEFINITION = "varchar(80)"; const GENLIC_DEFINITION = "varchar(80)";
const GENLIB_DEFINITION = "varchar(255)"; const GENLIB_DEFINITION = "varchar(255)";
const GENTEXT_DEFINITION = "mediumtext"; const GENTEXT_DEFINITION = "mediumtext";
const GENBOOL_DEFINITION = "integer(1)"; const GENBOOL_DEFINITION = "integer(1) default 0";
const GENUUID_DEFINITION = "varchar(36)"; const GENUUID_DEFINITION = "varchar(36)";
protected static function gencol($def): string { protected static function gencol($def): string {

View File

@ -11,7 +11,7 @@ class PgsqlStorage extends CapacitorStorage {
const SERTS_DEFINITION = "timestamp"; const SERTS_DEFINITION = "timestamp";
const GENSERIAL_DEFINITION = "serial primary key"; const GENSERIAL_DEFINITION = "serial primary key";
const GENTEXT_DEFINITION = "text"; const GENTEXT_DEFINITION = "text";
const GENBOOL_DEFINITION = "boolean"; const GENBOOL_DEFINITION = "boolean default false";
const GENUUID_DEFINITION = "uuid"; const GENUUID_DEFINITION = "uuid";
function __construct($pgsql) { function __construct($pgsql) {

View File

@ -4,6 +4,7 @@ namespace nulib\php\time;
use DateTimeInterface; use DateTimeInterface;
use DateTimeZone; use DateTimeZone;
use InvalidArgumentException; use InvalidArgumentException;
use nulib\str;
/** /**
* Class DateTime: une date et une heure * Class DateTime: une date et une heure
@ -32,7 +33,7 @@ class DateTime extends \DateTime {
const DMY_PATTERN = '/^(\d+)\/(\d+)(?:\/(\d+))?$/'; const DMY_PATTERN = '/^(\d+)\/(\d+)(?:\/(\d+))?$/';
const YMD_PATTERN = '/^((?:\d{2})?\d{2})(\d{2})(\d{2})$/'; const YMD_PATTERN = '/^((?:\d{2})?\d{2})(\d{2})(\d{2})$/';
const DMYHIS_PATTERN = '/^(\d+)\/(\d+)(?:\/(\d+))? +(\d+)[h:.](\d+)(?:[:.](\d+))?$/'; const DMYHIS_PATTERN = '/^(\d+)\/(\d+)(?:\/(\d+))? +(\d+)[h:.](\d+)(?:[:.](\d+))?$/';
const YMDHISZ_PATTERN = '/^((?:\d{2})?\d{2})(\d{2})(\d{2})[tT](\d{2})(\d{2})(\d{2})?([zZ])?$/'; const YMDHISZ_PATTERN = '/^((?:\d{2})?\d{2})-?(\d{2})-?(\d{2})[tT](\d{2}):?(\d{2}):?(\d{2})?([zZ]|\+\d{2}:?\d{2})?$/';
protected static function get_value(array $datetime, ?string $key, ?string $k, ?int $index) { protected static function get_value(array $datetime, ?string $key, ?string $k, ?int $index) {
@ -206,16 +207,34 @@ class DateTime extends \DateTime {
return $year; return $year;
} }
function __construct($datetime="now", DateTimeZone $timezone=null) { static function fix_z(?string $Z): ?string {
$Z = strtoupper($Z);
str::del_prefix($Z, "+");
if (preg_match('/^\d{4}$/', $Z)) {
$Z = substr($Z, 0, 2).":".substr($Z, 2);
}
if ($Z === "Z" || $Z === "UTC" || $Z === "00:00") return "UTC";
return "GMT+$Z";
}
function __construct($datetime="now", DateTimeZone $timezone=null, ?bool $forceLocalTimezone=null) {
$forceLocalTimezone ??= $timezone === null;
if ($forceLocalTimezone) {
$setTimezone = $timezone;
$timezone = null;
}
$datetime ??= "now"; $datetime ??= "now";
if ($datetime instanceof \DateTimeInterface) { if ($datetime instanceof \DateTimeInterface) {
if ($timezone === null) $timezone = $datetime->getTimezone(); $timezone ??= $datetime->getTimezone();
parent::__construct(); parent::__construct();
$this->setTimestamp($datetime->getTimestamp()); $this->setTimestamp($datetime->getTimestamp());
$this->setTimezone($timezone); $this->setTimezone($timezone);
} elseif (is_int($datetime)) { } elseif (is_int($datetime)) {
parent::__construct("now", $timezone); parent::__construct("now", $timezone);
$this->setTimestamp($datetime); $this->setTimestamp($datetime);
} elseif (is_string($datetime)) { } elseif (is_string($datetime)) {
$Y = $H = $Z = null; $Y = $H = $Z = null;
if (preg_match(self::DMY_PATTERN, $datetime, $ms)) { if (preg_match(self::DMY_PATTERN, $datetime, $ms)) {
@ -253,22 +272,30 @@ class DateTime extends \DateTime {
if ($Y !== null) { if ($Y !== null) {
if ($H === null) $datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d); if ($H === null) $datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d);
else $datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H, $M, $S); else $datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H, $M, $S);
if ($Z !== null) $timezone = new DateTimeZone("UTC"); if ($Z !== null) $timezone = new DateTimeZone(self::fix_z($Z));
} }
parent::__construct($datetime, $timezone); parent::__construct($datetime, $timezone);
} elseif (is_array($datetime) && ($datetime = self::parse_array($datetime)) !== null) { } elseif (is_array($datetime) && ($datetime = self::parse_array($datetime)) !== null) {
[$Y, $m, $d, $H, $M, $S, $tz] = $datetime; $setTimezone = $timezone;
$timezone = null;
[$Y, $m, $d, $H, $M, $S, $Z] = $datetime;
if ($H === null && $M === null && $S === null) { if ($H === null && $M === null && $S === null) {
$datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d); $datetime = sprintf("%04d-%02d-%02d", $Y, $m, $d);
} else { } else {
$datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H ?? 0, $M ?? 0, $S ?? 0); $datetime = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y, $m, $d, $H ?? 0, $M ?? 0, $S ?? 0);
} }
if ($tz === "Z" || $tz === "z") $tz = "UTC"; if ($Z !== null) $timezone = new DateTimeZone(self::fix_z($Z));
$timezone = $tz !== null? new DateTimeZone($tz): null;
parent::__construct($datetime, $timezone); parent::__construct($datetime, $timezone);
} else { } else {
throw new InvalidArgumentException("datetime must be a string or an instance of DateTimeInterface"); throw new InvalidArgumentException("datetime must be a string or an instance of DateTimeInterface");
} }
if ($forceLocalTimezone) {
$setTimezone ??= new DateTimeZone(date_default_timezone_get());
$this->setTimezone($setTimezone);
}
} }
function diff($target, $absolute=false): DateInterval { function diff($target, $absolute=false): DateInterval {
@ -276,7 +303,13 @@ class DateTime extends \DateTime {
} }
function format($format=self::DEFAULT_FORMAT): string { function format($format=self::DEFAULT_FORMAT): string {
return \DateTime::format($format); if (array_key_exists($format, self::INT_FORMATS)) {
$format = self::INT_FORMATS[$format];
} elseif (array_key_exists($format, self::STRING_FORMATS)) {
$format = self::STRING_FORMATS[$format];
}
if (is_callable($format)) return $format($this);
else return \DateTime::format($format);
} }
/** /**