]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/DP_DateExtensions.js
Start replacing jscalendar and DP_DateExtensions with Dojo
[Evergreen.git] / Open-ILS / web / opac / common / js / DP_DateExtensions.js
1 /*  DepressedPress.com DP_DateExtensions
2 Author: Jim Davis, the Depressed Press of Boston
3 Date: June 20, 2006
4 Contact: webmaster@depressedpress.com
5 Website: www.depressedpress.com
6
7 Full documentation can be found at:
8 http://www.depressedpress.com/Content/Development/JavaScript/Extensions/
9
10 DP_DateExtensions adds features to the JavaScript "Date" datatype.
11 Copyright (c) 1996-2006, The Depressed Press of Boston (depressedpress.com)
12 All rights reserved.
13 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
14
15 +) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
16 +) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
17 +) Neither the name of the DEPRESSED PRESS OF BOSTON (DEPRESSEDPRESS.COM) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
21
22 CHANGES: --------------------------------------------------------------------------------
23
24         2008-07-26 / dscott@laurentian.ca
25          - Comment out Date.parseIso8601 as we move to Dojo
26
27     2007-02-02 / billserickson@gmail.com
28      - chopped out some utility methods to trim file size
29      - changed some formatting for visual ease
30      - date / time can now be separated by a "T", "t" or a space
31      - truncating milliseconds.  not needed and .123456  
32         comes accross 123456ms and not 123ms + 456 microseconds
33 */
34
35 /*
36 Date.parseIso8601 = function(CurDate) {
37
38                 // Check the input parameters
39         if ( typeof CurDate != "string" ) {
40                 return null;
41         };
42                 // Set the fragment expressions
43         var S = "[\\-/:.]";
44         var Yr = "((?:1[6-9]|[2-9][0-9])[0-9]{2})";
45         var Mo = S + "((?:1[012])|(?:0[1-9])|[1-9])";
46         var Dy = S + "((?:3[01])|(?:[12][0-9])|(?:0[1-9])|[1-9])";
47         var Hr = "(2[0-4]|[01]?[0-9])";
48         var Mn = S + "([0-5]?[0-9])";
49         var Sd = "(?:" + S + "([0-5]?[0-9])(?:[.,]([0-9]+))?)?";
50         var TZ = "(?:(Z)|(?:([\+\-])(1[012]|[0]?[0-9])(?::?([0-5]?[0-9]))?))?";
51                 // RegEx the input
52                 // First check: Just date parts (month and day are optional)
53                 // Second check: Full date plus time (seconds, milliseconds and TimeZone info are optional)
54         var TF;
55
56         if ( TF = new RegExp("^" + Yr + "(?:" + Mo + "(?:" + Dy + ")?)?" + "$").exec(CurDate) ) {
57         } else if ( TF = new RegExp("^" + Yr + Mo + Dy + "[Tt ]" + Hr + Mn + Sd + TZ + "$").exec(CurDate) ) {};
58
59                 // If the date couldn't be parsed, return null
60         if ( !TF ) { return null };
61                 // Default the Time Fragments if they're not present
62         if ( !TF[2] ) { TF[2] = 1 } else { TF[2] = TF[2] - 1 };
63         if ( !TF[3] ) { TF[3] = 1 };
64         if ( !TF[4] ) { TF[4] = 0 };
65         if ( !TF[5] ) { TF[5] = 0 };
66         if ( !TF[6] ) { TF[6] = 0 };
67         if ( !TF[7] ) { TF[7] = 0 };
68         if ( !TF[8] ) { TF[8] = null };
69         if ( TF[9] != "-" && TF[9] != "+" ) { TF[9] = null };
70         if ( !TF[10] ) { TF[10] = 0 } else { TF[10] = TF[9] + TF[10] };
71         if ( !TF[11] ) { TF[11] = 0 } else { TF[11] = TF[9] + TF[11] };
72                 // If there's no timezone info the data is local time
73
74     TF[7] = 0;
75
76         if ( !TF[8] && !TF[9] ) {
77                 return new Date(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]);
78         };
79                 // If the UTC indicator is set the date is UTC
80         if ( TF[8] == "Z" ) {
81                 return new Date(Date.UTC(TF[1], TF[2], TF[3], TF[4], TF[5], TF[6], TF[7]));
82         };
83                 // If the date has a timezone offset
84         if ( TF[9] == "-" || TF[9] == "+" ) {
85                         // Get current Timezone information
86                 var CurTZ = new Date().getTimezoneOffset();
87                 var CurTZh = TF[10] - ((CurTZ >= 0 ? "-" : "+") + Math.floor(Math.abs(CurTZ) / 60))
88                 var CurTZm = TF[11] - ((CurTZ >= 0 ? "-" : "+") + (Math.abs(CurTZ) % 60))
89                         // Return the date
90                 return new Date(TF[1], TF[2], TF[3], TF[4] - CurTZh, TF[5] - CurTZm, TF[6], TF[7]);
91         };
92                 // If we've reached here we couldn't deal with the input, return null
93         return null;
94
95 };
96
97 */
98
99
100 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
101 /* "Date" Object Prototype Extensions */
102 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
103
104 Date.prototype.dateFormat = function(Mask) {
105
106         var FormattedDate = "";
107         var Ref_MonthFullName = ["January", "February", "March", "April", "May", 
108         "June", "July", "August", "September", "October", "November", "December"];
109         var Ref_MonthAbbreviation = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
110         var Ref_DayFullName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
111         var Ref_DayAbbreviation = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
112
113                 // Convert any supported simple masks into "real" masks
114         switch (Mask) {
115                 case "short":
116                         Mask = "m/d/yy";                
117                 break;
118                 case "medium":
119                         Mask = "mmm d, yyyy";
120                 break;
121                 case "long":
122                         Mask = "mmmm d, yyyy";
123                 break;
124                 case "full":
125                         Mask = "dddd, mmmm d, yyyy";
126                 break;
127         };
128
129                 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
130         Mask += " ";
131
132                 // Parse the Mask
133         var CurChar;
134         var MaskPart = "";
135         for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
136                         // Get the character
137                 CurChar = Mask.charAt(Cnt);
138                         // Determine if the character is a mask element
139                 if ( (CurChar != "d") && (CurChar != "m") && (CurChar != "y") ) {
140                                 // Determine if we need to parse a MaskPart or not
141                         if ( MaskPart != "" ) {
142                                         // Convert the mask part to the date value
143                                 switch (MaskPart) {
144                                         case "d":
145                                                 FormattedDate += this.getDate();
146                                                 break;
147                                         case "dd":
148                                                 FormattedDate += ("0" + this.getDate()).slice(-2);
149                                                 break;
150                                         case "ddd":
151                                                 FormattedDate += Ref_DayAbbreviation[this.getDay()];
152                                                 break;
153                                         case "dddd":
154                                                 FormattedDate += Ref_DayFullName[this.getDay()];
155                                                 break;
156                                         case "m":
157                                                 FormattedDate += this.getMonth() + 1;
158                                                 break;
159                                         case "mm":
160                                                 FormattedDate += ("0" + (this.getMonth() + 1)).slice(-2);
161                                                 break;
162                                         case "mmm":
163                                                 FormattedDate += Ref_MonthAbbreviation[this.getMonth()];
164                                                 break;
165                                         case "mmmm":
166                                                 FormattedDate += Ref_MonthFullName[this.getMonth()];
167                                                 break;
168                                         case "yy":
169                                                 FormattedDate += ("0" + this.getFullYear()).slice(-2);
170                                                 break;
171                                         case "yyyy":
172                                                 FormattedDate += ("000" + this.getFullYear()).slice(-4);
173                                                 break;
174                                 };
175                                         // Reset the MaskPart to nothing
176                                 MaskPart = "";
177                         };
178                                 // Add the character to the output
179                         FormattedDate += CurChar;
180                 } else {
181                                 // Add the current mask character to the MaskPart
182                         MaskPart += CurChar;
183                 };
184         };
185
186                 // Remove the temporary space from the end of the formatted date
187         FormattedDate = FormattedDate.substring(0,FormattedDate.length - 1);
188
189                 // Return the formatted date
190         return FormattedDate;
191
192 };
193
194
195 Date.prototype.timeFormat = function(Mask) {
196
197         var FormattedTime = "";
198
199                 // Convert any supported simple masks into "real" masks
200         switch (Mask) {
201                 case "short":
202                         Mask = "h:mm tt";               
203                 break;
204                 case "medium":
205                         Mask = "h:mm:ss tt";
206                 break;
207                 case "long":
208                         Mask = "h:mm:ss.l tt";
209                 break;
210                 case "full":
211                         Mask = "h:mm:ss.l tt";
212                 break;
213         };
214
215                 // Tack a temporary space at the end of the mask to ensure that the last character isn't a mask character
216         Mask += " ";
217
218                 // Parse the Mask
219         var CurChar;
220         var MaskPart = "";
221         for ( var Cnt = 0; Cnt < Mask.length; Cnt++ ) {
222                         // Get the character
223                 CurChar = Mask.charAt(Cnt);
224                         // Determine if the character is a mask element
225                 if ( (CurChar != "h") && (CurChar != "H") && (CurChar != "m") && 
226             (CurChar != "s") && (CurChar != "l") && (CurChar != "t") && (CurChar != "T") ) {
227                                 // Determine if we need to parse a MaskPart or not
228                         if ( MaskPart != "" ) {
229                                         // Convert the mask part to the date value
230                                 switch (MaskPart) {
231                                         case "h":
232                                                 var CurValue = this.getHours();
233                                                 if ( CurValue >  12 ) {
234                                                         CurValue = CurValue - 12;
235                                                 };
236                                                 FormattedTime += CurValue;
237                                                 break;
238                                         case "hh":
239                                                 var CurValue = this.getHours();
240                                                 if ( CurValue >  12 ) {
241                                                         CurValue = CurValue - 12;
242                                                 };
243                                                 FormattedTime += ("0" + CurValue).slice(-2);
244                                                 break;
245                                         case "H":
246                                                 FormattedTime += ("0" + this.getHours()).slice(-2);
247                                                 break;
248                                         case "HH":
249                                                 FormattedTime += ("0" + this.getHours()).slice(-2);
250                                                 break;
251                                         case "m":
252                                                 FormattedTime += this.getMinutes();
253                                                 break;
254                                         case "mm":
255                                                 FormattedTime += ("0" + this.getMinutes()).slice(-2);
256                                                 break;
257                                         case "s":
258                                                 FormattedTime += this.getSeconds();
259                                                 break;
260                                         case "ss":
261                                                 FormattedTime += ("0" + this.getSeconds()).slice(-2);
262                                                 break;
263                                         case "l":
264                                                 FormattedTime += ("00" + this.getMilliseconds()).slice(-3);
265                                                 break;
266                                         case "t":
267                                                 if ( this.getHours() > 12 ) {
268                                                         FormattedTime += "p";
269                                                 } else {
270                                                         FormattedTime += "a";
271                                                 };
272                                                 break;
273                                         case "tt":
274                                                 if ( this.getHours() > 12 ) {
275                                                         FormattedTime += "pm";
276                                                 } else {
277                                                         FormattedTime += "am";
278                                                 };
279                                                 break;
280                                         case "T":
281                                                 if ( this.getHours() > 12 ) {
282                                                         FormattedTime += "P";
283                                                 } else {
284                                                         FormattedTime += "A";
285                                                 };
286                                                 break;
287                                         case "TT":
288                                                 if ( this.getHours() > 12 ) {
289                                                         FormattedTime += "PM";
290                                                 } else {
291                                                         FormattedTime += "AM";
292                                                 };
293                                                 break;
294                                 };
295                                         // Reset the MaskPart to nothing
296                                 MaskPart = "";
297                         };
298                                 // Add the character to the output
299                         FormattedTime += CurChar;
300                 } else {
301                                 // Add the current mask character to the MaskPart
302                         MaskPart += CurChar;
303                 };
304         };
305
306                 // Remove the temporary space from the end of the formatted date
307         FormattedTime = FormattedTime.substring(0,FormattedTime.length - 1);
308
309                 // Return the formatted date
310         return FormattedTime;
311
312 };
313
314
315 /*
316     dropTZ - do not include the timezone in the output.  only used for YMDH+
317     useSpace - if true, use a space instaed of a "T" between date and time
318 */
319 Date.prototype.iso8601Format = function(Style, isUTC, dropTZ, useSpace) {
320
321         var FormattedDate = "";
322
323         switch (Style) {
324                 case "Y":
325                         FormattedDate += this.dateFormat("yyyy");
326                         break;
327                 case "YM":
328                         FormattedDate += this.dateFormat("yyyy-mm");
329                         break;
330                 case "YMD":
331                         FormattedDate += this.dateFormat("yyyy-mm-dd");
332                         break;
333                 case "YMDHM":
334                         FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm");
335                         break;
336                 case "YMDHMS":
337                         FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss");
338                         break;
339                 case "YMDHMSM":
340                         FormattedDate += this.dateFormat("yyyy-mm-dd") + ((useSpace) ? " " : "T") + this.timeFormat("HH:mm:ss.l");
341                         break;
342         };
343
344         if ( !dropTZ && (Style == "YMDHM" || Style == "YMDHMS" || Style == "YMDHMSM") ) {
345                 if ( isUTC ) {
346                         FormattedDate += "Z";
347                 } else {
348                                 // Get TimeZone Information
349                         var TimeZoneOffset = this.getTimezoneOffset();
350                         var TimeZoneInfo = (TimeZoneOffset >= 0 ? "-" : "+") + 
351                 ("0" + (Math.floor(Math.abs(TimeZoneOffset) / 60))).slice(-2) + ":" + 
352                 ("00" + (Math.abs(TimeZoneOffset) % 60)).slice(-2);
353                         FormattedDate += TimeZoneInfo;
354                 };
355         };      
356
357                 // Return the date
358         return FormattedDate;
359
360 };
361
362
363
364