Skip to content

Commit

Permalink
feat: add DB config dateFormat to provide default date/time formats
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 7, 2024
1 parent 846466e commit 0a8f785
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/Config/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];

/**
Expand All @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -347,6 +348,19 @@ abstract class BaseConnection implements ConnectionInterface
*/
protected $queryClass = Query::class;

/**
* Default Date/Time formats
*
* @var array<string, string>
*/
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.
*/
Expand Down
10 changes: 10 additions & 0 deletions user_guide_src/source/database/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a8f785

Please sign in to comment.