/** * Test Converting Methods * */ function test_converting() { // Converting $timespan = Timespan::startingDuration(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)), Duration::withDays(10)); // asDate() $temp = $timespan->asDate(); $this->assertTrue($temp->isEqualTo(Date::withYearMonthDay(2005, 5, 4))); // asDateAndTime() $temp = $timespan->asDateAndTime(); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)))); // asDuration() $temp = $timespan->asDuration(); $this->assertTrue($temp->isEqualTo(Duration::withDays(10))); // asMonth() $temp = $timespan->asMonth(); $this->assertTrue($temp->isEqualTo(Month::withMonthYear(5, 2005))); // asTime() $temp = $timespan->asTime(); $dateAndTime = DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)); $this->assertTrue($temp->isEqualTo($dateAndTime->asTime())); // asTimeStamp() $temp = $timespan->asTimeStamp(); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)))); // asWeek() $temp = $timespan->asWeek(); $this->assertTrue($temp->isEqualTo(Week::starting(Date::withYearMonthDay(2005, 5, 4)))); // asYear() $temp = $timespan->asYear(); $this->assertTrue($temp->isEqualTo(Year::starting(Date::withYearMonthDay(2005, 5, 4)))); // to() $temp = $timespan->to(Date::withYearMonthDay(2005, 10, 1)); $comparison = Timespan::startingEnding(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)), Date::withYearMonthDay(2005, 10, 1)); $this->assertTrue($temp->isEqualTo($comparison)); }
/** * Test aritmatic operations * */ function test_intersect_union() { // intersection() // union() // 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 ... 0 0 0 0 // 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ... 1 2 3 4 // // A |- - - - - - - - - -| // B |-| // C |- - - - - - - - -| // D ...|- - - -| $timespanA = Year::startingDuration(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 4, 15, 25, 10, Duration::withHours(-4)), Duration::withDays(10)); $timespanB = Year::startingDuration(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 10, 12, 0, 0, Duration::withHours(-4)), Duration::withDays(1)); $timespanC = Year::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 20), DateAndTime::withYearMonthDay(2005, 5, 29)); $timespanD = Year::startingEnding(DateAndTime::withYearMonthDay(2006, 6, 1), DateAndTime::withYearMonthDay(2005, 6, 6)); // intersection() $this->assertTrue($timespanB->isEqualTo($timespanA->intersection($timespanB))); $this->assertTrue($timespanC->isEqualTo($timespanA->intersection($timespanC))); $this->assertEqual($timespanA->intersection($timespanD), NULL); // union() $temp = Timespan::startingDuration(DateAndTime::withYearMonthDay(2005, 5, 4), Duration::withDays(371)); $union = $timespanA->union($timespanB); $this->assertTrue($temp->isEqualTo($union)); $unionDuration = $union->duration(); $this->assertEqual($unionDuration->days(), 371); $temp = Timespan::startingDuration(DateAndTime::withYearMonthDay(2005, 5, 4), Duration::withDays(381)); $this->assertTrue($temp->isEqualTo($timespanA->union($timespanC))); $temp = Timespan::startingDuration(DateAndTime::withYearMonthDay(2005, 5, 4), Duration::withDays(758)); $this->assertTrue($temp->isEqualTo($timespanA->union($timespanD))); }
/** * Return a Timespan where the receiver is the middle of the Duration * * @param object Duration $aDuration * @return object Timespan * @access public * @since 5/12/05 */ function middleOf($aDuration) { $duration = $aDuration->asDuration(); $obj = Timespan::startingDuration($this->minus($duration->dividedBy(2)), $duration); return $obj; }
/** * Test converting methods * */ function test_converting() { $time = Time::withHourMinuteSecond(15, 25, 10); // asDate () $temp = $time->asDate(); $this->assertTrue($temp->isEqualTo(Date::today())); $this->assertEqual(strtolower(get_class($temp)), 'date'); // asDateAndTime () $temp = $time->asDateAndTime(); $comparison = DateAndTime::midnight(); $comparison = $comparison->plus(Duration::withSeconds(55510)); $this->assertTrue($temp->isEqualTo($comparison)); $this->assertEqual(strtolower(get_class($temp)), 'dateandtime'); // asDuration () $temp = $time->asDuration(); $this->assertTrue($temp->isEqualTo(Duration::withSeconds(55510))); $this->assertEqual(strtolower(get_class($temp)), 'duration'); // asMonth () $temp = $time->asMonth(); $this->assertTrue($temp->isEqualTo(Month::starting(Date::today()))); $this->assertEqual(strtolower(get_class($temp)), 'month'); // asSeconds () $this->assertEqual($time->asSeconds(), 55510); // asTime () $temp = $time->asTime(); $this->assertTrue($temp->isEqualTo($time)); $this->assertEqual(strtolower(get_class($temp)), 'time'); // asTimeStamp () $temp = $time->asTimeStamp(); $comparison = TimeStamp::midnight(); $comparison = $comparison->plus(Duration::withSeconds(55510)); $this->assertTrue($temp->isEqualTo($comparison)); $this->assertEqual(strtolower(get_class($temp)), 'timestamp'); // asWeek () $temp = $time->asWeek(); $this->assertTrue($temp->isEqualTo(Week::starting(Date::today()))); $this->assertEqual(strtolower(get_class($temp)), 'week'); // asYear () $temp = $time->asYear(); $this->assertTrue($temp->isEqualTo(Year::starting(Date::today()))); $this->assertEqual(strtolower(get_class($temp)), 'year'); // to () $today = DateAndTime::today(); $tomorrow = DateAndTime::tomorrow(); $result = $time->to($tomorrow); $this->assertEqual(strtolower(get_class($result)), 'timespan'); $this->assertTrue($result->isEqualTo(Timespan::startingDuration($today->plus(Duration::withSeconds(55510)), Duration::withDaysHoursMinutesSeconds(0, 8, 34, 50)))); $result = $time->to(Time::withHourMinuteSecond(23, 25, 10)); $this->assertEqual(strtolower(get_class($result)), 'timespan'); $this->assertTrue($result->isEqualTo(Timespan::startingDuration($today->plus(Duration::withSeconds(55510)), Duration::withDaysHoursMinutesSeconds(0, 8, 0, 0)))); }
/** * Create a new object starting now, with a given duration. * Override - as each Week has a defined duration * * @param object DateAndTime $aDateAndTime * @param object Duration $aDuration * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE. * This parameter is used to get around the limitations of not being * able to find the class of the object that recieved the initial * method call. * @return object Week * @access public * @since 5/5/05 * @static */ static function startingDuration($aDateAndTime, $aDuration, $class = 'Week') { // Validate our passed class name. if (!(strtolower($class) == strtolower('Week') || is_subclass_of(new $class(), 'Week'))) { die("Class, '{$class}', is not a subclass of 'Week'."); } $asDateAndTime = $aDateAndTime->asDateAndTime(); $midnight = $asDateAndTime->atMidnight(); $dayNames = ChronologyConstants::DayNames(); $temp = $midnight->dayOfWeek() + 7 - array_search(Week::startDay(), $dayNames); $delta = abs($temp - intval($temp / 7) * 7); $adjusted = $midnight->minus(Duration::withDays($delta)); $obj = parent::startingDuration($adjusted, Duration::withWeeks(1), $class); return $obj; }
/** * Create a new object starting now, with a given duration. * * @param object DateAndTime $aDateAndTime * @param object Duration $aDuration * @param optional string $class DO NOT USE OUTSIDE OF PACKAGE. * This parameter is used to get around the limitations of not being * able to find the class of the object that recieved the initial * method call. * @return object Schedule * @access public * @since 5/5/05 * @static */ static function startingDuration($aDateAndTime, $aDuration, $class = 'Schedule') { $obj = parent::startingDuration($aDateAndTime, $aDuration, $class); return $obj; }
/** * Test aritmatic operations * */ function test_intersect_union() { // intersection() // union() // 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 // 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 // // A |- - - - - - - - - -| // B |-| // C |- - - - - - - - -| // D |- - - -| $timespanA = Date::startingDuration(DateAndTime::withYearMonthDay(2005, 5, 4), Duration::withDays(10)); $timespanB = Date::startingDuration(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 10, 12, 0, 0, Duration::withHours(-4)), Duration::withDays(1)); $timespanC = Date::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 8), DateAndTime::withYearMonthDay(2005, 5, 17)); $timespanD = Date::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 17), DateAndTime::withYearMonthDay(2005, 5, 21)); // intersection() $duration = Duration::withDays(1); $temp = Timespan::startingDuration(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 5, 10, 12, 0, 0, Duration::withHours(-4)), $duration->minus(DateAndTime::clockPrecision())); $result = $timespanA->intersection($timespanB); $this->assertTrue($temp->isEqualTo($result)); $tempEnd = DateAndTime::withYearMonthDay(2005, 5, 14); $temp = Timespan::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 8), $tempEnd->minus(DateAndTime::clockPrecision())); $this->assertTrue($temp->isEqualTo($timespanA->intersection($timespanC))); $this->assertEqual($timespanA->intersection($timespanD), NULL); // union() $this->assertTrue($timespanA->isEqualTo($timespanA->union($timespanB))); $temp = Timespan::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 4), DateAndTime::withYearMonthDay(2005, 5, 17)); $this->assertTrue($temp->isEqualTo($timespanA->union($timespanC))); $temp = Timespan::startingEnding(DateAndTime::withYearMonthDay(2005, 5, 4), DateAndTime::withYearMonthDay(2005, 5, 21)); $this->assertTrue($temp->isEqualTo($timespanA->union($timespanD))); }