/** * 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)); }
/** * Test the enumeration methods. */ function test_multipleduration_enumeration() { $schedule = Schedule::startingDuration(DateAndTime::withYearMonthDay(2005, 5, 15), Duration::withDays(7)); $durations = array(); $durations[] = Duration::withDays(1); $durations[] = Duration::withHours(1); $schedule->setSchedule($durations); $this->assertEqual($schedule->getSchedule(), $durations); $datesAndTimes = array(); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 15, 0, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 16, 0, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 16, 1, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 17, 1, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 17, 2, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 18, 2, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 18, 3, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 19, 3, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 19, 4, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 20, 4, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 20, 5, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 21, 5, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 21, 6, 0, 0); $this->assertEqual($schedule->dateAndTimes(), $datesAndTimes); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 21, 5, 0, 0); $this->assertNotEqual($schedule->dateAndTimes(), $datesAndTimes); $datesAndTimes = array(); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 17, 1, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 18, 1, 0, 0); $datesAndTimes[] = DateAndTime::withYearMonthDayHourMinuteSecond(2005, 5, 18, 2, 0, 0); $this->assertEqual($schedule->between(DateAndTime::withYearMonthDay(2005, 5, 17), DateAndTime::withYearMonthDay(2005, 5, 19)), $datesAndTimes); }
/** * Converts a database datetime/timestamp/time value (that has been fetched * from the db) to a DateAndTime object. * * Depending on the server configuration PostgreSQL retrieves date/time * types as 4 different formats. (format could be set with SET DateStyle * ). The default formatting is 'ISO'. Note that timestamp column types * return the full string. Date column types only return the date (no time). * * Style Description Example * 'ISO' ISO-8601 standard 1997-12-17 07:37:16-08 * 'SQL' Traditional style 12/17/1997 07:37:16.00 PST * 'PostgreSQL' Original style Wed Dec 17 07:37:16 1997 PST * 'German' Regional style 17.12.1997 07:37:16.00 PST * * @access public * @param mixed A database datetime/timestamp/time value (that has been fetched * from the db). * @return ref object The DateAndTime object. */ function fromDBDate($value) { if (in_array($value, array(NULL, '', '0000-00-00 00:00:00'))) { return NULL; } // Postgre if (preg_match("/([a-zA-Z]{3}) ([a-zA-Z]{3}) ([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) ([0-9]{4})/", $value, $r)) { $months = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12); return DateAndTime::withYearMonthDayHourMinuteSecond($r[7], $months[$r[2]], $r[3], $r[4], $r[5], $r[6]); } // German if (preg_match("/([0-9]{2})\\.([0-9]{2})\\.([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]{2}) .../", $value, $r)) { return DateAndTime::withYearMonthDayHourMinuteSecond($r[3], $r[2], $r[1], $r[4], $r[5], $r[6]); } // ISO/SQL $obj = DateAndTime::fromString($value); return $obj; }
/** * Test converting */ function test_converting() { $dateAndTime = DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 15, 25, 10, Duration::withHours(-5)); // asDate() $temp = $dateAndTime->asDate(); $this->assertTrue($temp->isEqualTo(Date::withYearMonthDay(2005, 6, 4))); // asDuration() $temp = $dateAndTime->asDuration(); $this->assertTrue($temp->isEqualTo(Duration::withSeconds(55510))); // asDateAndTime() $temp = $dateAndTime->asDateAndTime(); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 15, 25, 10, Duration::withHours(-5)))); // asLocal() $startDuration = Duration::withHours(-5); $localOffset = DateAndTime::localOffset(); $difference = $localOffset->minus($startDuration); $temp = $dateAndTime->asLocal(); $local = DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 15 + $difference->hours(), 25, 10, $localOffset); $this->assertTrue($temp->isEqualTo($local)); // asMonth() $temp = $dateAndTime->asMonth(); $this->assertTrue($temp->isEqualTo(Month::withMonthYear(6, 2005))); // asSeconds() $localOffset = DateAndTime::localOffset(); $this->assertEqual($dateAndTime->asSeconds(), 3295369510 + $localOffset->asSeconds()); // asTime() $temp = $dateAndTime->asTime(); $this->assertTrue($temp->isEqualTo(Time::withHourMinuteSecond(15, 25, 10))); $this->assertTrue($temp->isEqualTo(Time::withSeconds(55510))); // asTimeStamp() $temp = $dateAndTime->asTimeStamp(); $this->assertTrue($temp->isEqualTo(TimeStamp::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 15, 25, 10, Duration::withHours(-5)))); // asUTC() $temp = $dateAndTime->asUTC(); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 20, 25, 10, Duration::withHours(0)))); // asWeek() $temp = $dateAndTime->asWeek(); $this->assertTrue($temp->isEqualTo(Week::starting($dateAndTime))); // asYear() $temp = $dateAndTime->asYear(); $this->assertTrue($temp->isEqualTo(Year::starting($dateAndTime))); // midnight(); $temp = $dateAndTime->atMidnight(); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecond(2005, 6, 4, 0, 0, 0))); // middleOf() $dat = DateAndTime::withYearDay(2005, 100); $timespan = $dat->middleOf(Duration::withDays(100)); $start = $timespan->start(); $duration = $timespan->duration(); $end = $timespan->end(); $this->assertEqual($start->dayOfYear(), 50); $this->assertTrue($start->isEqualTo(DateAndTime::withYearDay(2005, 50))); $this->assertEqual($duration->days(), 100); $this->assertEqual($end->dayOfYear(), 149); // to() $datA = DateAndTime::withYearDay(2005, 125); $datB = DateAndTime::withYearDay(2006, 125); $timespan = $datA->to($datB); $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))); // withOffset() $temp = $dateAndTime->withOffset(Duration::withHours(-7)); $this->assertTrue($temp->isEqualTo(DateAndTime::withYearMonthDayHourMinuteSecondOffset(2005, 6, 4, 15, 25, 10, Duration::withHours(-7)))); }
/** * Create a new instance. * * @param integer $anIntYear * @param integer $anIntOrStringMonth * @param integer $anIntDay * @param integer $anIntHour * @param integer $anIntMinute * @param integer $anIntSecond * @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 TimeStamp * @access public * @static * @since 5/4/05 */ static function withYearMonthDayHourMinuteSecond($anIntYear, $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class = 'TimeStamp') { $obj = parent::withYearMonthDayHourMinuteSecond($anIntYear, $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class); return $obj; }
/** * Create a new instance. * * @param integer $anIntYear * @param integer $anIntOrStringMonth * @param integer $anIntDay * @param integer $anIntHour * @param integer $anIntMinute * @param integer $anIntSecond * @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 StorableTime * @access public * @static * @since 5/4/05 */ static function withYearMonthDayHourMinuteSecond($anIntYear, $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class = 'StorableTime') { return parent::withYearMonthDayHourMinuteSecond($anIntYear, $anIntOrStringMonth, $anIntDay, $anIntHour, $anIntMinute, $anIntSecond, $class); }