function OrderDay(year,month,day){
this.year = year || 1889;
this.month = month || 7;
this.day = day || 9;
}
OrderDay.prototype.fromOrderDay = function(orderDay){
this.year = orderDay.year;
this.month = orderDay.month;
this.day = orderDay.day;
}
OrderDay.prototype.fromDate = function(date){
this.year = date.getFullYear();
this.month = date.getMonth();
this.day = date.getDate();
}
OrderDay.prototype.fromTimelong = function(timelong){
var date = new Date(timelong);
this.fromDate(date);
}
OrderDay.prototype.parse = function(dateStr){
var arr = dateStr.split('-');
this.set('year',Number(arr[0]));
this.set('month',Number(arr[1]));
this.set('day',Number(arr[2]));
}
OrderDay.prototype.toDate = function(){
var date = new Date();
date.setFullYear(this.year);
date.setMonth(this.month);
date.setDate(this.day);
return date;
}
OrderDay.prototype.toTimelong = function(){
var date = new Date();
date.setFullYear(this.year);
date.setMonth(this.month);
date.setDate(this.day);
date.setHours(8, 0, 0, 0);//设置时间为默认8点钟
return date.getTime();
}
OrderDay.prototype.set = function(key,value){
this[key] = value;
}
OrderDay.prototype.stringify = function(){
return this.year +'-'+this.month+'-'+this.day;
}
OrderDay.prototype.getWeekDay = function(){
return this.toDate().getDay();
}
OrderDay.prototype.eq = function(day){
return this.year == day.year && this.month == day.month && this.day == day.day;
}
OrderDay.prototype.le = function(day){
if(this.year == day.year){
if(this.month == day.month){
return this.day <= day.day;
}else if(this.month < day.month){
return true;
}else{
return false;
}
}else if(this.year < day.year){
return true;
}else{
return false;
}
}
OrderDay.prototype.addDay = function(days){
var copy = new OrderDay();
copy.fromOrderDay(this);
copy.day = copy.day + days;
var date = copy.toDate();
copy.fromDate(date);
return copy;
}
OrderDay.prototype.addMonth = function(){
var copy = new OrderDay();
copy.fromOrderDay(this);
copy.month = copy.moth + 1;
var date = copy.toDate();
copy.fromDate(date);
return copy;
}
OrderDay.prototype.nextDays = function(count){
if(count > 0){
var ret = [],i = 0,orderDay ;
for(;i < count ;i ++){
orderDay = new OrderDay();
orderDay.fromOrderDay(this);
orderDay = orderDay.addDay( i + 1 );
ret.push(orderDay);
}
return ret;
}
}
OrderDay.prototype.readAble = function(pattern){
pattern = pattern || 'MM-dd(W)';
return this.toDate().format(pattern);
}
//简单的时间运算辅助类
function OrderTime(hour,min){
this.hour = hour||8;
this.min = min||0;
}
OrderTime.prototype.fromOrderTime = function(orderTime){
this.hour = orderTime.hour;
this.min = orderTime.min;
}
OrderTime.prototype.fromDate = function(date){
this.hour = date.getHours();
this.min = date.getMinutes();
}
OrderTime.prototype.fromTimelong = function(timelong){
var date = new Date(timelong);
this.hour = date.getHours();
this.min = date.getMinutes();
}
OrderTime.prototype.stringify = function(){
return this.hour + '-' + this.min;
}
OrderTime.prototype.parse = function(timestr){
var arr = timestr.split('-');
this.hour = arr[0];
this.min = arr[1];
}
OrderTime.prototype.addMin = function(args){
var totalMinutes = 0 ,totalHours = 0,arr;
var len = arguments.length;
//使参数可以是数组,可以是单个的数字,可以是正,页可以是负
if(len == 1){
if(arguments[0].splice){//数组
arr = arguments[0];
}else{
arr = [arguments[0]];
}
}else{
arr = Array.prototype.slice.call(arguments,0);
}
var i = 0 ; len = arr.length;
for(; i < len ;i ++){
totalMinutes = totalMinutes + Number(arr[i]);
}
if(totalMinutes >= 60){
totalHours = Math.floor( totalMinutes / 60 );
totalMinutes = totalMinutes % 60;
}else if (totalMinutes < 0) {
totalHours = -1 * Math.floor( (-1 * totalMinutes) / 60 ) - 1;
totalMinutes = 60 - ( (-1 * totalMinutes) % 60 );
}
//以上转换的结果,分钟一定是正,小时可能是负
var copy = new OrderTime();
copy.fromOrderTime(this);
copy.min = copy.min + totalMinutes;
copy.hour = copy.hour + totalHours;
//计算后,再计算一下分钟是否超过60就OK,修真一下进位
copy.hour = copy.hour + Math.floor(copy.min / 60);
copy.min = copy.min % 60;
return copy;
}
OrderTime.prototype.lt = function(orderTime){//时间早晚比较
if(this.hour < orderTime.hour){
return true;
}else if(this.hour == orderTime.hour && this.min < orderTime.min){
return true;
}else{
return false;
}
}
OrderTime.prototype.eq = function(orderTime){
return this.hour == orderTime.hour && this.min == orderTime.min;
}
OrderTime.prototype.le = function(orderTime) {//时间早晚比较
if (this.hour < orderTime.hour) {
return true;
} else if (this.hour == orderTime.hour && this.min <= orderTime.min) {
return true;
} else {
return false;
}
}
OrderTime.prototype.nextPoint = function(startTime,endTime,step){//下一个刻度点
startTime = startTime || new OrderTime(0,0);
endTime = endTime || new OrderTime(24,0);
step = step || 60;
var orderTime = new OrderTime();
orderTime.fromOrderTime(startTime);
var next ,current;
for(;orderTime.le(endTime);orderTime = orderTime.addMin(step)){
if(this.le(orderTime)){//第一次遇到比自己大的那个就有可能是,但是考虑到时间的容错能力,也有可能是前面的那个.
next = orderTime;
current = next.addMin(-1 * step);
break;
}
}
//比较自己和current的差距,如果在容差范围内,那么考虑接受,按照step的3分之一算。
//如果迟到step的三分之一,那么可以接受,否则,就只能选取下一个时间点
if(next){
if(this.le(current.addMin(Math.ceil(step/3)))){
return current;
}else{
return next;
}
}else{//没有找到以后的时间点了
return null;
}
}
OrderTime.prototype.prevPoint = function(startTime,endTime,step){
startTime = startTime || new OrderTime(0,0);
endTime = endTime || new OrderTime(24,0);
step = step || 60;
var orderTime = new OrderTime();
orderTime.fromOrderTime(startTime);
var prev ,current;
for(;orderTime.le(endTime);orderTime = orderTime.addMin(step)){
if(this.le(orderTime)){//第一次遇到比自己大的那个前一个就是,但是考虑到时间的容错能力,也有就是这一个.
current = orderTime;
prev = current.addMin(-1 * step);
break;
}
}
//比较自己和current的差距,如果在容差范围内,那么考虑接受,按照step的3分之一算。
//如果迟到step的三分之一,那么可以接受,否则,就只能选取下一个时间点
//把离自己比较近的点当做是需求刻度,当距离超过容错时,就取前一个刻度
if (current) {
if ( current.le( this.addMin( Math.ceil(step/3) ) ) ) {
return current;
} else {//没有找到以后的时间点了
return prev;
}
} else {
return null;
}
}
OrderTime.prototype.segments = function(fromOrderTime,toOrderTime,step) {//得到两个时间区间之间的时间点,整点 半点刻度对齐
step = step || 60;
var startTime = fromOrderTime.nextPoint();
var endTime = toOrderTime.prevPoint();
var segments = [];
var orderTime = new OrderTime();
orderTime.fromOrderTime(startTime);
for(;orderTime.le(endTime);orderTime = orderTime.addMin(step)){
segments.push(orderTime);
}
return segments;
}
OrderTime.prototype.readAble = function(){
return (this.hour < 10 ? ('0' + this.hour) : this.hour) + ":" + (this.min < 10 ? ('0' + this.min):this.min);
}