1 | /**
|
---|
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/
|
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * TimeSpan(milliseconds); |
---|
12 | * TimeSpan(days, hours, minutes, seconds); |
---|
13 | * TimeSpan(days, hours, minutes, seconds, milliseconds); |
---|
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") { |
---|
50 | var orient = (days < 0) ? -1 : +1; |
---|
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());
|
---|
179 | };
|
---|
180 |
|
---|
181 | /* |
---|
182 | * TimePeriod(startDate, endDate); |
---|
183 | * TimePeriod(years, months, days, hours, minutes, seconds, milliseconds); |
---|
184 | */
|
---|
185 | var TimePeriod = function (years, months, days, hours, minutes, seconds, milliseconds) {
|
---|
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();
|
---|
226 | temp.addYears(this.years);
|
---|
227 |
|
---|
228 | if (orient == +1) {
|
---|
229 | if (temp > d2) {
|
---|
230 | if (this.years !== 0) {
|
---|
231 | this.years--;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | } else {
|
---|
235 | if (temp < d2) {
|
---|
236 | if (this.years !== 0) {
|
---|
237 | this.years++;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | d1.addYears(this.years);
|
---|
243 |
|
---|
244 | if (orient == +1) {
|
---|
245 | while (d1 < d2 && d1.clone().addDays(Date.getDaysInMonth(d1.getYear(), d1.getMonth()) ) < d2) {
|
---|
246 | d1.addMonths(1);
|
---|
247 | this.months++;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | else {
|
---|
251 | while (d1 > d2 && d1.clone().addDays(-d1.getDaysInMonth()) > d2) {
|
---|
252 | d1.addMonths(-1);
|
---|
253 | this.months--;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | var diff = d2 - d1;
|
---|
258 |
|
---|
259 | if (diff !== 0) {
|
---|
260 | var ts = new TimeSpan(diff);
|
---|
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;
|
---|
269 | }; |
---|