[5341] | 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/
|
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | * 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") { |
---|
| 28 | 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();
|
---|
| 145 | };
|
---|
| 146 |
|
---|
| 147 | /* |
---|
| 148 | * TimePeriod(startDate, endDate); |
---|
| 149 | */
|
---|
| 150 | 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();
|
---|
| 169 | temp.addYears(this.years);
|
---|
| 170 |
|
---|
| 171 | if (orient == +1) {
|
---|
| 172 | if (temp > date2) {
|
---|
| 173 | if (this.years !== 0) {
|
---|
| 174 | this.years--;
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 | } else {
|
---|
| 178 | if (temp < date2) {
|
---|
| 179 | if (this.years !== 0) {
|
---|
| 180 | this.years++;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | date1.addYears(this.years);
|
---|
| 186 |
|
---|
| 187 | if (orient == +1) {
|
---|
| 188 | while (date1 < date2 && date1.clone().addDays(date1.getDaysInMonth()) < date2) {
|
---|
| 189 | date1.addMonths(1);
|
---|
| 190 | this.months++;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | else {
|
---|
| 194 | while (date1 > date2 && date1.clone().addDays(-date1.getDaysInMonth()) > date2) {
|
---|
| 195 | date1.addMonths(-1);
|
---|
| 196 | this.months--;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | var diff = date2 - date1;
|
---|
| 201 |
|
---|
| 202 | if (diff !== 0) {
|
---|
| 203 | 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 |
|
---|
| 216 | }; |
---|