diff options
Diffstat (limited to 'node_modules/montage/core/converter/date-converter.js')
-rwxr-xr-x | node_modules/montage/core/converter/date-converter.js | 2621 |
1 files changed, 2621 insertions, 0 deletions
diff --git a/node_modules/montage/core/converter/date-converter.js b/node_modules/montage/core/converter/date-converter.js new file mode 100755 index 00000000..b00a5b22 --- /dev/null +++ b/node_modules/montage/core/converter/date-converter.js | |||
@@ -0,0 +1,2621 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | @version: 1.0 Alpha-1 | ||
8 | @author: Coolite Inc. http://www.coolite.com/ | ||
9 | @date: 2008-04-13 | ||
10 | @copyright: Copyright (c) 2006-2008, Coolite Inc. (http://www.coolite.com/). All rights reserved. | ||
11 | @license: Licensed under The MIT License. See license.txt and http://www.datejs.com/license/. | ||
12 | @website: http://www.datejs.com/ | ||
13 | */ | ||
14 | /** | ||
15 | @module montage/core/converter/date-converter | ||
16 | @requires montage/core/core | ||
17 | @requires montage/core/converter/converter | ||
18 | */ | ||
19 | var Montage = require("montage").Montage, | ||
20 | Converter = require("core/converter/converter").Converter, | ||
21 | Validator = require("core/converter/converter").Validator; | ||
22 | |||
23 | (function () { | ||
24 | var $D = Date, | ||
25 | $P = $D.prototype, | ||
26 | $C = $D.CultureInfo, | ||
27 | p = function (s, l) { | ||
28 | if (!l) { | ||
29 | l = 2; | ||
30 | } | ||
31 | return ("000" + s).slice(l * -1); | ||
32 | }; | ||
33 | |||
34 | /** | ||
35 | Resets the time of this Date object to 12:00 AM (00:00), which is the start of the day. | ||
36 | @function | ||
37 | @param {Boolean} .clone() this date instance before clearing Time | ||
38 | @return {Date} itself | ||
39 | */ | ||
40 | $P.clearTime = function () { | ||
41 | this.setHours(0); | ||
42 | this.setMinutes(0); | ||
43 | this.setSeconds(0); | ||
44 | this.setMilliseconds(0); | ||
45 | return this; | ||
46 | }; | ||
47 | |||
48 | /** | ||
49 | * Resets the time of this Date object to the current time ('now'). | ||
50 | @function | ||
51 | @return {Date} itself | ||
52 | */ | ||
53 | $P.setTimeToNow = function () { | ||
54 | var n = new Date(); | ||
55 | this.setHours(n.getHours()); | ||
56 | this.setMinutes(n.getMinutes()); | ||
57 | this.setSeconds(n.getSeconds()); | ||
58 | this.setMilliseconds(n.getMilliseconds()); | ||
59 | return this; | ||
60 | }; | ||
61 | |||
62 | /** | ||
63 | 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). | ||
64 | @function | ||
65 | @return {Date} The current date. | ||
66 | */ | ||
67 | $D.today = function () { | ||
68 | return new Date().clearTime(); | ||
69 | }; | ||
70 | |||
71 | /** | ||
72 | Compares the first date to the second date and returns an number indication of their relative values. | ||
73 | @function | ||
74 | @param {Date} First Date object to compare [Required]. | ||
75 | @param {Date} Second Date object to compare to [Required]. | ||
76 | @return {Number} -1 = date1 is lessthan date2. 0 = values are equal. 1 = date1 is greaterthan date2. | ||
77 | */ | ||
78 | $D.compare = function (date1, date2) { | ||
79 | if (isNaN(date1) || isNaN(date2)) { | ||
80 | throw new Error(date1 + " - " + date2); | ||
81 | } else if (date1 instanceof Date && date2 instanceof Date) { | ||
82 | return (date1 < date2) ? -1 : (date1 > date2) ? 1 : 0; | ||
83 | } else { | ||
84 | throw new TypeError(date1 + " - " + date2); | ||
85 | } | ||
86 | }; | ||
87 | |||
88 | /** | ||
89 | Compares the first Date object to the second Date object and returns true if they are equal. | ||
90 | @function | ||
91 | @param {Date} First Date object to compare [Required] | ||
92 | @param {Date} Second Date object to compare to [Required] | ||
93 | @return {Boolean} true if dates are equal. false if they are not equal. | ||
94 | */ | ||
95 | $D.equals = function (date1, date2) { | ||
96 | return (date1.compareTo(date2) === 0); | ||
97 | }; | ||
98 | |||
99 | /** | ||
100 | Gets the day number (0-6) if given a CultureInfo specific string which is a valid dayName, abbreviatedDayName or shortestDayName (two char). | ||
101 | @function | ||
102 | @param {String} The name of the day (eg. "Monday, "Mon", "tuesday", "tue", "We", "we"). | ||
103 | @return {Number} The day number | ||
104 | */ | ||
105 | $D.getDayNumberFromName = function (name) { | ||
106 | var n = $C.dayNames, m = $C.abbreviatedDayNames, o = $C.shortestDayNames, s = name.toLowerCase(); | ||
107 | for (var i = 0; i < n.length; i++) { | ||
108 | if (n[i].toLowerCase() == s || m[i].toLowerCase() == s || o[i].toLowerCase() == s) { | ||
109 | return i; | ||
110 | } | ||
111 | } | ||
112 | return -1; | ||
113 | }; | ||
114 | |||
115 | /** | ||
116 | Gets the month number (0-11) if given a Culture Info specific string which is a valid monthName or abbreviatedMonthName. | ||
117 | @function | ||
118 | @param {String} The name of the month (eg. "February, "Feb", "october", "oct"). | ||
119 | @return {Number} The day number | ||
120 | */ | ||
121 | $D.getMonthNumberFromName = function (name) { | ||
122 | var n = $C.monthNames, m = $C.abbreviatedMonthNames, s = name.toLowerCase(); | ||
123 | for (var i = 0; i < n.length; i++) { | ||
124 | if (n[i].toLowerCase() == s || m[i].toLowerCase() == s) { | ||
125 | return i; | ||
126 | } | ||
127 | } | ||
128 | return -1; | ||
129 | }; | ||
130 | |||
131 | /** | ||
132 | Determines if the current date instance is within a LeapYear. | ||
133 | @function | ||
134 | @param {Number} The year. | ||
135 | @return {Boolean} true if date is within a LeapYear, otherwise false. | ||
136 | */ | ||
137 | $D.isLeapYear = function (year) { | ||
138 | return ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0); | ||
139 | }; | ||
140 | |||
141 | /** | ||
142 | Gets the number of days in the month, given a year and month value. Automatically corrects for LeapYear. | ||
143 | @function | ||
144 | @param {Number} The year. | ||
145 | @param {Number} The month (0-11). | ||
146 | @return {Number} The number of days in the month. | ||
147 | */ | ||
148 | $D.getDaysInMonth = function (year, month) { | ||
149 | return [31, ($D.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]; | ||
150 | }; | ||
151 | |||
152 | /** | ||
153 | @function | ||
154 | @param {Number} offset | ||
155 | @returns null | ||
156 | */ | ||
157 | $D.getTimezoneAbbreviation = function (offset) { | ||
158 | var z = $C.timezones, p; | ||
159 | for (var i = 0; i < z.length; i++) { | ||
160 | if (z[i].offset === offset) { | ||
161 | return z[i].name; | ||
162 | } | ||
163 | } | ||
164 | return null; | ||
165 | }; | ||
166 | /** | ||
167 | @function | ||
168 | @param {String} name | ||
169 | @returns null | ||
170 | */ | ||
171 | $D.getTimezoneOffset = function (name) { | ||
172 | var z = $C.timezones, p; | ||
173 | for (var i = 0; i < z.length; i++) { | ||
174 | if (z[i].name === name.toUpperCase()) { | ||
175 | return z[i].offset; | ||
176 | } | ||
177 | } | ||
178 | return null; | ||
179 | }; | ||
180 | |||
181 | /** | ||
182 | Returns a new Date object that is an exact date and time copy of the original instance. | ||
183 | @function | ||
184 | @return {Date} A new Date instance | ||
185 | */ | ||
186 | $P.clone = function () { | ||
187 | return new Date(this.getTime()); | ||
188 | }; | ||
189 | |||
190 | /** | ||
191 | Compares this instance to a Date object and returns an number indication of their relative values. | ||
192 | @function | ||
193 | @param {Date} Date Object to compare [Required]. | ||
194 | @return {Number} -1 = This is less-than date. 0 = values are equal. 1 = this is greater-than date. | ||
195 | */ | ||
196 | $P.compareTo = function (date) { | ||
197 | return Date.compare(this, date); | ||
198 | }; | ||
199 | |||
200 | /** | ||
201 | Compares this instance to another Date object and returns true if they are equal. | ||
202 | @function | ||
203 | @param {Date} Date Object to compare. If no date to compare, new Date() [now] is used. | ||
204 | @return {Boolean} true If dates are equal. False if they are not equal. | ||
205 | */ | ||
206 | $P.equals = function (date) { | ||
207 | return Date.equals(this, date || new Date()); | ||
208 | }; | ||
209 | |||
210 | /** | ||
211 | Determines if this instance is between a range of two dates or equal to either the start or end dates. | ||
212 | @function | ||
213 | @param {Date} start Of range [Required] | ||
214 | @param {Date} end Of range [Required] | ||
215 | @return {Boolean} true This is between or equal to the start and end dates, else false. | ||