|
1 // Copyright 2009 Google Inc. All Rights Reserved. |
|
2 |
|
3 var RFC822Date = {}; |
|
4 |
|
5 /** |
|
6 * Return a DateTime in RFC822 format. |
|
7 * @see http://www.w3.org/Protocols/rfc822/#z28 |
|
8 * @param {Date} date A Date object. |
|
9 * @param {string} opt_tzo The timezone offset. |
|
10 */ |
|
11 RFC822Date.format = function(date, opt_tzo) { |
|
12 var tzo = opt_tzo || RFC822Date.getTZO(date.getTimezoneOffset()); |
|
13 var rfc822Date = RFC822Date.DAYS[date.getDay()] + ', '; |
|
14 rfc822Date += RFC822Date.padZero(date.getDate()) + ' '; |
|
15 rfc822Date += RFC822Date.MONTHS[date.getMonth()] + ' '; |
|
16 rfc822Date += date.getFullYear() + ' '; |
|
17 rfc822Date += RFC822Date.padZero(date.getHours()) + ':'; |
|
18 rfc822Date += RFC822Date.padZero(date.getMinutes()) + ':'; |
|
19 rfc822Date += RFC822Date.padZero(date.getSeconds()) + ' ' ; |
|
20 rfc822Date += tzo; |
|
21 return rfc822Date; |
|
22 }; |
|
23 |
|
24 |
|
25 /** |
|
26 * @type {Array} |
|
27 */ |
|
28 RFC822Date.MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
|
29 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; |
|
30 |
|
31 |
|
32 /** |
|
33 * @type {Array} |
|
34 */ |
|
35 RFC822Date.DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
|
36 |
|
37 |
|
38 /** |
|
39 * Pads a value with a 0 if it is less than 10; |
|
40 * @param {number|string} |
|
41 * @return {string} |
|
42 */ |
|
43 RFC822Date.padZero = function(val) { |
|
44 val = val + ''; // cast into string |
|
45 if (val.length < 2) { |
|
46 val = '0' + val; |
|
47 } |
|
48 return val; |
|
49 }; |
|
50 |
|
51 |
|
52 /** |
|
53 * Returns a timezone offset in the format +|-dddd. |
|
54 * @param {String} tzo A time zone offset from GMT in minutes. |
|
55 * @return {string} The time zone offset as a string. |
|
56 */ |
|
57 RFC822Date.getTZO = function(tzo) { |
|
58 var hours = Math.floor(tzo / 60); |
|
59 var tzoFormatted = hours > 0 ? '-' : '+'; |
|
60 |
|
61 var absoluteHours = Math.abs(hours); |
|
62 tzoFormatted += absoluteHours < 10 ? '0' : ''; |
|
63 tzoFormatted += absoluteHours; |
|
64 |
|
65 var moduloMinutes = Math.abs(tzo % 60); |
|
66 tzoFormatted += moduloMinutes == 0 ? '00' : moduloMinutes |
|
67 |
|
68 return tzoFormatted; |
|
69 }; |
|
70 |