예제 #1
0
 public function testFebruary()
 {
     // Time zone with daylight savings time from the first Sunday in November
     // to the last Sunday in February.
     // Similar to the new rule for Brazil (Sao Paulo) in tzdata2006n.
     //
     // Note: In tzdata2007h, the rule had changed, so no actual zones uses
     // lastSun in Feb anymore.
     $tz1 = new AgaviSimpleTimeZone($this->tm, -3 * AgaviDateDefinitions::MILLIS_PER_HOUR, "nov-feb", AgaviDateDefinitions::NOVEMBER, 1, AgaviDateDefinitions::SUNDAY, 0, AgaviDateDefinitions::FEBRUARY, -1, AgaviDateDefinitions::SUNDAY, 0);
     // Now hardcode the same rules as for Brazil, so that we cover the intended code
     // even when in the future zoneinfo hardcodes these transition dates.
     $tz2 = new AgaviSimpleTimeZone($this->tm, -3 * AgaviDateDefinitions::MILLIS_PER_HOUR, "nov-feb2", AgaviDateDefinitions::NOVEMBER, 1, -AgaviDateDefinitions::SUNDAY, 0, AgaviDateDefinitions::FEBRUARY, -29, -AgaviDateDefinitions::SUNDAY, 0);
     // Gregorian calendar with the UTC time zone for getting sample test date/times.
     $gc = new AgaviGregorianCalendar(AgaviTimeZone::getGMT($this->tm));
     $data = array(array('year' => 2006, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 5, 'hour' => 02, 'minute' => 59, 'second' => 59, 'offsetHours' => -3), array('year' => 2006, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 5, 'hour' => 03, 'minute' => 00, 'second' => 00, 'offsetHours' => -2), array('year' => 2007, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 25, 'hour' => 01, 'minute' => 59, 'second' => 59, 'offsetHours' => -2), array('year' => 2007, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 25, 'hour' => 02, 'minute' => 00, 'second' => 00, 'offsetHours' => -3), array('year' => 2007, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 4, 'hour' => 02, 'minute' => 59, 'second' => 59, 'offsetHours' => -3), array('year' => 2007, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 4, 'hour' => 03, 'minute' => 00, 'second' => 00, 'offsetHours' => -2), array('year' => 2008, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 24, 'hour' => 01, 'minute' => 59, 'second' => 59, 'offsetHours' => -2), array('year' => 2008, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 24, 'hour' => 02, 'minute' => 00, 'second' => 00, 'offsetHours' => -3), array('year' => 2008, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 2, 'hour' => 02, 'minute' => 59, 'second' => 59, 'offsetHours' => -3), array('year' => 2008, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 2, 'hour' => 03, 'minute' => 00, 'second' => 00, 'offsetHours' => -2), array('year' => 2009, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 22, 'hour' => 01, 'minute' => 59, 'second' => 59, 'offsetHours' => -2), array('year' => 2009, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 22, 'hour' => 02, 'minute' => 00, 'second' => 00, 'offsetHours' => -3), array('year' => 2009, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 1, 'hour' => 02, 'minute' => 59, 'second' => 59, 'offsetHours' => -3), array('year' => 2009, 'month' => AgaviDateDefinitions::NOVEMBER, 'day' => 1, 'hour' => 03, 'minute' => 00, 'second' => 00, 'offsetHours' => -2), array('year' => 2010, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 28, 'hour' => 01, 'minute' => 59, 'second' => 59, 'offsetHours' => -2), array('year' => 2010, 'month' => AgaviDateDefinitions::FEBRUARY, 'day' => 28, 'hour' => 02, 'minute' => 00, 'second' => 00, 'offsetHours' => -3));
     $timezones = array($tz1, $tz2);
     #	    TimeZone *tz;
     #	    UDate dt;
     #	    int32_t t, i, raw, dst;
     $t = $i = $raw = $dst = null;
     for ($t = 0; $t < count($timezones); ++$t) {
         $tz = clone $timezones[$t];
         for ($i = 0; $i < count($data); ++$i) {
             $gc->set($data[$i]['year'], $data[$i]['month'], $data[$i]['day'], $data[$i]['hour'], $data[$i]['minute'], $data[$i]['second']);
             $dt = $gc->getTime();
             $tz->getOffsetRef($dt, false, $raw, $dst);
             if ($raw + $dst != $data[$i]['offsetHours'] * AgaviDateDefinitions::MILLIS_PER_HOUR) {
                 $this->fail(sprintf("test case %d.%d: tz.getOffset(%04d-%02d-%02d %02d:%02d:%02d) returns %d+%d != %d", $t, $i, $data[$i]['year'], $data[$i]['month'] + 1, $data[$i]['day'], $data[$i]['hour'], $data[$i]['minute'], $data[$i]['second'], $raw, $dst, $data[$i]['offsetHours'] * AgaviDateDefinitions::MILLIS_PER_HOUR));
             }
         }
     }
 }
 /**
  * Creates a new calendar instance with the current time set.
  *
  * @param      mixed This can be either an AgaviLocale, an AgaviTimeZone or
  *                   a string specifying the calendar type.
  *
  * @return     AgaviCalendar The current timezone instance.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 public function createCalendar($type = null)
 {
     $locale = $this->getCurrentLocale();
     $calendarType = null;
     $zone = null;
     $time = null;
     if ($type instanceof AgaviLocale) {
         $locale = $type;
     } elseif ($type instanceof AgaviTimeZone) {
         $zone = $type;
     } elseif ($type instanceof DateTime) {
         $time = $type;
     } elseif (is_int($type)) {
         $time = $type * AgaviDateDefinitions::MILLIS_PER_SECOND;
     } elseif ($type !== null) {
         $calendarType = $type;
     }
     if ($time === null) {
         $time = AgaviCalendar::getNow();
     }
     if (!$zone) {
         if ($locale->getLocaleTimeZone()) {
             $zone = $this->createTimeZone($locale->getLocaleTimeZone());
         }
     }
     if (!$calendarType) {
         $calendarType = $locale->getLocaleCalendar();
         if (!$calendarType) {
             $calendarType = AgaviCalendar::GREGORIAN;
         }
     }
     switch ($calendarType) {
         case AgaviCalendar::GREGORIAN:
             $c = new AgaviGregorianCalendar($this);
             break;
         default:
             throw new AgaviException('Calendar type ' . $calendarType . ' not supported');
     }
     // Now, reset calendar to default state:
     if ($zone) {
         $c->setTimeZone($zone);
     }
     if ($time instanceof DateTime) {
         // FIXME: we can't use $time->getTimezone()->getName() here since that triggers
         // https://github.com/facebook/hhvm/issues/1777 but luckily using format('e')
         // works for both php and hhvm
         $tzName = $time->format('e');
         if (preg_match('/^[+-0-9]/', $tzName)) {
             $tzName = 'GMT' . $tzName;
         }
         $c->setTimeZone($this->createTimeZone($tzName));
         $dateStr = $time->format('Y z G i s');
         list($year, $doy, $hour, $minute, $second) = explode(' ', $dateStr);
         $c->set(AgaviDateDefinitions::YEAR, $year);
         $c->set(AgaviDateDefinitions::DAY_OF_YEAR, $doy + 1);
         $c->set(AgaviDateDefinitions::HOUR_OF_DAY, $hour);
         $c->set(AgaviDateDefinitions::MINUTE, $minute);
         $c->set(AgaviDateDefinitions::SECOND, $second);
         // complete the calendar
         $c->getAll();
     } else {
         $c->setTime($time);
         // let the new calendar have the current time.
     }
     return $c;
 }
 /**
  * Queries if the given date is in daylight savings time in
  * this time zone.
  * This method is wasteful since it creates a new AgaviGregorianCalendar and
  * deletes it each time it is called. 
  *
  * @param      float The given time
  * 
  * @return     bool  True if the given date is in daylight savings time,
  *                   false, otherwise.
  * 
  * @author     Dominik del Bondio <*****@*****.**>
  * @author     The ICU Project
  * @since      0.11.0
  */
 public function inDaylightTime($date)
 {
     $cal = new AgaviGregorianCalendar($this);
     $cal->setTime($date);
     return $cal->inDaylightTime();
 }
