I've noticed Pathauto annoyingly uses the server time to generate a creation date/time -- as opposed to going through Drupal's own time zone filter which uses the /admin/settings/date-time setting.

If I get the time to look at it, I will, but unfortunately that won't be for a while, so I thought I'd file a bug report to at least make you aware of the issue, as it sure would be a helpful fix.

Apologies if this is a duplicate.

Comments

greggles’s picture

Title: Displaying incorrect creation time » use site timezone to determine node creation time
Project: Pathauto » Token
Version: 5.x-2.0 » 5.x-1.x-dev
Component: User interface » Code

This is a bug against token module probable. token_node.inc would be the file to look at.

Questions: should the token use the site time zone? The time zone of the user creating the node? The time zone of the user editing the node?

I agree that "server time" is wrong, but I'm not sure there is an easy answer on which is right. Perhaps the api for making timezone adjusted dates takes a lot of this into account already. I haven't looked.

meatbites’s picture

Title: use site timezone to determine node creation time » Improved time and date support (timezones, etc.)
Status: Active » Needs review
StatusFileSize
new3.71 KB

With this patch, Token now takes advantage of Drupal's format_date. This addresses both the lack of timezone support and any other time and date settings Drupal may have both now and in future.

Toggling user-configurable timezones is indeed an option already built into Drupal. A further option specific to Token could probably be added later, but I think that's a feature outside of this issue as the functionality is already there.

Christefano-oldaccount’s picture

subscribing

meatbites’s picture

I just had a quick play. The following might pave way to a better implementation, as I'm not sure the multiple calls to format_date is entirely efficient. I've not yet tested this under token.

$date_types_index = array(
  'yyyy'  => 'Y',
  'yy'    => 'y',
  'month' => 'F',
  'mon'   => 'M',
  'mm'    => 'm',
  'm'     => 'n',
  'ww'    => 'W',
  'date'  => 'N',
  'day'   => 'l',
  'ddd'   => 'D',
  'dd'    => 'd',
  'd'     => 'j'
);

// Token date formats mapped to their respective formatted date values (i.e.: 'yyyy' => '2008').
$date_types = array_combine(
  array_keys($date_types_index),
  explode(',', format_date($node_created, 'custom', implode(',', $date_types_index)))
);

foreach ($date_types as $key => $value) {
  $values[$key] = $value;
}

Thoughts?

meatbites’s picture

Another quick fiddle -- this would be close to the final version, I'd think. I'll eventually get to properly testing this and putting it into a patch, unless someone beats me to it.


// Returns an array of date format keys mapped to their respective drupal-formatted date values.
function _token_get_date_values($date, $date_type = 'created') {
  // Node changed date.
  if ($date_type == 'changed') {
    $date_types_index = array(
      'mod-yyyy'  => 'Y',
      'mod-yy'    => 'y',
      'mod-month' => 'F',
      'mod-mon'   => 'M',
      'mod-mm'    => 'm',
      'mod-m'     => 'n',
      'mod-ww'    => 'W',
      'mod-date'  => 'N',
      'mod-day'   => 'l',
      'mod-ddd'   => 'D',
      'mod-dd'    => 'd',
      'mod-d'     => 'j'
    );
  }
  // Node created date (default).
  else {
    $date_types_index = array(
      'yyyy'  => 'Y',
      'yy'    => 'y',
      'month' => 'F',
      'mon'   => 'M',
      'mm'    => 'm',
      'm'     => 'n',
      'ww'    => 'W',
      'date'  => 'N',
      'day'   => 'l',
      'ddd'   => 'D',
      'dd'    => 'd',
      'd'     => 'j'
    );
  }
  return array_combine(
    array_keys($date_types_index),
    explode(',', format_date($date, 'custom', implode(',', $date_types_index)))
  );
}

if (isset($node->created)) {
  $values = array_merge($values, _token_get_date_values($node->created, 'created'));
}

if (isset($node->changed)) {
  $values = array_merge($values, _token_get_date_values($node->changed, 'changed'));
}

meatbites’s picture

Title: Improved time and date support (timezones, etc.) » Full support for Drupal time and date
Assigned: Unassigned » meatbites
StatusFileSize
new3.51 KB

See attached patch for the final working version of my above code. It's slightly faster than calling drupal_format multiple times.

Note: I added back the conditionals originally inserted here that leave the dates empty if they're not needed. These seemed to mysteriously disappear from HEAD -- was this intentional? If so, I'll rework a patch without the function.

meatbites’s picture

Version: 5.x-1.x-dev » 5.x-1.10
StatusFileSize
new3.43 KB

Re-rolled the patch to suit Token 5.x-1.10.

Additional:
- Added (int) cast to incoming date (as per this version of Token).
- Minor whitespace fix.

dave reid’s picture

Status: Needs review » Closed (duplicate)

Better patch for the current version in #307520: Date formatting function so I'm marking this as a duplicate of that issue.