source: trunk/prototype/plugins/datejs/time-debug.js @ 7143

Revision 7143, 8.7 KB checked in by antonio, 12 years ago (diff)

Ticket #3085 - Corrigida inconsistência com o formato de hora no Expresso Calendar

RevLine 
[5341]1/**
[7143]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/
[5341]8 */
9 
10/*
[7143]11 * TimeSpan(milliseconds);
12 * TimeSpan(days, hours, minutes, seconds);
[5341]13 * TimeSpan(days, hours, minutes, seconds, milliseconds);
14 */
[7143]15var TimeSpan = function (days, hours, minutes, seconds, milliseconds) {
16    var attrs = "days hours minutes seconds milliseconds".split(/\s+/);
[5341]17   
[7143]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") {
[5341]50        var orient = (days < 0) ? -1 : +1;
[7143]51        this.setMilliseconds(Math.abs(days));
[5341]52       
[7143]53        this.setDays(Math.floor(this.getMilliseconds() / 86400000) * orient);
54        this.setMilliseconds(this.getMilliseconds() % 86400000);
[5341]55
[7143]56        this.setHours(Math.floor(this.getMilliseconds() / 3600000) * orient);
57        this.setMilliseconds(this.getMilliseconds() % 3600000);
[5341]58
[7143]59        this.setMinutes(Math.floor(this.getMilliseconds() / 60000) * orient);
60        this.setMilliseconds(this.getMilliseconds() % 60000);
[5341]61
[7143]62        this.setSeconds(Math.floor(this.getMilliseconds() / 1000) * orient);
63        this.setMilliseconds(this.getMilliseconds() % 1000);
[5341]64
[7143]65        this.setMilliseconds(this.getMilliseconds() * orient);
[5341]66    }
67
[7143]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    };
[5341]82
[7143]83    this.equals = function (time) {
84        return (this.compareTo(time) === 0);
85    };   
[5341]86
[7143]87    this.add = function (time) {
88        return (time === null) ? this : this.addSeconds(time.getTotalMilliseconds() / 1000);
89    };
[5341]90
[7143]91    this.subtract = function (time) {
92        return (time === null) ? this : this.addSeconds(-time.getTotalMilliseconds() / 1000);
93    };
[5341]94
[7143]95    this.addDays = function (n) {
96        return new TimeSpan(this.getTotalMilliseconds() + (n * 86400000));
97    };
[5341]98
[7143]99    this.addHours = function (n) {
100        return new TimeSpan(this.getTotalMilliseconds() + (n * 3600000));
101    };
[5341]102
[7143]103    this.addMinutes = function (n) {
104        return new TimeSpan(this.getTotalMilliseconds() + (n * 60000));
105    };
[5341]106
[7143]107    this.addSeconds = function (n) {
108        return new TimeSpan(this.getTotalMilliseconds() + (n * 1000));
109    };
[5341]110
[7143]111    this.addMilliseconds = function (n) {
112        return new TimeSpan(this.getTotalMilliseconds() + n);
113    };
[5341]114
[7143]115    this.get12HourHour = function () {
116        return (this.getHours() > 12) ? this.getHours() - 12 : (this.getHours() === 0) ? 12 : this.getHours();
117    };
[5341]118
[7143]119    this.getDesignator = function () {
120        return (this.getHours() < 12) ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator;
121    };
[5341]122
[7143]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            }
[5341]167        }
[7143]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 */
177Date.prototype.getTimeOfDay = function () {
178    return new TimeSpan(0, this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
[5341]179};
180
181/*
182 * TimePeriod(startDate, endDate);
[7143]183 * TimePeriod(years, months, days, hours, minutes, seconds, milliseconds);
[5341]184 */
185var TimePeriod = function (years, months, days, hours, minutes, seconds, milliseconds) {
[7143]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    }
[5341]207   
[7143]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
[5341]218   
[7143]219        var d1 = years.clone();
220        var d2 = months.clone();
[5341]221   
[7143]222        var temp = d1.clone();
223        var orient = (d1 > d2) ? -1 : +1;
[5341]224       
[7143]225        this.years = d2.getFullYear() - d1.getFullYear();
[5341]226        temp.addYears(this.years);
227       
228        if (orient == +1) {
[7143]229            if (temp > d2) {
[5341]230                if (this.years !== 0) {
231                    this.years--;
232                }
233            }
234        } else {
[7143]235            if (temp < d2) {
[5341]236                if (this.years !== 0) {
237                    this.years++;
238                }
239            }
240        }
241       
[7143]242        d1.addYears(this.years);
[5341]243
244        if (orient == +1) {
[7143]245            while (d1 < d2 && d1.clone().addDays(Date.getDaysInMonth(d1.getYear(), d1.getMonth()) ) < d2) {
246                d1.addMonths(1);
[5341]247                this.months++;
248            }
249        }
250        else {
[7143]251            while (d1 > d2 && d1.clone().addDays(-d1.getDaysInMonth()) > d2) {
252                d1.addMonths(-1);
[5341]253                this.months--;
254            }
255        }
256       
[7143]257        var diff = d2 - d1;
[5341]258
259        if (diff !== 0) {
260            var ts = new TimeSpan(diff);
[7143]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());
[5341]266        }
267    }
[7143]268    return this;
[5341]269};
Note: See TracBrowser for help on using the repository browser.