Backup of db before drupal security update
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / Plugin / DataType / TimeSpan.php
1 <?php
2
3 namespace Drupal\Core\TypedData\Plugin\DataType;
4
5 use Drupal\Core\TypedData\Type\DurationInterface;
6
7 /**
8  * The time span data type represents durations as number of seconds.
9  *
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.
15  *
16  * @DataType(
17  *   id = "timespan",
18  *   label = @Translation("Time span in seconds")
19  * )
20  *
21  * @see \Drupal\Core\TypedData\Type\DurationIso8601
22  */
23 class TimeSpan extends IntegerData implements DurationInterface {
24
25   /**
26    * {@inheritdoc}
27    */
28   public function getDuration() {
29     if ($this->value) {
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');
33     }
34   }
35
36   /**
37    * {@inheritdoc}
38    */
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
42     // exceeding a day.
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) +
47       ($duration->i * 60) +
48        $duration->s;
49
50     // Notify the parent of any changes.
51     if ($notify && isset($this->parent)) {
52       $this->parent->onChange($this->name);
53     }
54   }
55
56 }