Blame view

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