source: trunk/prototype/app/plugins/datejs/sugarpak-debug.js @ 5341

Revision 5341, 7.8 KB checked in by wmerlotto, 12 years ago (diff)

Ticket #2434 - Commit inicial do novo módulo de agenda do Expresso - expressoCalendar

Line 
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 **************************************************************
11 ** SugarPak - Domain Specific Language -  Syntactical Sugar **
12 **************************************************************
13 */
14 
15/**
16 * Gets a date that is set to the current date and time.
17 * @return {Date}    The current date and time.
18 */
19Date.now = function () {
20    return new Date();
21};
22
23/**
24 * Gets a date that is set to the current date. The time is set to the start of the day (00:00 or 12:00 AM).
25 * @return {Date}    The current date.
26 */
27Date.today = function () {
28    return Date.now().clearTime();
29};
30
31// private
32Date.prototype._orient = +1;
33
34/**
35 * Moves the date to the next instance of a date as specified by a trailing date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
36 * Example
37<pre><code>
38Date.today().next().friday();
39Date.today().next().fri();
40Date.today().next().march();
41Date.today().next().mar();
42Date.today().next().week();
43</code></pre>
44 *
45 * @return {Date}    this
46 */
47Date.prototype.next = function () {
48    this._orient = +1;
49    return this;
50};
51
52/**
53 * Moves the date to the previous instance of a date as specified by a trailing date element function (eg. .day(), .month()), month name function (eg. .january(), .jan()) or day name function (eg. .friday(), fri()).
54 * Example
55<pre><code>
56Date.today().last().friday();
57Date.today().last().fri();
58Date.today().last().march();
59Date.today().last().mar();
60Date.today().last().week();
61</code></pre>
62 * 
63 * @return {Date}    this
64 */
65Date.prototype.last = Date.prototype.prev = Date.prototype.previous = function () {
66    this._orient = -1;
67    return this;
68};
69
70// private
71Date.prototype._is = false;
72   
73/**
74 * Performs a equality check when followed by either a month name or day name function.
75 * Example
76<pre><code>
77Date.today().is().friday(); // true|false
78Date.today().is().fri();
79Date.today().is().march();
80Date.today().is().mar();
81</code></pre>
82 * 
83 * @return {bool}    true|false
84 */
85Date.prototype.is = function () {
86    this._is = true;
87    return this;
88};
89
90// private
91Number.prototype._dateElement = "day";
92
93/**
94 * Creates a new Date (Date.now()) and adds this (Number) to the date based on the preceding date element function (eg. second|minute|hour|day|month|year).
95 * Example
96<pre><code>
97// Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.
98(3).days().fromNow();
99(6).months().fromNow();
100
101// Declared Number variables do not require parentheses.
102var n = 6;
103n.months().fromNow();
104</code></pre>
105 * 
106 * @return {Date}    A new Date instance
107 */
108Number.prototype.fromNow = function () {
109    var c = {};
110    c[this._dateElement] = this;
111    return Date.now().add(c);
112};
113
114/**
115 * Creates a new Date (Date.now()) and subtract this (Number) from the date based on the preceding date element function (eg. second|minute|hour|day|month|year).
116 * Example
117<pre><code>
118// Undeclared Numbers must be wrapped with parentheses. Requirment of JavaScript.
119(3).days().ago();
120(6).months().ago();
121
122// Declared Number variables do not require parentheses.
123var n = 6;
124n.months().ago();
125</code></pre>
126 * 
127 * @return {Date}    A new Date instance
128 */
129Number.prototype.ago = function () {
130    var c = {};
131    c[this._dateElement] = this * -1;
132    return Date.now().add(c);
133};
134
135// Build dynamic date element, month name and day name functions.
136(function () {
137    var $D = Date.prototype, $N = Number.prototype;
138
139    /* Do NOT modify the following string tokens. These tokens are used to build dynamic functions. */
140    var dx = ("sunday monday tuesday wednesday thursday friday saturday").split(/\s/),
141        mx = ("january february march april may june july august september october november december").split(/\s/),
142        px = ("Millisecond Second Minute Hour Day Week Month Year").split(/\s/),
143        de;
144   
145    // Create day name functions and abbreviated day name functions (eg. monday(), friday(), fri()).
146    var df = function (n) {
147        return function () {
148            if (this._is) {
149                this._is = false;
150                return this.getDay() == n;
151            }
152            return this.moveToDayOfWeek(n, this._orient);
153        };
154    };
155   
156    for (var i = 0 ; i < dx.length ; i++) {
157        $D[dx[i]] = $D[dx[i].substring(0, 3)] = df(i);
158    }
159   
160    // Create month name functions and abbreviated month name functions (eg. january(), march(), mar()).
161    var mf = function (n) {
162        return function () {
163            if (this._is) {
164                this._is = false;
165                return this.getMonth() === n;
166            }
167            return this.moveToMonth(n, this._orient);
168        };
169    };
170   
171    for (var j = 0 ; j < mx.length ; j++) {
172        $D[mx[j]] = $D[mx[j].substring(0, 3)] = mf(j);
173    }
174   
175    // Create date element functions and plural date element functions used with Date (eg. day(), days(), months()).
176    var ef = function (j) {
177        return function () {
178            if (j.substring(j.length - 1) != "s") {
179                j += "s";
180            }
181            return this["add" + j](this._orient);
182        };
183    };
184   
185    // Create date element functions and plural date element functions used with Number (eg. day(), days(), months()).
186    var nf = function (n) {
187        return function () {
188            this._dateElement = n;
189            return this;
190        };
191    };
192   
193    for (var k = 0 ; k < px.length ; k++) {
194        de = px[k].toLowerCase();
195        $D[de] = $D[de + "s"] = ef(px[k]);
196        $N[de] = $N[de + "s"] = nf(de);
197    }
198}());
199
200/**
201 * Converts the current date instance into a JSON string value.
202 * @return {String}  JSON string of date
203 */
204Date.prototype.toJSONString = function () {
205    return this.toString("yyyy-MM-ddThh:mm:ssZ");
206};
207
208/**
209 * Converts the current date instance to a string using the culture specific shortDatePattern.
210 * @return {String}  A string formatted as per the culture specific shortDatePattern
211 */
212Date.prototype.toShortDateString = function () {
213    return this.toString(Date.CultureInfo.formatPatterns.shortDatePattern);
214};
215
216/**
217 * Converts the current date instance to a string using the culture specific longDatePattern.
218 * @return {String}  A string formatted as per the culture specific longDatePattern
219 */
220Date.prototype.toLongDateString = function () {
221    return this.toString(Date.CultureInfo.formatPatterns.longDatePattern);
222};
223
224/**
225 * Converts the current date instance to a string using the culture specific shortTimePattern.
226 * @return {String}  A string formatted as per the culture specific shortTimePattern
227 */
228Date.prototype.toShortTimeString = function () {
229    return this.toString(Date.CultureInfo.formatPatterns.shortTimePattern);
230};
231
232/**
233 * Converts the current date instance to a string using the culture specific longTimePattern.
234 * @return {String}  A string formatted as per the culture specific longTimePattern
235 */
236Date.prototype.toLongTimeString = function () {
237    return this.toString(Date.CultureInfo.formatPatterns.longTimePattern);
238};
239
240/**
241 * Get the ordinal suffix of the current day.
242 * @return {String}  "st, "nd", "rd" or "th"
243 */
244Date.prototype.getOrdinal = function () {
245    switch (this.getDate()) {
246    case 1:
247    case 21:
248    case 31:
249        return "st";
250    case 2:
251    case 22:
252        return "nd";
253    case 3:
254    case 23:
255        return "rd";
256    default:
257        return "th";
258    }
259};
Note: See TracBrowser for help on using the repository browser.