You are here

function _exif_date_handler in Exif 6

Helper function to handle all date values from exif header. This is designed for the date_api and date modules, but is compatible if these aren't enabled.

Parameters

array $field: The field definition for the matcing exif date

string $exif_date: The date extracted from exif header.

Return value

array The field value array for delta = 0

1 call to _exif_date_handler()
exif_nodeapi in ./exif.module
implementation of hook_nodeapi

File

./exif.module, line 156
implementing the drupal api

Code

function _exif_date_handler($field, $exif_date) {
  if (!module_exists('date_api')) {

    // Don't bother doing anything if the webmaster doesn't ...
    return array(
      'value' => $exif_date,
    );
  }
  require_once drupal_get_path('module', 'date_api') . '/date_api_elements.inc';
  $date_datetime = date_convert_from_custom($exif_date, variable_get('date_format_EXIF', 'Y:m:d H:i:s'));
  if (!in_array($field['type'], array(
    'date',
    'datetime',
    'datestamp',
  ))) {

    // Field is not a date field type, so we return a ISO-8601 representation
    return array(
      'value' => date_convert($date_datetime, DATE_DATETIME, DATE_ISO),
    );
  }

  // Exif doesn't handles timezones, so we assume the exif date is in the
  // timezone configured for this date field.  This means the exif date needs
  // to be converted to UTC before it's stored.
  $timezone = date_get_timezone($field['tz_handling']);
  $date = date_make_date($date_datetime, $timezone, DATE_DATETIME, $field['granularity']);

  // Store date offset before converting to UTC as this is lost when setting
  // timezone to 'UTC'.
  $offset = date_offset_get($date);
  date_timezone_set($date, timezone_open('UTC'));

  // Finally, convert the date object in UTC to a date according to the field
  // type: DATE_ISO, DATE_DATETIME or DATE_UNIX.
  $date_field = date_convert($date, DATE_OBJECT, $field['type']);
  return array(
    'value' => $date_field,
    'value2' => $date_field,
    'timezone' => $timezone,
    'offset' => $offset,
    'offset2' => $offset,
  );
}