/** * Create a new object starting now, with a given duration. * Override - as each month 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 Month * @access public * @since 5/5/05 * @static */ static function startingDuration($aDateAndTime, $aDuration, $class = 'Month') { // Validate our passed class name. if (!(strtolower($class) == strtolower('Month') || is_subclass_of(new $class(), 'Month'))) { die("Class, '{$class}', is not a subclass of 'Month'."); } $start = $aDateAndTime->asDateAndTime(); $adjusted = DateAndTime::withYearMonthDay($start->year(), $start->month(), 1); $days = Month::daysInMonthForYear($adjusted->month(), $adjusted->year()); $month = new $class(); $month->setStart($adjusted); $month->setDuration(Duration::withDays($days)); return $month; }
/** * Create a new object starting from midnight * * @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 Year * @access public * @since 5/5/05 * @static */ static function startingDuration($aDateAndTime, $aDuration, $class = 'Year') { // Validate our passed class name. if (!(strtolower($class) == strtolower('Year') || is_subclass_of(new $class(), 'Year'))) { die("Class, '{$class}', is not a subclass of 'Year'."); } $asDateAndTime = $aDateAndTime->asDateAndTime(); $midnight = $asDateAndTime->atMidnight(); $year = new $class(); $year->setStart($midnight); $year->setDuration(Duration::withDays(Year::getDaysInYear($midnight->year()))); return $year; }
/** * Test Converting Methods * */ function test_converting() { // Converting $timespan = Year::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::withYearMonthDayHourMinuteSecond(2005, 5, 4, 00, 00, 00))); // asDuration() $temp = $timespan->asDuration(); $this->assertTrue($temp->isEqualTo(Duration::withDays(365))); // asMonth() $temp = $timespan->asMonth(); $this->assertTrue($temp->isEqualTo(Month::withMonthYear(5, 2005))); // asTime() $temp = $timespan->asTime(); $dateAndTime = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 4, 0, 0, 0); $this->assertTrue($temp->isEqualTo($dateAndTime->asTime())); // asTimeStamp() $temp = $timespan->asTimeStamp(); $this->assertTrue($temp->isEqualTo(TimeStamp::withYearMonthDayHourMinuteSecond(2005, 5, 4, 0, 0, 0))); // asWeek() $temp = $timespan->asWeek(); $this->assertTrue($temp->isEqualTo(Week::starting(Date::withYearMonthDay(2005, 5, 1)))); // 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::withYearMonthDayHourMinuteSecond(2005, 5, 4, 0, 0, 0), Date::withYearMonthDay(2005, 10, 1)); $this->assertTrue($temp->isEqualTo($comparison)); }
/** * Answer the number of seconds the receiver represents. * * @return integer * @access public * @since 5/3/05 */ function seconds() { // Above 2^31 seconds, (amost exactly 100 years), PHP converts the // variable from an integer to a float to allow it to grow larger. // While addition and subraction work fine with floats, float modulos // and divisions loose precision. This precision loss does not affect // the proper value of days up to the maximum duration tested, 50billion // years. if (abs($this->seconds) > pow(2, 31)) { $remainderDuration = $this->minus(Duration::withDays($this->days())); return $remainderDuration->seconds(); } else { if ($this->isPositive()) { return floor($this->seconds % ChronologyConstants::SecondsInMinute()); } else { return 0 - floor(abs($this->seconds) % ChronologyConstants::SecondsInMinute()); } } }
/** * Test some leap years. * */ function test_end() { $datA = DateAndTime::withYearDay(2005, 125); $datB = DateAndTime::withYearDay(2006, 125); $timespan = Schedule::startingDuration(DateAndTime::withYearDay(2005, 125), Duration::withDays(365)); $this->assertEqual($timespan->startYear(), 2005); $this->assertEqual($timespan->dayOfYear(), 125); $duration = $timespan->duration(); $this->assertTrue($duration->isEqualTo(Duration::withDays(365))); $end = $timespan->end(); $this->assertEqual($end->julianDayNumber(), 2453860); $this->assertEqual($end->julianDayNumber() - $datA->julianDayNumber(), 364); $this->assertEqual($end->year(), 2006); $this->assertEqual($end->dayOfYear(), 124); $this->assertTrue($end->isEqualTo(DateAndTime::withYearDayHourMinuteSecond(2006, 124, 23, 59, 59))); }
/** * Create a new instance. * * @param integer $anIntYear * @param integer $anIntDayOfYear * @param integer $anIntHour * @param integer $anIntMinute * @param integer $anIntSecond * @param object Duration $aDurationOffset * @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 DateAndTime * @access public * @static * @since 5/4/05 */ static function withYearDayHourMinuteSecondOffset($anIntYear, $anIntDayOfYear, $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class = 'DateAndTime') { eval('$result = ' . $class . '::withYearMonthDayHourMinuteSecondOffset( $anIntYear, 1, 1, $anIntHour, $anIntMinute, $anIntSecond, $aDurationOffset, $class );'); if ($anIntDayOfYear <= 1) { $day = Duration::withDays(0); } else { $day = Duration::withDays($anIntDayOfYear - 1); } $obj = $result->plus($day); return $obj; }
/** * Magnitude operations * */ function test_magnitude_ops() { // Plus a Duration $dateAndTime = DateAndTime::withYearDayHourMinuteSecond(2005, 100, 0, 0, 0); $result = $dateAndTime->plus(Duration::withSeconds(1)); $this->assertEqual(strtolower(get_class($result)), 'dateandtime'); $this->assertTrue($result->isEqualTo(DateAndTime::withYearDayHourMinuteSecond(2005, 100, 0, 0, 1))); // minus a Duration $dateAndTime = DateAndTime::withYearDayHourMinuteSecond(2005, 100, 0, 0, 0); $result = $dateAndTime->minus(Duration::withSeconds(1)); $this->assertEqual(strtolower(get_class($result)), 'dateandtime'); $this->assertEqual($result->year(), 2005); $this->assertEqual($result->dayOfYear(), 99); $this->assertEqual($result->hour(), 23); $this->assertEqual($result->minute(), 59); $this->assertEqual($result->second(), 59); $this->assertTrue($result->isEqualTo(DateAndTime::withYearDayHourMinuteSecond(2005, 99, 23, 59, 59))); // Minus a DateAndTime $dateAndTime = DateAndTime::withYearDayHourMinuteSecond(2006, 100, 0, 0, 0); $result = $dateAndTime->minus(DateAndTime::withYearDayHourMinuteSecond(2005, 100, 0, 0, 0)); $this->assertEqual(strtolower(get_class($result)), 'duration'); $this->assertTrue($result->isEqualTo(Duration::withDays(365))); // Minus a DateAndTime over a leap year $dateAndTime = DateAndTime::withYearDayHourMinuteSecond(2005, 10, 0, 0, 0); $result = $dateAndTime->minus(DateAndTime::withYearDayHourMinuteSecond(2004, 10, 0, 0, 0)); $this->assertEqual(strtolower(get_class($result)), 'duration'); $this->assertTrue($result->isEqualTo(Duration::withDays(366))); // Plus a DateAndTime $dateAndTime = DateAndTime::withYearDayHourMinuteSecond(2000, 100, 5, 15, 30); $result = $dateAndTime->plus(DateAndTime::withYearDayHourMinuteSecond(2000, 100, 5, 30, 15)); $this->assertEqual(strtolower(get_class($result)), 'dateandtime'); $this->assertEqual($result->year(), 2000); $this->assertEqual($result->dayOfYear(), 100); $this->assertEqual($result->hour(), 10); $this->assertEqual($result->minute(), 45); $this->assertEqual($result->second(), 45); }
/** * 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; }
/** * Test durations that generate seconds larger than 2^32 seconds. * This may or may not work on some systems. * (32-bit) integers can be up to ~2billion. * This corresponds to ~100 years (36500 days). * The system may or may not support going over this limit. * The system this was developed on, * * Linux version 2.4.18-14 (bhcompile@stripples.devel.redhat.com) * (gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)) #1 * Wed Sep 4 13:35:50 EDT 2002 * * PHP 4.3.2 * './configure' '--with-apxs2=/usr/local/apache2/bin/apxs' * '--enable-safe-mode' '--with-openssl' '--with-gd' * '--enable-gd-native-ttf' '--with-jpeg-dir=/usr/lib' * '--with-png-dir=/usr/lib' '--with-xpm-dir=/usr/lib' * '--with-ttf=/usr/lib' '--with-t1lib=/usr/lib' '--with-ldap' * '--with-mysql' '--with-pdflib' '--with-zlib-dir=/usr/lib' * '--with-gettext' '--with-xml' '--with-pgsql=/usr' * '--with-java=/usr/java' '--with-iconv=/usr/local/lib' '--with-dom' * * supports the following tests. */ function test_big_durations() { // 50 years (18250 days) $duration = Duration::withDays(18250); $this->assertEqual($duration->days(), 18250); $this->assertEqual($duration->hours(), 0); $this->assertEqual($duration->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($duration->asSeconds(), 86400 * 18250); $this->assertEqual($duration->asSeconds(), 1576800000); // 100 years (36500 days) $duration = Duration::withDays(36500); $this->assertEqual($duration->days(), 36500); $this->assertEqual($duration->hours(), 0); $this->assertEqual($duration->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($duration->asSeconds(), 86400 * 36500); $this->assertEqual($duration->asSeconds(), 3153600000); // 500 years (182500 days) $duration = Duration::withDays(182500); $this->assertEqual($duration->days(), 182500); $this->assertEqual($duration->hours(), 0); $this->assertEqual($duration->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($duration->asSeconds(), 86400 * 182500); $this->assertEqual($duration->asSeconds(), 15768000000); // 5000 years (1825000 days) $duration = Duration::withDays(1825000); $this->assertEqual($duration->days(), 1825000); $this->assertEqual($duration->hours(), 0); $this->assertEqual($duration->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($duration->asSeconds(), 86400 * 1825000); $this->assertEqual($duration->asSeconds(), 157680000000); // 5000 years (1825000 days) // minus 500 years (182500 days) // should equal 4500 years (1642500 days) $duration = Duration::withDays(1825000); $result = $duration->minus(Duration::withDays(182500)); $this->assertEqual($result->days(), 1642500); $this->assertEqual($result->hours(), 0); $this->assertEqual($result->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($result->asSeconds(), 86400 * 1642500); $this->assertEqual($result->asSeconds(), 141912000000); // 500,000,000 years (182500000000 days) // minus 500 years (182500 days) // should equal 499,999,500 years (182481750000 days) $duration = Duration::withDays(182500000000); $result = $duration->minus(Duration::withDays(182500)); $this->assertEqual($result->days(), 182499817500); $this->assertEqual($result->hours(), 0); $this->assertEqual($result->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($result->asSeconds(), 86400 * 182499817500); $this->assertEqual($result->asSeconds(), hexdec('3804e5eaf8ea00')); // 50,000,000,000 years (18250000000000 days) // minus 500 years (182500 days) // should equal 49,999,999,500 years (18249999817500 days) $duration = Duration::withDays(18250000000000); $result = $duration->minus(Duration::withDays(182500)); $this->assertEqual($result->days(), 18249999817500); $this->assertEqual($result->hours(), 0); $this->assertEqual($result->minutes(), 0); $this->assertEqual($duration->seconds(), 0); $this->assertEqual($result->asSeconds(), 86400 * 18249999817500); $this->assertEqual($result->asSeconds(), hexdec('15e1eb3b3dfd6a00')); // Beyond negative 4 billion years, the precision drops from // second precision to hour precision. // 4,000,000,000 years (1460000000000 days) $duration = Duration::withDaysHoursMinutesSeconds(1460000000000, 2, 23, 12); $this->assertEqual($duration->days(), 1460000000000); $this->assertEqual($duration->hours(), 2); $this->assertEqual($duration->minutes(), 23); $this->assertEqual($duration->seconds(), 12); // 50,000,000,000 years (18250000000000 days) $duration = Duration::withDaysHoursMinutesSeconds(18250000000000, 2, 23, 12); $this->assertEqual($duration->days(), 18250000000000); $this->assertEqual($duration->hours(), 2); // $this->assertEqual($duration->minutes(), 23); // $this->assertEqual($duration->seconds(), 12); }
/** * Answer a TimeStamp which is anInteger days after the receiver. * * @param integer $anInteger * @return object TimeStamp * @access public * @since 5/13/05 */ function plusDays($anInteger) { $obj = $this->plus(Duration::withDays($anInteger)); return $obj; }
/** * Answer the date that occurs $anInteger days from this date * * @param integer $anInteger * @return object Date * @access public * @since 5/20/05 */ function addDays($anInteger) { $asDateAndTime = $this->asDateAndTime(); $newDateAndTime = $asDateAndTime->plus(Duration::withDays($anInteger)); $obj = $newDateAndTime->asDate(); return $obj; }
/** * Add in empty data arrays for dates with no values * * @param string $upcoming * @return void * @access private * @since 2/29/08 */ private function addEmptyDates($upcoming) { if (!count($this->data['labels'])) { return; } $i = Date::fromString(end($this->data['labels']))->plus(Duration::withDays(1)); $upcoming = Date::fromString($upcoming); while ($i->isLessThan($upcoming)) { $this->data['labels'][] = $i->yyyymmddString(); $this->data['comments'][] = 0; $this->data['edits'][] = 0; $this->data['files'][] = 0; $this->data['logins'][] = 0; $this->data['users'][] = 0; $this->data['errors'][] = 0; $i = $i->plus(Duration::withDays(1)); } }