Exemple #1
0
  virtual int computePreference(const std::string& id,
				boost::local_time::time_zone_ptr tz)
  {
    /*
     * We implement here the following heuristic:
     *
     * If abbrev != name:
     *   preference = 6 (this seems to select 'standard' cities in USA)
     * Otherwise,
     *   take first city of the following 'preferred' list:
     *     - Europe (5)
     *     - Asia (4),
     *     - Australia (3),
     *     - America (2)
     * Otherwise, 1
     */

    if (tz->std_zone_abbrev() != tz->std_zone_name())
      return 6;
    else {
      if (boost::starts_with(id, "Europe/"))
	return 5;
      else if (boost::starts_with(id, "Australia/"))
	return 4;
      else if (boost::starts_with(id, "Asia/"))
	return 3;
      else if (boost::starts_with(id, "America/"))
	return 2;
      else
	return 1;
    }
  }
Exemple #2
0
sj::time_of_day::time_of_day(boost::posix_time::time_duration time,
                         boost::local_time::time_zone_ptr tz,
                         boost::optional<bool> dst)
    : _time(time)
    , _tz(tz)
    , _dst(dst)
{
    assert(time >= time_duration() && time < time_duration(hours(24)));
    static time_duration zerodur = time_duration(hours(0));

    // following code is only to check if dst parameter is neccessary
    if (dst)
    {
        return;
    }

    if (tz->dst_offset() < zerodur)
    {
        std::stringstream ss;
        ss << "irrational timezone: " << tz->std_zone_name()
           << " DST offset: " << tz->dst_offset();
        throw std::invalid_argument(ss.str());
    }

    static date sampledate(2000, 1, 15);

    ptime sampletime(sampledate, time);
    time_duration tz_starttime = tz->dst_local_start_time(2000).time_of_day();
    time_duration tz_endtime = tz->dst_local_end_time(2000).time_of_day();
    ptime startsample(sampledate, tz_starttime);
    if (startsample > sampletime)
    {
        startsample -= days(1);
    }
    ptime endsample(sampledate, tz_endtime);
    if (endsample < sampletime)
    {
        endsample += days(1);
    }

    time_period tpd1(startsample, tz->dst_offset());
    time_period tpd2(endsample - tz->dst_offset(), tz->dst_offset());

    if (tpd1.contains(sampletime) || tpd2.contains(sampletime))
    {
        std::stringstream ss;
        ss << "time_of_day(): dst setting is required for "
           << "tz: " << tz->std_zone_name() << " time: " << time;
        throw std::invalid_argument(ss.str());
    }
}
Exemple #3
0
void XRuleTime::parse_time_zone(
    IronBee::ConfigurationParser      cp,
    const char*                       str,
    boost::local_time::time_zone_ptr& zone
)
{
    using namespace std;
    using namespace boost::local_time;

    string tzstr(str);

    tzstr.insert(
        ((str[0] == '-' || str[0] == '+') ? 3 : 2),
        ":"
    );

    try {
        zone.reset(new posix_time_zone(tzstr));
    }
    catch (const boost::local_time::bad_offset& e) {
        BOOST_THROW_EXCEPTION(
            IronBee::einval()
                << IronBee::errinfo_level(IB_LOG_ERROR)
                << IronBee::errinfo_what(
                    " Zone offset out of range. "
                    "Valid values are -1200 <= tz <= +1400.")
        );
    }
    catch (const boost::local_time::bad_adjustment& e) {
        BOOST_THROW_EXCEPTION(
            IronBee::einval()
                << IronBee::errinfo_what(e.what())
                << IronBee::errinfo_level(IB_LOG_ERROR)
        );
    }
    catch (const std::out_of_range& e) {
        BOOST_THROW_EXCEPTION(
            IronBee::einval()
                << IronBee::errinfo_what(e.what())
                << IronBee::errinfo_level(IB_LOG_ERROR)
        );
    }
}