Changeset 7151 for branches/2.4/prototype/plugins/datejs/time-debug.js
- Timestamp:
- 09/03/12 18:05:54 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.4/prototype/plugins/datejs/time-debug.js
r5341 r7151 1 1 /** 2 * Version: 1.0 Alpha-1 3 * Build Date: 12-Nov-2007 4 * Copyright (c) 2006-2007, Coolite Inc. (http://www.coolite.com/). All rights reserved. 5 * License: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/. 6 * Website: http://www.datejs.com/ or http://www.coolite.com/datejs/ 2 * @version: 1.0 Alpha-1 3 * @author: Coolite Inc. http://www.coolite.com/ 4 * @date: 2008-04-13 5 * @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved. 6 * @license: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/. 7 * @website: http://www.datejs.com/ 7 8 */ 8 9 9 10 /* 11 * TimeSpan(milliseconds); 12 * TimeSpan(days, hours, minutes, seconds); 10 13 * TimeSpan(days, hours, minutes, seconds, milliseconds); 11 * TimeSpan(milliseconds); 12 */ 13 TimeSpan = function (days, hours, minutes, seconds, milliseconds) { 14 this.days = 0; 15 this.hours = 0; 16 this.minutes = 0; 17 this.seconds = 0; 18 this.milliseconds = 0; 19 20 if (arguments.length == 5) { 21 this.days = days; 22 this.hours = hours; 23 this.minutes = minutes; 24 this.seconds = seconds; 25 this.milliseconds = milliseconds; 26 } 27 else if (arguments.length == 1 && typeof days == "number") { 14 */ 15 var TimeSpan = function (days, hours, minutes, seconds, milliseconds) { 16 var attrs = "days hours minutes seconds milliseconds".split(/\s+/); 17 18 var gFn = function (attr) { 19 return function () { 20 return this[attr]; 21 }; 22 }; 23 24 var sFn = function (attr) { 25 return function (val) { 26 this[attr] = val; 27 return this; 28 }; 29 }; 30 31 for (var i = 0; i < attrs.length ; i++) { 32 var $a = attrs[i], $b = $a.slice(0, 1).toUpperCase() + $a.slice(1); 33 TimeSpan.prototype[$a] = 0; 34 TimeSpan.prototype["get" + $b] = gFn($a); 35 TimeSpan.prototype["set" + $b] = sFn($a); 36 } 37 38 if (arguments.length == 4) { 39 this.setDays(days); 40 this.setHours(hours); 41 this.setMinutes(minutes); 42 this.setSeconds(seconds); 43 } else if (arguments.length == 5) { 44 this.setDays(days); 45 this.setHours(hours); 46 this.setMinutes(minutes); 47 this.setSeconds(seconds); 48 this.setMilliseconds(milliseconds); 49 } else if (arguments.length == 1 && typeof days == "number") { 28 50 var orient = (days < 0) ? -1 : +1; 29 this.milliseconds = Math.abs(days); 30 31 this.days = Math.floor(this.milliseconds / (24 * 60 * 60 * 1000)) * orient; 32 this.milliseconds = this.milliseconds % (24 * 60 * 60 * 1000); 33 34 this.hours = Math.floor(this.milliseconds / (60 * 60 * 1000)) * orient; 35 this.milliseconds = this.milliseconds % (60 * 60 * 1000); 36 37 this.minutes = Math.floor(this.milliseconds / (60 * 1000)) * orient; 38 this.milliseconds = this.milliseconds % (60 * 1000); 39 40 this.seconds = Math.floor(this.milliseconds / 1000) * orient; 41 this.milliseconds = this.milliseconds % 1000; 42 43 this.milliseconds = this.milliseconds * orient; 44 return this; 45 } 46 else { 47 return null; 48 } 49 }; 50 51 TimeSpan.prototype.compare = function (timeSpan) { 52 var t1 = new Date(1970, 1, 1, this.hours(), this.minutes(), this.seconds()), t2; 53 if (timeSpan === null) { 54 t2 = new Date(1970, 1, 1, 0, 0, 0); 55 } 56 else { 57 t2 = new Date(1970, 1, 1, timeSpan.hours(), timeSpan.minutes(), timeSpan.seconds()); /* t2 = t2.addDays(timeSpan.days()); */ 58 } 59 return (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0; 60 }; 61 62 TimeSpan.prototype.add = function (timeSpan) { 63 return (timeSpan === null) ? this : this.addSeconds(timeSpan.getTotalMilliseconds() / 1000); 64 }; 65 66 TimeSpan.prototype.subtract = function (timeSpan) { 67 return (timeSpan === null) ? this : this.addSeconds(-timeSpan.getTotalMilliseconds() / 1000); 68 }; 69 70 TimeSpan.prototype.addDays = function (n) { 71 return new TimeSpan(this.getTotalMilliseconds() + (n * 24 * 60 * 60 * 1000)); 72 }; 73 74 TimeSpan.prototype.addHours = function (n) { 75 return new TimeSpan(this.getTotalMilliseconds() + (n * 60 * 60 * 1000)); 76 }; 77 78 TimeSpan.prototype.addMinutes = function (n) { 79 return new TimeSpan(this.getTotalMilliseconds() + (n * 60 * 1000)); 80 }; 81 82 TimeSpan.prototype.addSeconds = function (n) { 83 return new TimeSpan(this.getTotalMilliseconds() + (n * 1000)); 84 }; 85 86 TimeSpan.prototype.addMilliseconds = function (n) { 87 return new TimeSpan(this.getTotalMilliseconds() + n); 88 }; 89 90 TimeSpan.prototype.getTotalMilliseconds = function () { 91 return (this.days() * (24 * 60 * 60 * 1000)) + (this.hours() * (60 * 60 * 1000)) + (this.minutes() * (60 * 1000)) + (this.seconds() * (1000)); 92 }; 93 94 TimeSpan.prototype.get12HourHour = function () { 95 return ((h = this.hours() % 12) ? h : 12); 96 }; 97 98 TimeSpan.prototype.getDesignator = function () { 99 return (this.hours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator; 100 }; 101 102 TimeSpan.prototype.toString = function (format) { 103 function _toString() { 104 if (this.days() !== null && this.days() > 0) { 105 return this.days() + "." + this.hours() + ":" + p(this.minutes()) + ":" + p(this.seconds()); 106 } 107 else { 108 return this.hours() + ":" + p(this.minutes()) + ":" + p(this.seconds()); 109 } 110 } 111 function p(s) { 112 return (s.toString().length < 2) ? "0" + s : s; 113 } 114 var self = this; 115 return format ? format.replace(/d|dd|HH|H|hh|h|mm|m|ss|s|tt|t/g, 116 function (format) { 117 switch (format) { 118 case "d": 119 return self.days(); 120 case "dd": 121 return p(self.days()); 122 case "H": 123 return self.hours(); 124 case "HH": 125 return p(self.hours()); 126 case "h": 127 return self.get12HourHour(); 128 case "hh": 129 return p(self.get12HourHour()); 130 case "m": 131 return self.minutes(); 132 case "mm": 133 return p(self.minutes()); 134 case "s": 135 return self.seconds(); 136 case "ss": 137 return p(self.seconds()); 138 case "t": 139 return ((this.hours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator).substring(0, 1); 140 case "tt": 141 return (this.hours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator; 142 } 143 } 144 ) : this._toString(); 51 this.setMilliseconds(Math.abs(days)); 52 53 this.setDays(Math.floor(this.getMilliseconds() / 86400000) * orient); 54 this.setMilliseconds(this.getMilliseconds() % 86400000); 55 56 this.setHours(Math.floor(this.getMilliseconds() / 3600000) * orient); 57 this.setMilliseconds(this.getMilliseconds() % 3600000); 58 59 this.setMinutes(Math.floor(this.getMilliseconds() / 60000) * orient); 60 this.setMilliseconds(this.getMilliseconds() % 60000); 61 62 this.setSeconds(Math.floor(this.getMilliseconds() / 1000) * orient); 63 this.setMilliseconds(this.getMilliseconds() % 1000); 64 65 this.setMilliseconds(this.getMilliseconds() * orient); 66 } 67 68 this.getTotalMilliseconds = function () { 69 return (this.getDays() * 86400000) + (this.getHours() * 3600000) + (this.getMinutes() * 60000) + (this.getSeconds() * 1000); 70 }; 71 72 this.compareTo = function (time) { 73 var t1 = new Date(1970, 1, 1, this.getHours(), this.getMinutes(), this.getSeconds()), t2; 74 if (time === null) { 75 t2 = new Date(1970, 1, 1, 0, 0, 0); 76 } 77 else { 78 t2 = new Date(1970, 1, 1, time.getHours(), time.getMinutes(), time.getSeconds()); 79 } 80 return (t1 < t2) ? -1 : (t1 > t2) ? 1 : 0; 81 }; 82 83 this.equals = function (time) { 84 return (this.compareTo(time) === 0); 85 }; 86 87 this.add = function (time) { 88 return (time === null) ? this : this.addSeconds(time.getTotalMilliseconds() / 1000); 89 }; 90 91 this.subtract = function (time) { 92 return (time === null) ? this : this.addSeconds(-time.getTotalMilliseconds() / 1000); 93 }; 94 95 this.addDays = function (n) { 96 return new TimeSpan(this.getTotalMilliseconds() + (n * 86400000)); 97 }; 98 99 this.addHours = function (n) { 100 return new TimeSpan(this.getTotalMilliseconds() + (n * 3600000)); 101 }; 102 103 this.addMinutes = function (n) { 104 return new TimeSpan(this.getTotalMilliseconds() + (n * 60000)); 105 }; 106 107 this.addSeconds = function (n) { 108 return new TimeSpan(this.getTotalMilliseconds() + (n * 1000)); 109 }; 110 111 this.addMilliseconds = function (n) { 112 return new TimeSpan(this.getTotalMilliseconds() + n); 113 }; 114 115 this.get12HourHour = function () { 116 return (this.getHours() > 12) ? this.getHours() - 12 : (this.getHours() === 0) ? 12 : this.getHours(); 117 }; 118 119 this.getDesignator = function () { 120 return (this.getHours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator; 121 }; 122 123 this.toString = function (format) { 124 this._toString = function () { 125 if (this.getDays() !== null && this.getDays() > 0) { 126 return this.getDays() + "." + this.getHours() + ":" + this.p(this.getMinutes()) + ":" + this.p(this.getSeconds()); 127 } 128 else { 129 return this.getHours() + ":" + this.p(this.getMinutes()) + ":" + this.p(this.getSeconds()); 130 } 131 }; 132 133 this.p = function (s) { 134 return (s.toString().length < 2) ? "0" + s : s; 135 }; 136 137 var me = this; 138 139 return format ? format.replace(/dd?|HH?|hh?|mm?|ss?|tt?/g, 140 function (format) { 141 switch (format) { 142 case "d": 143 return me.getDays(); 144 case "dd": 145 return me.p(me.getDays()); 146 case "H": 147 return me.getHours(); 148 case "HH": 149 return me.p(me.getHours()); 150 case "h": 151 return me.get12HourHour(); 152 case "hh": 153 return me.p(me.get12HourHour()); 154 case "m": 155 return me.getMinutes(); 156 case "mm": 157 return me.p(me.getMinutes()); 158 case "s": 159 return me.getSeconds(); 160 case "ss": 161 return me.p(me.getSeconds()); 162 case "t": 163 return ((me.getHours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator).substring(0, 1); 164 case "tt": 165 return (me.getHours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator; 166 } 167 } 168 ) : this._toString(); 169 }; 170 return this; 171 }; 172 173 /** 174 * Gets the time of day for this date instances. 175 * @return {TimeSpan} TimeSpan 176 */ 177 Date.prototype.getTimeOfDay = function () { 178 return new TimeSpan(0, this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds()); 145 179 }; 146 180 147 181 /* 148 182 * TimePeriod(startDate, endDate); 183 * TimePeriod(years, months, days, hours, minutes, seconds, milliseconds); 149 184 */ 150 185 var TimePeriod = function (years, months, days, hours, minutes, seconds, milliseconds) { 151 this.years = 0; 152 this.months = 0; 153 this.days = 0; 154 this.hours = 0; 155 this.minutes = 0; 156 this.seconds = 0; 157 this.milliseconds = 0; 158 159 // startDate and endDate as arguments 160 if (arguments.length == 2 && arguments[0] instanceof Date && arguments[1] instanceof Date) { 161 162 var date1 = years.clone(); 163 var date2 = months.clone(); 164 165 var temp = date1.clone(); 166 var orient = (date1 > date2) ? -1 : +1; 167 168 this.years = date2.getFullYear() - date1.getFullYear(); 186 var attrs = "years months days hours minutes seconds milliseconds".split(/\s+/); 187 188 var gFn = function (attr) { 189 return function () { 190 return this[attr]; 191 }; 192 }; 193 194 var sFn = function (attr) { 195 return function (val) { 196 this[attr] = val; 197 return this; 198 }; 199 }; 200 201 for (var i = 0; i < attrs.length ; i++) { 202 var $a = attrs[i], $b = $a.slice(0, 1).toUpperCase() + $a.slice(1); 203 TimePeriod.prototype[$a] = 0; 204 TimePeriod.prototype["get" + $b] = gFn($a); 205 TimePeriod.prototype["set" + $b] = sFn($a); 206 } 207 208 if (arguments.length == 7) { 209 this.years = years; 210 this.months = months; 211 this.setDays(days); 212 this.setHours(hours); 213 this.setMinutes(minutes); 214 this.setSeconds(seconds); 215 this.setMilliseconds(milliseconds); 216 } else if (arguments.length == 2 && arguments[0] instanceof Date && arguments[1] instanceof Date) { 217 // startDate and endDate as arguments 218 219 var d1 = years.clone(); 220 var d2 = months.clone(); 221 222 var temp = d1.clone(); 223 var orient = (d1 > d2) ? -1 : +1; 224 225 this.years = d2.getFullYear() - d1.getFullYear(); 169 226 temp.addYears(this.years); 170 227 171 228 if (orient == +1) { 172 if (temp > d ate2) {229 if (temp > d2) { 173 230 if (this.years !== 0) { 174 231 this.years--; … … 176 233 } 177 234 } else { 178 if (temp < d ate2) {235 if (temp < d2) { 179 236 if (this.years !== 0) { 180 237 this.years++; … … 183 240 } 184 241 185 d ate1.addYears(this.years);242 d1.addYears(this.years); 186 243 187 244 if (orient == +1) { 188 while (d ate1 < date2 && date1.clone().addDays(date1.getDaysInMonth()) < date2) {189 d ate1.addMonths(1);245 while (d1 < d2 && d1.clone().addDays(Date.getDaysInMonth(d1.getYear(), d1.getMonth()) ) < d2) { 246 d1.addMonths(1); 190 247 this.months++; 191 248 } 192 249 } 193 250 else { 194 while (d ate1 > date2 && date1.clone().addDays(-date1.getDaysInMonth()) > date2) {195 d ate1.addMonths(-1);251 while (d1 > d2 && d1.clone().addDays(-d1.getDaysInMonth()) > d2) { 252 d1.addMonths(-1); 196 253 this.months--; 197 254 } 198 255 } 199 256 200 var diff = d ate2 - date1;257 var diff = d2 - d1; 201 258 202 259 if (diff !== 0) { 203 260 var ts = new TimeSpan(diff); 204 205 this.days = ts.days; 206 this.hours = ts.hours; 207 this.minutes = ts.minutes; 208 this.seconds = ts.seconds; 209 this.milliseconds = ts.milliseconds; 210 } 211 212 // UTC Hacks required... 213 return this; 214 } 215 261 this.setDays(ts.getDays()); 262 this.setHours(ts.getHours()); 263 this.setMinutes(ts.getMinutes()); 264 this.setSeconds(ts.getSeconds()); 265 this.setMilliseconds(ts.getMilliseconds()); 266 } 267 } 268 return this; 216 269 };
Note: See TracChangeset
for help on using the changeset viewer.