source: code/Website/open-flash-chart/string/DateUtils.as

Last change on this file was 7849, checked in by dennisw, 15 years ago
File size: 8.7 KB
Line 
1package string
2{
3 public class DateUtils
4 {
5
6 protected static var dateConsts:Object = {
7 shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
8 longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
9 shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
10 longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
11 }
12
13 public static function replace_magic_values( tip:String, xVal:Number):String {
14 // convert from a unix timestamp to an AS3 date
15 var as3Date:Date = new Date(xVal * 1000);
16 tip = tip.replace('#date#', DateUtils.formatDate(as3Date, "Y-m-d"));
17 // check for user formatted dates
18 var begPtr:int = tip.indexOf("#date:");
19 while (begPtr >= 0)
20 {
21 var endPtr:int = tip.indexOf("#", begPtr + 1) + 1;
22 var replaceStr:String = tip.substr(begPtr, endPtr-begPtr);
23 var timeFmt:String = replaceStr.substr(6, replaceStr.length - 7);
24 var dateStr:String = DateUtils.formatDate(as3Date, timeFmt);
25 tip = tip.replace(replaceStr, dateStr);
26 begPtr = tip.indexOf("#date:");
27 }
28
29 begPtr = tip.indexOf("#gmdate:");
30 while (begPtr >= 0)
31 {
32 endPtr = tip.indexOf("#", begPtr + 1) + 1;
33 replaceStr = tip.substr(begPtr, endPtr-begPtr);
34 timeFmt = replaceStr.substr(8, replaceStr.length - 9);
35 dateStr= DateUtils.formatUTCDate(as3Date, timeFmt);
36 tip = tip.replace(replaceStr, dateStr);
37 begPtr = tip.indexOf("#gmdate:");
38 }
39
40 return tip;
41 }
42
43 // Simulates PHP's date function
44 public static function formatDate( aDate:Date, fmt:String ): String
45 {
46 var returnStr:String = '';
47 for (var i:int = 0; i < fmt.length; i++) {
48 var curChar:String = fmt.charAt(i);
49 switch (curChar)
50 {
51 // day
52 case 'd':
53 returnStr += (aDate.getDate() < 10 ? '0' : '') + aDate.getDate();
54 break;
55 case 'D':
56 returnStr += DateUtils.dateConsts.shortDays[aDate.getDate()];
57 break;
58 case 'j':
59 returnStr += aDate.getDate();
60 break;
61 case 'l':
62 returnStr += DateUtils.dateConsts.longDays[aDate.getDay()];
63 break;
64 case 'N':
65 returnStr += aDate.getDay() + 1;
66 break;
67 case 'S':
68 returnStr += (aDate.getDate() % 10 == 1 && aDate.getDate() != 11 ? 'st' : (aDate.getDate() % 10 == 2 && aDate.getDate() != 12 ? 'nd' : (aDate.getDate() % 10 == 3 && aDate.getDate() != 13 ? 'rd' : 'th')));
69 break;
70 case 'w':
71 returnStr += aDate.getDay();
72 break;
73 //z: function() { return "Not Yet Supported"; },
74
75 // Week
76 //W: function() { return "Not Yet Supported"; },
77
78 // Month
79 case 'F':
80 returnStr += DateUtils.dateConsts.longMonths[aDate.getMonth()];
81 break;
82 case 'm':
83 returnStr += (aDate.getMonth() < 9 ? '0' : '') + (aDate.getMonth() + 1);
84 break;
85 case 'M':
86 returnStr += DateUtils.dateConsts.shortMonths[aDate.getMonth()];
87 break;
88 case 'n':
89 returnStr += aDate.getMonth() + 1;
90 break;
91 //t: function() { return "Not Yet Supported"; },
92
93 // Year
94 //L: function() { return "Not Yet Supported"; },
95 //o: function() { return "Not Supported"; },
96 case 'Y':
97 returnStr += aDate.getFullYear();
98 break;
99 case 'y':
100 returnStr += ('' + aDate.getFullYear()).substr(2);
101 break;
102
103 // Time
104 case 'a':
105 returnStr += aDate.getHours() < 12 ? 'am' : 'pm';
106 break;
107 case 'A':
108 returnStr += aDate.getHours() < 12 ? 'AM' : 'PM';
109 break;
110 //B: function() { return "Not Yet Supported"; },
111 case 'g':
112 returnStr += aDate.getHours() == 0 ? 12 : (aDate.getHours() > 12 ? aDate.getHours() - 12 : aDate.getHours());
113 break;
114 case 'G':
115 returnStr += aDate.getHours();
116 break;
117 case 'h':
118 returnStr += (aDate.getHours() < 10 || (12 < aDate.getHours() < 22) ? '0' : '') + (aDate.getHours() < 10 ? aDate.getHours() + 1 : aDate.getHours() - 12);
119 break;
120 case 'H':
121 returnStr += (aDate.getHours() < 10 ? '0' : '') + aDate.getHours();
122 break;
123 case 'i':
124 returnStr += (aDate.getMinutes() < 10 ? '0' : '') + aDate.getMinutes();
125 break;
126 case 's':
127 returnStr += (aDate.getSeconds() < 10 ? '0' : '') + aDate.getSeconds();
128 break;
129
130 // Timezone
131 //e: function() { return "Not Yet Supported"; },
132 //I: function() { return "Not Supported"; },
133 case 'O':
134 returnStr += (aDate.getTimezoneOffset() < 0 ? '-' : '+') + (aDate.getTimezoneOffset() / 60 < 10 ? '0' : '') + (aDate.getTimezoneOffset() / 60) + '00';
135 break;
136 //T: function() { return "Not Yet Supported"; },
137 case 'Z':
138 returnStr += aDate.getTimezoneOffset() * 60;
139 break;
140
141 // Full Date/Time
142 //c: function() { return "Not Yet Supported"; },
143 case 'r':
144 returnStr += aDate.toString();
145 break;
146 case 'U':
147 returnStr += aDate.getTime() / 1000;
148 break;
149
150 default:
151 returnStr += curChar;
152 }
153 }
154 return returnStr;
155 };
156
157 // Simulates PHP's date function
158 public static function formatUTCDate( aDate:Date, fmt:String ): String
159 {
160 var returnStr:String = '';
161 for (var i:int = 0; i < fmt.length; i++) {
162 var curChar:String = fmt.charAt(i);
163 switch (curChar)
164 {
165 // day
166 case 'd':
167 returnStr += (aDate.getUTCDate() < 10 ? '0' : '') + aDate.getUTCDate();
168 break;
169 case 'D':
170 returnStr += DateUtils.dateConsts.shortDays[aDate.getUTCDate()];
171 break;
172 case 'j':
173 returnStr += aDate.getUTCDate();
174 break;
175 case 'l':
176 returnStr += DateUtils.dateConsts.longDays[aDate.getUTCDay()];
177 break;
178 case 'N':
179 returnStr += aDate.getUTCDay() + 1;
180 break;
181 case 'S':
182 returnStr += (aDate.getUTCDate() % 10 == 1 && aDate.getUTCDate() != 11 ? 'st' : (aDate.getUTCDate() % 10 == 2 && aDate.getUTCDate() != 12 ? 'nd' : (aDate.getUTCDate() % 10 == 3 && aDate.getUTCDate() != 13 ? 'rd' : 'th')));
183 break;
184 case 'w':
185 returnStr += aDate.getUTCDay();
186 break;
187 //z: function() { return "Not Yet Supported"; },
188
189 // Week
190 //W: function() { return "Not Yet Supported"; },
191
192 // Month
193 case 'F':
194 returnStr += DateUtils.dateConsts.longMonths[aDate.getUTCMonth()];
195 break;
196 case 'm':
197 returnStr += (aDate.getUTCMonth() < 9 ? '0' : '') + (aDate.getUTCMonth() + 1);
198 break;
199 case 'M':
200 returnStr += DateUtils.dateConsts.shortMonths[aDate.getUTCMonth()];
201 break;
202 case 'n':
203 returnStr += aDate.getUTCMonth() + 1;
204 break;
205 //t: function() { return "Not Yet Supported"; },
206
207 // Year
208 //L: function() { return "Not Yet Supported"; },
209 //o: function() { return "Not Supported"; },
210 case 'Y':
211 returnStr += aDate.getUTCFullYear();
212 break;
213 case 'y':
214 returnStr += ('' + aDate.getUTCFullYear()).substr(2);
215 break;
216
217 // Time
218 case 'a':
219 returnStr += aDate.getUTCHours() < 12 ? 'am' : 'pm';
220 break;
221 case 'A':
222 returnStr += aDate.getUTCHours() < 12 ? 'AM' : 'PM';
223 break;
224 //B: function() { return "Not Yet Supported"; },
225 case 'g':
226 returnStr += aDate.getUTCHours() == 0 ? 12 : (aDate.getUTCHours() > 12 ? aDate.getUTCHours() - 12 : aDate.getHours());
227 break;
228 case 'G':
229 returnStr += aDate.getUTCHours();
230 break;
231 case 'h':
232 returnStr += (aDate.getUTCHours() < 10 || (12 < aDate.getUTCHours() < 22) ? '0' : '') + (aDate.getUTCHours() < 10 ? aDate.getUTCHours() + 1 : aDate.getUTCHours() - 12);
233 break;
234 case 'H':
235 returnStr += (aDate.getUTCHours() < 10 ? '0' : '') + aDate.getUTCHours();
236 break;
237 case 'i':
238 returnStr += (aDate.getUTCMinutes() < 10 ? '0' : '') + aDate.getUTCMinutes();
239 break;
240 case 's':
241 returnStr += (aDate.getUTCSeconds() < 10 ? '0' : '') + aDate.getUTCSeconds();
242 break;
243
244 // Timezone
245 //e: function() { return "Not Yet Supported"; },
246 //I: function() { return "Not Supported"; },
247 case 'O':
248 returnStr += '+0000';
249 break;
250 //T: function() { return "Not Yet Supported"; },
251 case 'Z':
252 returnStr += 0;
253 break;
254
255 // Full Date/Time
256 //c: function() { return "Not Yet Supported"; },
257 case 'r':
258 returnStr += aDate.toUTCString();
259 break;
260 case 'U':
261 returnStr += aDate.getTime() / 1000;
262 break;
263
264 default:
265 returnStr += curChar;
266 }
267 }
268 return returnStr;
269 };
270
271
272 }
273}
Note: See TracBrowser for help on using the repository browser.