A utility class that brings togehter functions we end up using often across Bat to hand Event Intervals.
Ejemplo n.º 1
0
 public function testDivide2()
 {
     $start_date = new \DateTime('2016-02-11 12:00');
     $end_date = new \DateTime('2016-02-11 21:59');
     $duration = new \DateInterval('P1D');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 0.42);
     $duration = new \DateInterval('PT3H');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 3.33);
     $duration = new \DateInterval('PT2H');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 5);
     $duration = new \DateInterval('PT1H');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 10);
     $duration = new \DateInterval('PT30M');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 20);
     $duration = new \DateInterval('PT15M');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 40);
     $duration = new \DateInterval('PT10M');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 60);
     $duration = new \DateInterval('PT5M');
     $percentage = EventInterval::divide($start_date, $end_date, $duration);
     $this->assertEquals(round($percentage, 2), 120);
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function determineValue()
 {
     $value = 0;
     // Instantiate a calendar
     $calendar = new Calendar(array($this->unit), $this->store);
     $events = $calendar->getEvents($this->start_date, $this->end_date);
     foreach ($events as $unit => $unit_events) {
         if ($unit == $this->unit->getUnitId()) {
             foreach ($unit_events as $event) {
                 $percentage = EventInterval::divide($event->getStartDate(), $event->getEndDate(), $this->duration_unit);
                 $value = $value + $event->getValue() * $percentage;
             }
         }
     }
     return round($value, 2);
 }