comparison 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
comparison
equal deleted inserted replaced
0:df0370bfe3f0 1:0dcfcdf50c62
1 var timeParsePatterns = [
2 // 9
3 { re: /^\d{1,2}$/i,
4 handler: function(bits) {
5 if (bits[0].length == 1) {
6 return '0' + bits[0] + ':00';
7 } else {
8 return bits[0] + ':00';
9 }
10 }
11 },
12 // 13:00
13 { re: /^\d{2}[:.]\d{2}$/i,
14 handler: function(bits) {
15 return bits[0].replace('.', ':');
16 }
17 },
18 // 9:00
19 { re: /^\d[:.]\d{2}$/i,
20 handler: function(bits) {
21 return '0' + bits[0].replace('.', ':');
22 }
23 },
24 // 3 am / 3 a.m. / 3am
25 { re: /^(\d+)\s*([ap])(?:.?m.?)?$/i,
26 handler: function(bits) {
27 var hour = parseInt(bits[1]);
28 if (hour == 12) {
29 hour = 0;
30 }
31 if (bits[2].toLowerCase() == 'p') {
32 if (hour == 12) {
33 hour = 0;
34 }
35 return (hour + 12) + ':00';
36 } else {
37 if (hour < 10) {
38 return '0' + hour + ':00';
39 } else {
40 return hour + ':00';
41 }
42 }
43 }
44 },
45 // 3.30 am / 3:15 a.m. / 3.00am
46 { re: /^(\d+)[.:](\d{2})\s*([ap]).?m.?$/i,
47 handler: function(bits) {
48 var hour = parseInt(bits[1]);
49 var mins = parseInt(bits[2]);
50 if (mins < 10) {
51 mins = '0' + mins;
52 }
53 if (hour == 12) {
54 hour = 0;
55 }
56 if (bits[3].toLowerCase() == 'p') {
57 if (hour == 12) {
58 hour = 0;
59 }
60 return (hour + 12) + ':' + mins;
61 } else {
62 if (hour < 10) {
63 return '0' + hour + ':' + mins;
64 } else {
65 return hour + ':' + mins;
66 }
67 }
68 }
69 },
70 // noon
71 { re: /^no/i,
72 handler: function(bits) {
73 return '12:00';
74 }
75 },
76 // midnight
77 { re: /^mid/i,
78 handler: function(bits) {
79 return '00:00';
80 }
81 }
82 ];
83
84 function parseTimeString(s) {
85 for (var i = 0; i < timeParsePatterns.length; i++) {
86 var re = timeParsePatterns[i].re;
87 var handler = timeParsePatterns[i].handler;
88 var bits = re.exec(s);
89 if (bits) {
90 return handler(bits);
91 }
92 }
93 return s;
94 }