From 0a8f785dd35ca62c42121d8ac17ba9044b57e9e6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 6 Feb 2024 14:17:15 +0900 Subject: [PATCH] feat: add DB config `dateFormat` to provide default date/time formats --- app/Config/Database.php | 10 ++++++++++ system/Database/BaseConnection.php | 14 ++++++++++++++ user_guide_src/source/database/configuration.rst | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/app/Config/Database.php b/app/Config/Database.php index 4e4aac7c0449..1ccac955dbcb 100644 --- a/app/Config/Database.php +++ b/app/Config/Database.php @@ -43,6 +43,11 @@ class Database extends Config 'failover' => [], 'port' => 3306, 'numberNative' => false, + 'dateFormat' => [ + 'date' => 'Y-m-d', + 'datetime' => 'Y-m-d H:i:s', + 'time' => 'H:i:s', + ], ]; /** @@ -69,6 +74,11 @@ class Database extends Config 'port' => 3306, 'foreignKeys' => true, 'busyTimeout' => 1000, + 'dateFormat' => [ + 'date' => 'Y-m-d', + 'datetime' => 'Y-m-d H:i:s', + 'time' => 'H:i:s', + ], ]; public function __construct() diff --git a/system/Database/BaseConnection.php b/system/Database/BaseConnection.php index d4d9032d7a8e..13398ed0ab11 100644 --- a/system/Database/BaseConnection.php +++ b/system/Database/BaseConnection.php @@ -26,6 +26,7 @@ * @property float $connectDuration * @property float $connectTime * @property string $database + * @property array $dateFormat * @property string $DBCollat * @property bool $DBDebug * @property string $DBDriver @@ -347,6 +348,19 @@ abstract class BaseConnection implements ConnectionInterface */ protected $queryClass = Query::class; + /** + * Default Date/Time formats + * + * @var array + */ + protected array $dateFormat = [ + 'date' => 'Y-m-d', + 'datetime' => 'Y-m-d H:i:s', + 'datetime-ms' => 'Y-m-d H:i:s.v', + 'datetime-us' => 'Y-m-d H:i:s.u', + 'time' => 'H:i:s', + ]; + /** * Saves our connection settings. */ diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index 128e28d9eeef..f555f3a48967 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -184,8 +184,18 @@ Explanation of Values: To enforce Foreign Key constraint, set this config item to true. **busyTimeout** milliseconds (int) - Sleeps for a specified amount of time when a table is locked (``SQLite3`` only). **numberNative** true/false (boolean) - Whether or not to enable MYSQLI_OPT_INT_AND_FLOAT_NATIVE (``MySQLi`` only). +**dateFormat** The default date/time formats as PHP's `DateTime format`_. + * ``date`` - date format + * ``datetime`` - date and time format + * ``datetime-ms`` - date and time with millisecond format + * ``datetime-us`` - date and time with microsecond format + * ``time`` - time format + This can be used since v4.5.0, and you can get the value, e.g., ``$db->dateFormat[datetime]``. + Currently, the database drivers do not use these values directly, but just provide the values. ================ =========================================================================================================== +.. _DateTime format: https://www.php.net/manual/en/datetime.format.php + .. note:: Depending on what database driver you are using (``MySQLi``, ``Postgre``, etc.) not all values will be needed. For example, when using ``SQLite3`` you will not need to supply a username or password, and the database name