Welcome Guest, Not a member yet? Register   Sign In
Timestamp Inconsistency
#1
Exclamation 
(This post was last modified: 09-08-2020, 02:56 PM by Gary.)

Having recently experimented with time zones, I find there is an inconsistency in the way CI handles its timestamps (mostly used in $useSoftDeletes / $createdField functionality):

The code in Model.php that handles the timestamps is as follows:
Code:
protected function setDate(int $userData = null)
{
  $currentDate = is_numeric($userData) ? (int) $userData : time();

  switch ($this->dateFormat)
  {
    case 'int':
      return $currentDate;
    case 'datetime':
      return date('Y-m-d H:i:s', $currentDate);
    case 'date':
      return date('Y-m-d', $currentDate);

Case 'int' returns the time in UTC (GMT) whereas 'datetime' and 'date' both return the time in localized time... which makes it a horrible can of worms to manage in the application code if one has users in more than one time zone (or a user reconfigures their time zone).

I'd propose EITHER:

1) standardizing it (and conform to what some believe is 'best practice') to always NOT use zone'd timestamps (ie: making the same timestamp as 'int' be returned in all cases- easily done by using gmdate() in place of the two date() functions.

or

2) add two new (additional) cases, which would make it backward-compatible with what currently exists:
Code:
case 'datetime_utc':
      return gmdate('Y-m-d H:i:s', $currentDate);
case 'date_utc':
      return gmdate('Y-m-d', $currentDate);

Personally, I find 2) less desirable because it leans toward making what should be the 'standard' (and consistent) response a special case. I'd propose implementing 1) and then, if local time options were still desired, implement those as the special cases, perhaps with 'datetime_local' and 'date_local' options.
Reply


Messages In This Thread
Timestamp Inconsistency - by Gary - 09-08-2020, 05:34 AM
RE: Timestamp Inconsistency - by MGatner - 09-10-2020, 03:07 PM
RE: Timestamp Inconsistency - by Gary - 09-12-2020, 08:44 AM
RE: Timestamp Inconsistency - by tgix - 09-12-2020, 10:39 AM
RE: Timestamp Inconsistency - by Gary - 09-14-2020, 05:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB