Mercurial > public > madeira
annotate media/django/js/timeparse.js @ 1:0dcfcdf50c62
Initial import of Madeira project from the private repository.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 06 Apr 2009 03:10:59 +0000 |
parents | |
children |
rev | line source |
---|---|
bgneal@1 | 1 var timeParsePatterns = [ |
bgneal@1 | 2 // 9 |
bgneal@1 | 3 { re: /^\d{1,2}$/i, |
bgneal@1 | 4 handler: function(bits) { |
bgneal@1 | 5 if (bits[0].length == 1) { |
bgneal@1 | 6 return '0' + bits[0] + ':00'; |
bgneal@1 | 7 } else { |
bgneal@1 | 8 return bits[0] + ':00'; |
bgneal@1 | 9 } |
bgneal@1 | 10 } |
bgneal@1 | 11 }, |
bgneal@1 | 12 // 13:00 |
bgneal@1 | 13 { re: /^\d{2}[:.]\d{2}$/i, |
bgneal@1 | 14 handler: function(bits) { |
bgneal@1 | 15 return bits[0].replace('.', ':'); |
bgneal@1 | 16 } |
bgneal@1 | 17 }, |
bgneal@1 | 18 // 9:00 |
bgneal@1 | 19 { re: /^\d[:.]\d{2}$/i, |
bgneal@1 | 20 handler: function(bits) { |
bgneal@1 | 21 return '0' + bits[0].replace('.', ':'); |
bgneal@1 | 22 } |
bgneal@1 | 23 }, |
bgneal@1 | 24 // 3 am / 3 a.m. / 3am |
bgneal@1 | 25 { re: /^(\d+)\s*([ap])(?:.?m.?)?$/i, |
bgneal@1 | 26 handler: function(bits) { |
bgneal@1 | 27 var hour = parseInt(bits[1]); |
bgneal@1 | 28 if (hour == 12) { |
bgneal@1 | 29 hour = 0; |
bgneal@1 | 30 } |
bgneal@1 | 31 if (bits[2].toLowerCase() == 'p') { |
bgneal@1 | 32 if (hour == 12) { |
bgneal@1 | 33 hour = 0; |
bgneal@1 | 34 } |
bgneal@1 | 35 return (hour + 12) + ':00'; |
bgneal@1 | 36 } else { |
bgneal@1 | 37 if (hour < 10) { |
bgneal@1 | 38 return '0' + hour + ':00'; |
bgneal@1 | 39 } else { |
bgneal@1 | 40 return hour + ':00'; |
bgneal@1 | 41 } |
bgneal@1 | 42 } |
bgneal@1 | 43 } |
bgneal@1 | 44 }, |
bgneal@1 | 45 // 3.30 am / 3:15 a.m. / 3.00am |
bgneal@1 | 46 { re: /^(\d+)[.:](\d{2})\s*([ap]).?m.?$/i, |
bgneal@1 | 47 handler: function(bits) { |
bgneal@1 | 48 var hour = parseInt(bits[1]); |
bgneal@1 | 49 var mins = parseInt(bits[2]); |
bgneal@1 | 50 if (mins < 10) { |
bgneal@1 | 51 mins = '0' + mins; |
bgneal@1 | 52 } |
bgneal@1 | 53 if (hour == 12) { |
bgneal@1 | 54 hour = 0; |
bgneal@1 | 55 } |
bgneal@1 | 56 if (bits[3].toLowerCase() == 'p') { |
bgneal@1 | 57 if (hour == 12) { |
bgneal@1 | 58 hour = 0; |
bgneal@1 | 59 } |
bgneal@1 | 60 return (hour + 12) + ':' + mins; |
bgneal@1 | 61 } else { |
bgneal@1 | 62 if (hour < 10) { |
bgneal@1 | 63 return '0' + hour + ':' + mins; |
bgneal@1 | 64 } else { |
bgneal@1 | 65 return hour + ':' + mins; |
bgneal@1 | 66 } |
bgneal@1 | 67 } |
bgneal@1 | 68 } |
bgneal@1 | 69 }, |
bgneal@1 | 70 // noon |
bgneal@1 | 71 { re: /^no/i, |
bgneal@1 | 72 handler: function(bits) { |
bgneal@1 | 73 return '12:00'; |
bgneal@1 | 74 } |
bgneal@1 | 75 }, |
bgneal@1 | 76 // midnight |
bgneal@1 | 77 { re: /^mid/i, |
bgneal@1 | 78 handler: function(bits) { |
bgneal@1 | 79 return '00:00'; |
bgneal@1 | 80 } |
bgneal@1 | 81 } |
bgneal@1 | 82 ]; |
bgneal@1 | 83 |
bgneal@1 | 84 function parseTimeString(s) { |
bgneal@1 | 85 for (var i = 0; i < timeParsePatterns.length; i++) { |
bgneal@1 | 86 var re = timeParsePatterns[i].re; |
bgneal@1 | 87 var handler = timeParsePatterns[i].handler; |
bgneal@1 | 88 var bits = re.exec(s); |
bgneal@1 | 89 if (bits) { |
bgneal@1 | 90 return handler(bits); |
bgneal@1 | 91 } |
bgneal@1 | 92 } |
bgneal@1 | 93 return s; |
bgneal@1 | 94 } |