$native(Date);Date.extend({clone:function(){return new Date(this.getTime())},increment:function(a,b){this.setTime(this.getTime()+Date.$units[a||"day"]*(b||1));return this},decrement:function(a,b){this.setTime(this.getTime()-Date.$units[a||"day"]*(b||1));return this},clearTime:function(){this.setHours(0);this.setMinutes(0);this.setSeconds(0);this.setMilliseconds(0);return this},diff:function(b,a){if($type(b)=="string"){b=Date.parse(b)}return Math.floor((this.getTime()-b.getTime())/Date.$units[a||"day"])},getOrdinal:function(){var a=this.toString();var b=a.substr(-(Math.min(a.length,2)));return(b>3&&b<21)?"th":["th","st","nd","rd","th"][Math.min(this%10,4)]},getDayOfYear:function(){return((Date.UTC(this.getFullYear(),this.getMonth(),this.getDate()+1,0,0,0)-Date.UTC(this.getFullYear(),0,1,0,0,0))/Date.$units.day)},lastDayofMonth:function(){var a=this.clone();a.setMonth(a.getMonth()+1,0);return a.getDate()},getWeek:function(){var a=(new Date(this.getFullYear(),0,1)).getDate();return Math.round((this.getDayOfYear()+(a>3?a-4:a+3))/7)},getTimezone:function(){return this.toString().replace(/^.*? ([A-Z]{3}).[0-9]{4}.*$/,"$1").replace(/^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/,"$1$2$3")},getGMTOffset:function(){var a=this.getTimezoneOffset();return((a>0)?"-":"+")+Math.floor(Math.abs(a)/60).zeroise(2)+(a%60).zeroise(2)},isLeapYear:function(){return Date.isLeapYear(this.getYear())},daysInMonth:function(b,a){return Date.isLeapYear(this.getMonth(),this.getYear())},parse:function(a){this.setTime(Date.parse(a));return this},format:function(a){if(!this.valueOf()){return"&nbsp;"}if(Date.$formats[a.toLowerCase()]){a=Date.$formats[a.toLowerCase()]}var b=this;return a.replace(/\%([aAbBcdHIjmMpSUWwxXyYTZ])/g,function(c,d){switch(d){case"a":return Date.$days[b.getDay()].substr(0,3);case"A":return Date.$days[b.getDay()];case"b":return Date.$months[b.getMonth()].substr(0,3);case"B":return Date.$months[b.getMonth()];case"c":return b.toString();case"d":return b.getDate().zeroise(2);case"H":return b.getHours().zeroise(2);case"I":return((b.getHours()%12)||12).zeroise(2);case"j":return b.getDayOfYear().zeroise(3);case"m":return(b.getMonth()+1).zeroise(2);case"M":return b.getMinutes().zeroise(2);case"p":return b.getHours()<12?"AM":"PM";case"S":return b.getSeconds().zeroise(2);case"U":return b.getWeek().zeroise(2);case"W":throw new Error("%W is not supported yet");case"w":return b.getDay();case"x":return b.format("%m/%d/%Y");case"X":return b.format("%I:%M%p");case"y":return b.getFullYear().toString().substr(2);case"Y":return b.getFullYear();case"T":return b.getGMTOffset();case"Z":return b.getTimezone();case"%":return"%"}return d})}});Date.prototype.compare=Date.prototype.diff;Date.prototype.strftime=Date.prototype.format;Date.$nativeParse=Date.parse;$extend(Date,{$months:["January","February","March","April","May","June","July","August","September","October","November","December"],$days:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],$daysInMonth:[31,28,31,30,31,30,31,31,30,31,30,31],$epoch:-1,$era:-2,$units:{ms:1,second:1000,minute:60000,hour:3600000,day:86400000,week:608400000,month:2678400000,year:31536000000},$formats:{db:"%Y-%m-%d %H:%M:%S",iso8601:"%Y-%m-%dT%H:%M:%S%T",rfc822:"%a, %d %b %Y %H:%M:%S %Z","short":"%d %b %H:%M","long":"%B %d, %Y %H:%M"},isLeapYear:function(a){return(((a%4)===0)&&((a%1000)!==0)||((a%4000)===0))},daysInMonth:function(b,a){b=(b+12)%12;if(b==1&&Date.isLeapYear(a)){return 29}return Date.$daysInMonth[b]},parse:function(e){var c=$type(e);if(c=="number"){return new Date(str)}if(c!="string"){return e}if(!e.length){return null}for(var b=0,a=Date.$parsePatterns.length;b<a;b++){var d=Date.$parsePatterns[b].re.exec(e);if(d){return Date.$parsePatterns[b].handler(d)}}return new Date(Date.$nativeParse(e))},parseMonth:function(d,c){var b=-1;switch($type(d)){case"object":b=Date.$months[d.getMonth()];break;case"number":b=Date.$months[d-1]||false;if(!b){throw new Error("Invalid month index value must be between 1 and 12:"+index)}break;case"string":var a=Date.$months.filter(function(e){return this.test(e)},new RegExp("^"+d,"i"));if(!a.length){throw new Error("Invalid month string")}if(a.length>1){throw new Error("Ambiguous month")}b=a[0];break}return(c)?Date.$months.indexOf(b):b},parseDay:function(a,d){var c=-1;switch($type(a)){case"number":c=Date.$days[a-1]||false;if(!c){throw new Error("Invalid day index value must be between 1 and 7")}break;case"string":var b=Date.$days.filter(function(e){return this.test(e)},new RegExp("^"+a,"i"));if(!b.length){throw new Error("Invalid day string")}if(b.length>1){throw new Error("Ambiguous day")}c=b[0];break}return(d)?Date.$days.indexOf(c):c},$parsePatterns:[{re:/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/,handler:function(a){var b=new Date();b.setYear(a[3]);b.setMonth(a[1].toInt()-1,a[2].toInt());return b}},{re:/^(\d{4})(?:-?(\d{2})(?:-?(\d{2})(?:[T ](\d{2})(?::?(\d{2})(?::?(\d{2})(?:\.(\d+))?)?)?(?:Z|(?:([-+])(\d{2})(?::?(\d{2}))?)?)?)?)?)?$/,handler:function(a){var c=0;var b=new Date(a[1],0,1);if(a[2]){b.setMonth(a[2]-1)}if(a[3]){b.setDate(a[3])}if(a[4]){b.setHours(a[4])}if(a[5]){b.setMinutes(a[5])}if(a[6]){b.setSeconds(a[6])}if(a[7]){b.setMilliseconds(("0."+a[7]).toInt()*1000)}if(a[9]){c=(a[9].toInt()*60)+a[10].toInt();c*=((a[8]=="-")?1:-1)}c-=b.getTimezoneOffset();b.setTime((b*1)+(c*60*1000).toInt());return b}},{re:/^tod/i,handler:function(){return new Date()}},{re:/^tom/i,handler:function(){var a=new Date();a.setDate(a.getDate()+1);return a}},{re:/^yes/i,handler:function(){var a=new Date();a.setDate(a.getDate()-1);return a}},{re:/^(\d{1,2})(st|nd|rd|th)?$/i,handler:function(a){var b=new Date();b.setDate(a[1].toInt());return b}},{re:/^(\d{1,2})(?:st|nd|rd|th)? (\w+)$/i,handler:function(a){var b=new Date();b.setMonth(Date.parseMonth(a[2],true),a[1].toInt());return b}},{re:/^(\d{1,2})(?:st|nd|rd|th)? (\w+),? (\d{4})$/i,handler:function(a){var b=new Date();b.setMonth(Date.parseMonth(a[2],true),a[1].toInt());b.setYear(a[3]);return b}},{re:/^(\w+) (\d{1,2})(?:st|nd|rd|th)?,? (\d{4})$/i,handler:function(a){var b=new Date();b.setMonth(Date.parseMonth(a[1],true),a[2].toInt());b.setYear(a[3]);return b}},{re:/^next (\w+)$/i,handler:function(e){var f=new Date();var b=f.getDay();var c=Date.parseDay(e[1],true);var a=c-b;if(c<=b){a+=7}f.setDate(f.getDate()+a);return f}},{re:/^last (\w+)$/i,handler:function(a){throw new Error("Not yet implemented")}}]});Number.extend({zeroise:function(a){return String(this).zeroise(a)}});String.extend({repeat:function(c){var a=[];for(var b=0;b<c;b++){a.push(this)}return a.join("")},zeroise:function(a){return"0".repeat(a-this.length)+this}});
