package test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import com.caucho.util.QDate;

import junit.framework.TestCase;

public class TestQDate extends TestCase {

	QDate _calendar = new QDate();

	SimpleDateFormat headerFormat;

	private final static long HOUR = 3600000L;

	@Override
	protected void setUp() throws Exception {
		headerFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",
				Locale.US);
		headerFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
		super.setUp();
	}

	public void testFirstFailing() throws Exception {
		long time = System.currentTimeMillis();
		for (int i = 0; i < 365; i++) {
			String dateString = headerFormat.format(new Date(time));
			long expected = headerFormat.parse(dateString).getTime();
			long actual = _calendar.parseDate(dateString);
			assertEquals("First failing timestamp: " + dateString + " (off by "
					+ (expected - actual) + " ms)", expected, actual);
			time -= 24 * HOUR;
		}
	}

}
