DateFormatPtr DatePatternConverter::getDateFormat()

in src/main/cpp/datepatternconverter.cpp [61:139]


DateFormatPtr DatePatternConverter::getDateFormat(const OptionsList& options)
{
	DateFormatPtr df;
	int maximumCacheValidity = 1000000;

	if (options.size() == 0)
	{
		df = std::make_shared<ISO8601DateFormat>();
	}
	else
	{
		LogString dateFormatStr(options[0]);

		if (dateFormatStr.empty() ||
			StringHelper::equalsIgnoreCase(dateFormatStr,
				LOG4CXX_STR("ISO8601"), LOG4CXX_STR("iso8601")))
		{
			df = std::make_shared<ISO8601DateFormat>();
		}
		else if (StringHelper::equalsIgnoreCase(dateFormatStr,
				LOG4CXX_STR("ABSOLUTE"), LOG4CXX_STR("absolute")))
		{
			df = std::make_shared<AbsoluteTimeDateFormat>();
		}
		else if (StringHelper::equalsIgnoreCase(dateFormatStr,
				LOG4CXX_STR("DATE"), LOG4CXX_STR("date")))
		{
			df = std::make_shared<DateTimeDateFormat>();
		}
		else
		{
			if (dateFormatStr.find(0x25 /*'%'*/) == std::string::npos)
			{
				try
				{
					df = std::make_shared<SimpleDateFormat>(dateFormatStr);
					maximumCacheValidity =
						CachedDateFormat::getMaximumCacheValidity(dateFormatStr);
				}
				catch (std::exception& e)
				{
					df = std::make_shared<ISO8601DateFormat>();
					LogLog::warn(((LogString)
							LOG4CXX_STR("Could not instantiate SimpleDateFormat with pattern "))
						+ dateFormatStr, e);
				}
			}
			else
			{
				df = std::make_shared<StrftimeDateFormat>(dateFormatStr);
			}
		}

		if (options.size() >= 2)
		{
			TimeZonePtr tz;
			try
			{
				tz = TimeZone::getTimeZone(options[1]);
			}
			catch (std::exception& e)
			{
				LogLog::warn(LOG4CXX_STR("Invalid time zone: ") + options[1], e);
			}

			if (tz)
			{
				df->setTimeZone(tz);
			}
		}
	}

	if (maximumCacheValidity > 0)
	{
		df = std::make_shared<CachedDateFormat>(df, maximumCacheValidity);
	}

	return df;
}