date.devel_generate.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @file
  4. * Date Devel Generate code.
  5. */
  6. /**
  7. * Implements hook_devel_generate().
  8. *
  9. * Included only when needed.
  10. */
  11. function date_devel_generate($entity, $field, $instance, $bundle) {
  12. $entity_field = array();
  13. if (isset($instance['widget']['settings']['year_range'])) {
  14. $split = explode(':', $instance['widget']['settings']['year_range']);
  15. $back = str_replace('-', '', $split[0]);
  16. $forward = str_replace('+', '', $split[1]);
  17. }
  18. else {
  19. $back = 2;
  20. $forward = 2;
  21. }
  22. // Pick a random year within the time range,
  23. // and a random second within that year.
  24. $year = date_format(date_now(), 'Y') - $back + mt_rand(0, ($forward + $back));
  25. $start = new DateObject($year . '-01-01 00:00:00', date_get_timezone_db($field['settings']['tz_handling']));
  26. $leap = date_format($start, 'L');
  27. $max_days = $leap ? 366 : 365;
  28. $seconds = mt_rand(0, ($max_days * 86400));
  29. date_modify($start, "+$seconds seconds");
  30. $increment = $instance['widget']['settings']['increment'];
  31. date_increment_round($start, $increment);
  32. // Modify End date by 1 hour to 3 days, shorter for repeating dates
  33. // longer for others.
  34. $start2 = clone($start);
  35. $max = !empty($field['settings']['repeat']) ? 720 : 4320;
  36. $max = 240;
  37. date_modify($start2, '+' . mt_rand(60, $max) . ' minutes');
  38. date_increment_round($start2, $increment);
  39. if ($field['settings']['tz_handling'] == 'date') {
  40. // Choose a random timezone.
  41. // Not all keys exist, so we have to check.
  42. $timezones = array_keys(date_timezone_names(TRUE));
  43. $key = mt_rand(0, count($timezones) - 1);
  44. if (!array_key_exists($key, $timezones)) {
  45. $timezone = date_default_timezone();
  46. }
  47. else {
  48. $timezone = $timezones[$key];
  49. }
  50. }
  51. else {
  52. $timezone = date_get_timezone($field['settings']['tz_handling']);
  53. }
  54. switch ($field['type']) {
  55. case 'date':
  56. $format = DATE_FORMAT_ISO;
  57. break;
  58. case 'datestamp':
  59. $format = DATE_FORMAT_UNIX;
  60. break;
  61. case 'datetime':
  62. $format = DATE_FORMAT_DATETIME;
  63. break;
  64. }
  65. $entity_field['value'] = date_format($start, $format);
  66. if ($field['settings']['todate']) {
  67. $entity_field['value2'] = date_format($start2, $format);
  68. }
  69. date_timezone_set($start, timezone_open($timezone));
  70. $entity_field['timezone'] = $timezone;
  71. $entity_field['offset'] = date_offset_get($start);
  72. date_timezone_set($start2, timezone_open($timezone));
  73. $entity_field['offset2'] = date_offset_get($start2);
  74. return $entity_field;
  75. }