/**
 * 
 */
package com.trendrr.oss.casting;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.trendrr.oss.IsoDateUtil;
import com.trendrr.oss.Reflection;
import com.trendrr.oss.TypeCast;



/**
 * @author Dustin Norlander
 * @created Nov 30, 2010
 * 
 */
public class DateCaster extends TypeCaster<Date> {

	/* (non-Javadoc)
	 * @see com.trendrr.oss.casting.TypeCaster#cast(java.lang.Class, java.lang.Object)
	 */
	@Override
	public Date doCast(Class cls, Object obj) {
		if (obj instanceof Calendar) 
			return ((Calendar)obj).getTime();
		
		if (obj instanceof Long) {
			long val = (Long)obj;
			if (val < 200000000000l) {
				//date is before 1976 so we just assume
				//assume this is seconds since epoch
				val *= 1000;
			}
			return new Date(val);
		}
		
		//convert joda DateTimezx
		if (Reflection.hasMethod(obj, "toDate")) {
			return (Date)Reflection.execute(obj, "toDate");
		}
		String str = TypeCast.cast(String.class, obj);
		try {
			//try iso format
			return IsoDateUtil.parse(str);
		} catch (Exception x) {
			//swallow
		}
		
		try {
			//TODO: reverse standard Date().toString()
			
			
		} catch (Exception x) {
			
		}

		
		
		
		
		return null;
	}

}

@@ -68,16 +68,51 @@
 Format;%0A
+import java.text.SimpleDateFormat;%0A
 import j
@@ -378,16 +378,123 @@
 ate%3E %7B%0A%0A
+%09public static final DateFormat RSS_DATE_FORMAT =%0A%09%09new SimpleDateFormat(%22EEE, dd MMM yyyy HH:mm:ss z%22);%0A%09%0A
 %09/* (non
@@ -1232,24 +1232,133 @@
 %0A%09%09%0A%09%09try %7B%0A
+%09%09%09//try RSS 2.0 standard%0A%09%09%09return RSS_DATE_FORMAT.parse(str);%0A%09%09%7D catch (Exception x) %7B%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09try %7B%0A
 %09%09%09//TODO: r

add RSS date format to date caster