3 namespace Drupal\Core\TypedData\Plugin\DataType;
5 use Drupal\Core\TypedData\Type\DurationInterface;
8 * The time span data type represents durations as number of seconds.
10 * The plain value is the (integer) number of seconds. Note that time spans only
11 * map correctly to durations as long as the number of seconds does not exceed
12 * a day (there is already a difference in applying a duration of a day or 24
13 * hours due to daylight savings). If that's an issue, consider using
14 * \Drupal\Core\TypedData\Type\DurationIso8601 instead.
18 * label = @Translation("Time span in seconds")
21 * @see \Drupal\Core\TypedData\Type\DurationIso8601
23 class TimeSpan extends IntegerData implements DurationInterface {
28 public function getDuration() {
30 // Keep the duration in seconds as there is generally no valid way to
31 // convert it to days, months or years.
32 return new \DateInterval('PT' . $this->value . 'S');
39 public function setDuration(\DateInterval $duration, $notify = TRUE) {
40 // Note that this applies the assumption of 12 month's a 30 days and
41 // each year having 365 days. There is no accurate conversion for time spans
43 $this->value = ($duration->y * 365 * 24 * 60 * 60) +
44 ($duration->m * 30 * 24 * 60 * 60) +
45 ($duration->d * 24 * 60 * 60) +
46 ($duration->h * 60 * 60) +
50 // Notify the parent of any changes.
51 if ($notify && isset($this->parent)) {
52 $this->parent->onChange($this->name);