Full support for Drupal time and date

meatbites - December 6, 2007 - 09:58
Project:Token
Version:5.x-1.10
Component:Code
Category:bug report
Priority:normal
Assigned:meatbites
Status:patch (code needs review)
Description

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.

#1

greggles - December 9, 2007 - 14:54
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.

#2

meatbites - January 5, 2008 - 06:20
Title:use site timezone to determine node creation time» Improved time and date support (timezones, etc.)
Status:active» patch (code needs review)

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.

AttachmentSize
token_format_date_support.patch3.71 KB

#3

christefanø - January 18, 2008 - 21:51

subscribing

#4

meatbites - January 19, 2008 - 02:03

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.

<?php
$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?

#5

meatbites - January 21, 2008 - 09:42

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.

<?php
// 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'));
}
?>

#6

meatbites - January 28, 2008 - 06:07
Title:Improved time and date support (timezones, etc.)» Full support for Drupal time and date
Assigned to:Anonymous» meatbites

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.

AttachmentSize
token_format_date_support_1.patch3.51 KB

#7

meatbites - February 1, 2008 - 10:34
Version:5.x-1.x-dev» 5.x-1.10

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.

AttachmentSize
token_format_date_support_5.x-1.10.patch3.43 KB
 
 

Drupal is a registered trademark of Dries Buytaert.