Posted by moshe weitzman on December 28, 2006 at 5:29am
3 followers
Jump to:
| Project: | Date |
| Version: | 7.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (fixed) |
Issue Summary
is there some example lying around that will help convert from event.module timestamp+timezone schema to date fields? can anyone suggest an approach?
Comments
#1
Let me think about this. It should be possible using the date functions in date.inc.
#2
OK, not sure if this is what you mean, but here is a bit of code to calculate the parts of an event date and get them into formats needed by the date module. I haven't actually tested this, so hopefully I didn't make any typos :-)
<?php
include_once(drupal_get_path('module', 'date') .'/date.inc');
include_once(DATE_TIMEZONES);
// starting values
$timestamp = time(); // event timestamp
$zid = 477; // event timezone id
// calculate the offset for this date
$offset = event_get_offset($zid, $timestamp);
// get the timezone name for this date
$timezones = event_get_timezones();
$timezone = $timezones[$zid]['timezone'];
// calculate the ISO value for this date
$date = date_make_date();
date_set_date($date, $timestamp, $timezone, 'local', DATE_UNIX);
date_show_value($date, 'db', DATE_ISO);
?>
#3
Or actually, the ISO value for the db value of the date could even just be this, since the timestamp is stored as GMT:
<?php// calculate the ISO value for this date
$date = date_make_date();
date_set_date($date, $timestamp, 'GMT', 'db', DATE_UNIX);
date_show_value($date, 'db', DATE_ISO);
?>
#4
I imagine you're trying to create an import script. I should play with this some more and try to come up with a more complete approach to take an event value out of the database and insert it into a date field. But anyway in the meantime maybe these will help.
#5
This is completely untested and is just the beginning of a solution, but something like this might work:
<?php
include_once(drupal_get_path('module', 'date') .'/date.inc');
include_once(DATE_TIMEZONES);
$type_name = 'content_event'; // the content type to import these values into.
$field_name = 'field_date'; // the field to import the start time into.
$field_name2 = 'field_date2'; // the field to import the end time into
$field_description = 'field_description'; // the field to import the description into
// Get info about the field we are importing into
$field = content_fields($field_name);
// Get date tz handling, could be date, site, GMT, or none.
$tz_handling = $field['tz_handling'];
// Get event tz handling, could be event, site, or user.
$event_tz_handling = variable_get('event_timezone_display', 'event');
$timezones = event_get_timezones();
$result = db_query("SELECT * FROM {event} ORDER BY event_start");
while ($event = db_fetch_object($result)) {
$node = node_load($event->nid);
$node->nid = 0;
$node->type = $type_name;
$node->$description[0]['value'] = $node->body;
unset($node->body);
$timestamp = $event->event_start;
$timestamp2 = $event->event_end;
if ($field['type'] == 'date') {
$node->$field_name[0]['value'] = date_unix2iso($timestamp);
$node->$field_name2[0]['value'] = date_unix2iso($timestamp2);
}
else {
$node->$field_name[0]['value'] = $timestamp;
$node->$field_name2[0]['value'] = $timestamp2;
}
if ($tz_handling == 'date' && $event_tz_handling == 'event') {
$node->$field_name[0]['timezone'] = $timezones[$event->timezone];
$node->$field_name[0]['offset'] = event_get_offset($event->timezone, $timestamp);
$node->$field_name2[0]['timezone'] = $timezones[$event->timezone2];
$node->$field_name2[0]['offset'] = event_get_offset($event->timezone, $timestamp2);
}
node_save($node);
}
?>
#6
Already saw a problem event without trying it. Should be:
<?php
include_once(drupal_get_path('module', 'date') .'/date.inc');
include_once(DATE_TIMEZONES);
$type_name = 'content_event'; // the content type to import these values into.
$field_name = 'field_date'; // the field to import the start time into.
$field_name2 = 'field_date2'; // the field to import the end time into
$field_description = 'field_description'; // the field to import the description into
// Get info about the field we are importing into
$field = content_fields($field_name);
// Get date tz handling, could be date, site, GMT, or none.
$tz_handling = $field['tz_handling'];
// Get event tz handling, could be event, site, or user.
$event_tz_handling = variable_get('event_timezone_display', 'event');
$timezones = event_get_timezones();
$result = db_query("SELECT * FROM {event} ORDER BY event_start");
while ($event = db_fetch_object($result)) {
$node = node_load($event->nid);
$node->nid = 0;
$node->type = $type_name;
$node->$description[0]['value'] = $node->body;
unset($node->body);
$timestamp = $event->event_start;
$timestamp2 = $event->event_end;
if ($field['type'] == 'date') {
$node->$field_name[0]['value'] = date_unix2iso($timestamp);
$node->$field_name2[0]['value'] = date_unix2iso($timestamp2);
}
else {
$node->$field_name[0]['value'] = $timestamp;
$node->$field_name2[0]['value'] = $timestamp2;
}
if ($tz_handling == 'date' && $event_tz_handling == 'event') {
$node->$field_name[0]['timezone'] = $timezones[$event->timezone]['timezone'];
$node->$field_name[0]['offset'] = event_get_offset($event->timezone, $timestamp);
$node->$field_name2[0]['timezone'] = $timezones[$event->timezone2]['timezone'];
$node->$field_name2[0]['offset'] = event_get_offset($event->timezone, $timestamp2);
}
node_save($node);
}
?>
#7
I just committed a very experimental module to the HEAD version of the Date code. It's called Date Copy and it allows you to import date information from either Event nodes or from an iCal feed. This is not production ready and needs testing, but is available for anyone who wants to try it out.
#8
Import for Events and iCal now seems to be working fairly well in the 5.x version (as an optional included module called Date Copy), so it is now available in both HEAD and 5.x. If someone wants it backported and creates a patch to do it, I'll commit the patch, but otherwise have no plans to backport this to 4.7 because is uses some 5.x FAPI features.
#9
#10
Any port for drupal 6?