/**
  * 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;
 }
 /**
  * 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;
 }