예제 #4
0
 /**
  * Initializes the 100-year window that dates with 2-digit years are 
  * considered to fall within so that its start date is 80 years before the 
  * current time.
  * 
  * @author     Dominik del Bondio <*****@*****.**>
  * @author     The ICU Project
  * @since      0.11.0
  */
 private static function initializeSystemDefaultCentury()
 {
     // initialize systemDefaultCentury and systemDefaultCenturyYear based
     // on the current time.  They'll be set to 80 years before
     // the current time.
     // No point in locking as it should be idempotent.
     if (self::$fgSystemDefaultCenturyStart == self::$fgSystemDefaultCentury) {
         $calendar = new GregorianCalendar();
         $calendar->setTime(AgaviCalendar::getNow());
         $calendar->add(AgaviDateDefinitions::YEAR, -80);
         $newStart = $calendar->getTime();
         $newYear = $calendar->get(AgaviDateDefinitions::YEAR);
         self::$fgSystemDefaultCenturyStart = $newStart;
         self::$fgSystemDefaultCenturyStartYear = $newYear;
     }
 }
예제 #5
0
 /**
  * Verify that various fields on a known date are set correctly.
  */
 public function testFields060()
 {
     $year = 1997;
     $month = AgaviDateDefinitions::OCTOBER;
     $dDate = 22;
     $calendar = new AgaviGregorianCalendar($this->tm, $year, $month, $dDate);
     $expectedFields = array(AgaviDateDefinitions::YEAR => 1997, AgaviDateDefinitions::MONTH => AgaviDateDefinitions::OCTOBER, AgaviDateDefinitions::DATE => 22, AgaviDateDefinitions::DAY_OF_WEEK => AgaviDateDefinitions::WEDNESDAY, AgaviDateDefinitions::DAY_OF_WEEK_IN_MONTH => 4, AgaviDateDefinitions::DAY_OF_YEAR => 295);
     foreach ($expectedFields as $field => $value) {
         $fieldValue = $calendar->get($field);
         $this->assertEquals($value, (int) $fieldValue);
     }
 }
 /**
  * Creates a new calendar instance with the current time set.
  *
  * @param      mixed This can be either an AgaviLocale, an AgaviTimeZone or
  *                   a string specifying the calendar type.
  *
  * @return     AgaviCalendar The current timezone instance.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 public function createCalendar($type = null)
 {
     $locale = $this->getCurrentLocale();
     $calendarType = null;
     $zone = null;
     $time = null;
     if ($type instanceof AgaviLocale) {
         $locale = $type;
     } elseif ($type instanceof AgaviTimeZone) {
         $zone = $type;
     } elseif ($type instanceof DateTime) {
         $time = $type;
     } elseif (is_int($type)) {
         $time = $type * AgaviDateDefinitions::MILLIS_PER_SECOND;
     } elseif ($type !== null) {
         $calendarType = $type;
     }
     if ($time === null) {
         $time = AgaviCalendar::getNow();
     }
     if (!$zone) {
         if ($locale->getLocaleTimeZone()) {
             $zone = $this->createTimeZone($locale->getLocaleTimeZone());
         }
     }
     if (!$calendarType) {
         $calendarType = $locale->getLocaleCalendar();
         if (!$calendarType) {
             $calendarType = AgaviCalendar::GREGORIAN;
         }
     }
     switch ($calendarType) {
         case AgaviCalendar::GREGORIAN:
             $c = new AgaviGregorianCalendar($this);
             break;
         default:
             throw new AgaviException('Calendar type ' . $calendarType . ' not supported');
     }
     // Now, reset calendar to default state:
     if ($zone) {
         $c->setTimeZone($zone);
     }
     if ($time instanceof DateTime) {
         $tzName = $time->getTimezone()->getName();
         if (version_compare(PHP_VERSION, '5.3', '<')) {
             // when a datetime object is created with an timezone offset like in '2005-02-21 00:00:00+01:00'
             // php falsely returns the name of the current default timezone as the name of the datetimes timezone
             // but luckily timezone abbreviation (T) is GMT name (GMT-0200) of the timezone
             // to not accidentally report dates which are really in the default timezone the name is explicitly checked
             if ($tzName == date_default_timezone_get()) {
                 $abbr = $time->format('T');
                 if (preg_match('/^GMT[+-]\\d{4}$/', $abbr)) {
                     $tzName = $abbr;
                 }
             }
         }
         if (preg_match('/^[+-0-9]/', $tzName)) {
             $tzName = 'GMT' . $tzName;
         }
         $c->setTimeZone($this->createTimeZone($tzName));
         $dateStr = $time->format('Y z G i s');
         list($year, $doy, $hour, $minute, $second) = explode(' ', $dateStr);
         $c->set(AgaviDateDefinitions::YEAR, $year);
         $c->set(AgaviDateDefinitions::DAY_OF_YEAR, $doy + 1);
         $c->set(AgaviDateDefinitions::HOUR_OF_DAY, $hour);
         $c->set(AgaviDateDefinitions::MINUTE, $minute);
         $c->set(AgaviDateDefinitions::SECOND, $second);
         // complete the calendar
         $c->getAll();
     } else {
         $c->setTime($time);
         // let the new calendar have the current time.
     }
     return $c;
 }