date.devel_generate.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Determine how much to go back and forward depending on whether a relative
  16. // number of years (with - or + sign) or an absolute year is given.
  17. $back = strpos($split[0], '-') === 0
  18. ? str_replace('-', '', $split[0])
  19. : date_format(date_now(), 'Y') - $split[0];
  20. $forward = strpos($split[1], '+') === 0
  21. ? str_replace('+', '', $split[1])
  22. : $split[1] - date_format(date_now(), 'Y');
  23. }
  24. else {
  25. $back = 2;
  26. $forward = 2;
  27. }
  28. // Pick a random year within the time range,
  29. // and a random second within that year.
  30. $year = date_format(date_now(), 'Y') - $back + mt_rand(0, ($forward + $back));
  31. $start = new DateObject($year . '-01-01 00:00:00', date_get_timezone_db($field['settings']['tz_handling']));
  32. $leap = date_format($start, 'L');
  33. $max_days = $leap ? 366 : 365;
  34. $seconds = mt_rand(0, ($max_days * 86400));
  35. date_modify($start, "+$seconds seconds");
  36. $increment = $instance['widget']['settings']['increment'];
  37. date_increment_round($start, $increment);
  38. // Modify End date by 1 hour to 3 days, shorter for repeating dates
  39. // longer for others.
  40. $start2 = clone($start);
  41. $max = !empty($field['settings']['repeat']) ? 720 : 4320;
  42. $max = 240;
  43. date_modify($start2, '+' . mt_rand(60, $max) . ' minutes');
  44. date_increment_round($start2, $increment);
  45. if ($field['settings']['tz_handling'] == 'date') {
  46. // Choose a random timezone.
  47. // Not all keys exist, so we have to check.
  48. $timezones = array_keys(date_timezone_names(TRUE));
  49. $key = mt_rand(0, count($timezones) - 1);
  50. if (!array_key_exists($key, $timezones)) {
  51. $timezone = date_default_timezone();
  52. }
  53. else {
  54. $timezone = $timezones[$key];
  55. }
  56. }
  57. else {
  58. $timezone = date_get_timezone($field['settings']['tz_handling']);
  59. }
  60. switch ($field['type']) {
  61. case 'date':
  62. $format = DATE_FORMAT_ISO;
  63. break;
  64. case 'datestamp':
  65. $format = DATE_FORMAT_UNIX;
  66. break;
  67. case 'datetime':
  68. $format = DATE_FORMAT_DATETIME;
  69. break;
  70. }
  71. $entity_field['value'] = date_format($start, $format);
  72. if ($field['settings']['todate']) {
  73. $entity_field['value2'] = date_format($start2, $format);
  74. }
  75. date_timezone_set($start, timezone_open($timezone));
  76. $entity_field['timezone'] = $timezone;
  77. $entity_field['offset'] = date_offset_get($start);
  78. date_timezone_set($start2, timezone_open($timezone));
  79. $entity_field['offset2'] = date_offset_get($start2);
  80. return $entity_field;
  81. }