/**
 * Date handling utilities.
 */

Date.prototype.clone = function ()
{
	return new Date (this.getTime ());
}

module.exports =
{
	 WDays:
	[
		 'Sunday'
		,'Monday'
		,'Tuesday'
		,'Wednesday'
		,'Thursday'
		,'Friday'
		,'Saturday'
	]
	,AbrWDays:
	[
		 'Su'
		,'Mo'
		,'Tu'
		,'We'
		,'Th'
		,'Fr'
		,'Sa'
	]
	,Months:
	[
		 'January'
		,'February'
		,'March'
		,'April'
		,'May'
		,'June'
		,'July'
		,'August'
		,'September'
		,'October'
		,'November'
		,'December'
	]
	,AbrMonths:
	[
		 'Jan'
		,'Feb'
		,'Mar'
		,'Apr'
		,'MayAbr'
		,'Jun'
		,'Jul'
		,'Ago'
		,'Sep'
		,'Oct'
		,'Nov'
		,'Dec'
	]
	
	,tokenD: '%A, %B %e'
	
	,regexp: new RegExp ('%[a-zA-Z]', 'g')

	,pad: function (number)
	{
		if (number < 10)
			return '0'+ number.toString ();

		return number.toString ();
	}
	
	,regexpFunc: function (d, token)
	{
		switch (token.charAt (1))
		{
			// Minutes with 2 digits
			case 'M': return this.pad (d.getMinutes ());

			// Hour with 2 digits in 24 hour format
			case 'H': return this.pad (d.getHours ());

			// Complete time
			case 'T': return d.toLocaleTimeString ();

			// Complete week day name
			case 'A': return _(this.WDays[d.getDay ()]);

			// Abreviated week day name
			case 'a': return _(this.AbrWDays[d.getDay ()]);

			// Day of month with 2 digits
			case 'd': return this.pad (d.getDate ());

			// Day of month
			case 'e': return d.getDate ();

			// Complete month name
			case 'B': return _(this.Months[d.getMonth ()]);

			// Abreviated month name
			case 'b': return _(this.AbrMonths[d.getMonth ()]);

			// Month number with 2 digits
			case 'm': return this.pad (d.getMonth () + 1);

			// Year with 4 digits
			case 'Y': return d.getFullYear ();

			// Complete date without year
			case 'D': return _(this.tokenD).replace (this.regexp, this.regexpFunc.bind (this, d));
		}
		
		return token;
	}

	,strftime: function (date, format)
	{
		if (!date)
			return '';

		return format.replace (this.regexp, this.regexpFunc.bind (this, date));
	}
};