
//	====================================================================================================
//	...dayse.js - javascript functions for date and time conversion...
//		-	(c)2004 by Glimage - written by J.C.Valks
//	====================================================================================================

//	----------------------------------------------------------------------------------------------------
//	...function 'E' - convert 'year' - 'month' - 'day to 'number of days'...
//	----------------------------------------------------------------------------------------------------
	E=function(y) {return 365*(y-1984)+(-1&y/4)-(-1&y/100)+(-1&y/400)-(-1&(1200+y)/3200)}

//	----------------------------------------------------------------------------------------------------
//	...function 'Dm' - convert 'month' to 'days'...
//	----------------------------------------------------------------------------------------------------
	Dm=function(m) {return -1&(2+153*m)/5}

//	----------------------------------------------------------------------------------------------------
//	...function 'Md' - convert 'days' to 'months'...
//	----------------------------------------------------------------------------------------------------
	Md=function(d) {return -1&(2+5*d)/153}

//	----------------------------------------------------------------------------------------------------
//	...function 'dt2dayse' - convert 'date-time' to 'dayse'...
//		-	dayse is defined as the number of days since Monday, January 1st 1984...
//	----------------------------------------------------------------------------------------------------
	dt2dayse=function(year,month,day,hours,minutes,seconds) {
		return date2dayse(year,month,day)+time2dayse(hours,minutes,seconds);
	}

//	----------------------------------------------------------------------------------------------------
//	...function 'date2dayse' - convert 'date' to 'dayse'...
//	----------------------------------------------------------------------------------------------------
	date2dayse=function(year,month,day) {
		var date=new Date();
		year=year||date.getYear();
		month=month||(1+date.getMonth());
		day=day||(date.getDate());
		return E(year-(-1&(12-month)/10))+Dm((9+month)%12)+day-422
	}

//	----------------------------------------------------------------------------------------------------
//	...function 'time2dayse' - convert 'time' to 'dayse'...
//	----------------------------------------------------------------------------------------------------
	time2dayse=function(hours,minutes,seconds) {
		var date=new Date();
		hours=hours||date.getHours();
		minutes=minutes||date.getMinutes();
		seconds=seconds||date.getSeconds();
		return (seconds+60*(minutes+60*hours))/86400
	}

//	----------------------------------------------------------------------------------------------------
//	...function 'dayse2date' - convert 'dayse' to 'date-object'...
//		- properties of date-object:
//			o	day_week - the day of the week, 0=Monday, 1=Tuesday,...
//			o	year - the year...
//			o	day_year - the day in the year, starting with 0...
//			o	month - the month of the year, 1=January, 2=February,...
//			o	day - the day of the month, starting with 1...
//			o	week_year - the week of the year...
//	----------------------------------------------------------------------------------------------------
	dayse2date=function(dayse) {
		dayse=-1&dayse;
		var i,d,_=new Object();
		_.day_week=1+dayse%7;
		_.year=-1199+3200*(-1&(d=dayse+1162566)/1168775)+400*(-1&(d%=1168775)/146097)+100*(i=(-1&(d%=146097)/36524)-(-1&d/146096))+4*(-1&(d=(i+d)%36525)/1461)+(i=(-1&(d%=1461)/365)-(-1&d/1460));
		_.day_year=(d=(i+d)%366);
		d=(306+d)%(365+(-1&(i=_.year-1)%4/3)-(-1&i%100/99)+(-1&i%400/399)-(-1&(1200+i)%3200/3199));
		_.month=1+(2+Md(d))%12;
		_.day=1+d-Dm(Md(d));
		var wJan4th_Prev=-1&(E(_.year-2)+252)/7;
		var wJan4th_This=-1&(dayse-_.day_year+367)/7;
		var wJan4th_Next=-1&(E(_.year)+252)/7;
		var wToday=-1&(dayse+364)/7;
		_.week_year=1+(wToday-wJan4th_Prev-(wJan4th_This-wJan4th_Prev)*(-1&(wToday+wJan4th_Next-2*wJan4th_This+1)/(wJan4th_Next-wJan4th_This+1)))%(wJan4th_Next-wJan4th_This);
		return _;
	}

//	----------------------------------------------------------------------------------------------------
//	...function 'dayse2time' - convert 'dayse' to 'time-object'...
//		- properties of time-object:
//			o	seconds - ranging from 0 to 59...
//			o	minutes - ranging from 0 to 59...
//			o	hours - ranging from 0 to 23...
//			o	hours12 - ranging from 1 to 12...
//			o	a - appendix - either 'a' or 'p'...
//	----------------------------------------------------------------------------------------------------
	dayse2time=function(dayse) {
		dayse=-1&dayse*86400;
		var _=new Object();
		_.seconds=dayse%60;
		_.minutes=(dayse=-1&dayse/60)%60;
		_.hours=(dayse=-1&dayse/60)%24;
		_.hours12=1+(_.hours+11)%12;
		_.a=(_.hours<13)?'a':'p';
		return _;
	}

