void CachedDateFormat::format()

in src/main/cpp/cacheddateformat.cpp [246:312]


void CachedDateFormat::format(LogString& buf, log4cxx_time_t now, Pool& p) const
{

	//
	// If the current requested time is identical to the previously
	//     requested time, then append the cache contents.
	//
	if (now == m_priv->previousTime)
	{
		buf.append(m_priv->cache);
		return;
	}

	//
	//   If millisecond pattern was not unrecognized
	//     (that is if it was found or milliseconds did not appear)
	//
	if (m_priv->millisecondStart != UNRECOGNIZED_MILLISECONDS)
	{
		//    Check if the cache is still valid.
		//    If the requested time is within the same integral second
		//       as the last request and a shorter expiration was not requested.
		if (now < m_priv->slotBegin + m_priv->expiration
			&& now >= m_priv->slotBegin
			&& now < m_priv->slotBegin + 1000000L)
		{
			//
			//    if there was a millisecond field then update it
			//
			if (m_priv->millisecondStart >= 0)
			{
				millisecondFormat((int) ((now - m_priv->slotBegin) / 1000), m_priv->cache, m_priv->millisecondStart);
			}

			//
			//   update the previously requested time
			//      (the slot begin should be unchanged)
			m_priv->previousTime = now;
			buf.append(m_priv->cache);

			return;
		}
	}

	//
	//  could not use previous value.
	//    Call underlying formatter to format date.
	m_priv->cache.erase(m_priv->cache.begin(), m_priv->cache.end());
	m_priv->formatter->format(m_priv->cache, now, p);
	buf.append(m_priv->cache);
	m_priv->previousTime = now;
	m_priv->slotBegin = (m_priv->previousTime / 1000000) * 1000000;

	if (m_priv->slotBegin > m_priv->previousTime)
	{
		m_priv->slotBegin -= 1000000;
	}

	//
	//    if the milliseconds field was previous found
	//       then reevaluate in case it moved.
	//
	if (m_priv->millisecondStart >= 0)
	{
		m_priv->millisecondStart = findMillisecondStart(now, m_priv->cache, m_priv->formatter, p);
	}
}