OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
SimpleTimeParser.cc
Go to the documentation of this file.
1 // This file is part of the "NcML Module" project, a BES module designed
3 // to allow NcML files to be used to be used as a wrapper to add
4 // AIS to existing datasets of any format.
5 //
6 // Copyright (c) 2009 OPeNDAP, Inc.
7 // Author: Michael Johnson <m.johnson@opendap.org>
8 //
9 // For more information, please also see the main website: http://opendap.org/
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License along with this library; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //
25 // Please see the files COPYING and COPYRIGHT for more information on the GLPL.
26 //
27 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
29 
30 #include "SimpleTimeParser.h"
31 
32 #include <sstream>
33 
34 using std::map;
35 using std::string;
36 using std::istringstream;
37 
38 namespace agg_util
39 {
40  const long SimpleTimeParser::_sSecsInMin = 60L;
41  const long SimpleTimeParser::_sSecsInHour = 60L * SimpleTimeParser::_sSecsInMin;
42  const long SimpleTimeParser::_sSecsInDay = 24L * SimpleTimeParser::_sSecsInHour;
43  const long SimpleTimeParser::_sSecsInWeek = 7L * SimpleTimeParser::_sSecsInDay;
44  const long SimpleTimeParser::_sSecsInMonth = 31L * SimpleTimeParser::_sSecsInDay;
45  const long SimpleTimeParser::_sSecsInYear = 365L * SimpleTimeParser::_sSecsInDay;
46 
47  map< string, long > SimpleTimeParser::_sParseTable = std::map<string,long>();
48  bool SimpleTimeParser::_sInited = false;
49 
51  {
52  }
53 
55  {
56  }
57 
58  bool
59  SimpleTimeParser::parseIntoSeconds(long& seconds, const string& duration)
60  {
61  bool success = true;
62 
63  if (!_sInited)
64  {
65  initParseTable();
66  }
67 
68  istringstream iss;
69  iss.str(duration);
70  iss >> seconds;
71  if (iss.fail())
72  {
73  success = false;
74  }
75  else // we got the numerical portion, now parse the units.
76  {
77  string units;
78  iss >> units;
79  if (iss.fail())
80  {
81  success = false;
82  }
83  else
84  {
85  std::map< std::string, long >::iterator foundIt = _sParseTable.find(units);
86  if (foundIt == _sParseTable.end())
87  {
88  success = false;
89  }
90  else
91  {
92  seconds *= foundIt->second;
93  }
94  }
95  }
96 
97  if (!success)
98  {
99  seconds = -1;
100  }
101  return success;
102  }
103 
104 
105 
106  void
107  SimpleTimeParser::initParseTable()
108  {
109  /*
110  * seconds: { s, sec, secs, second, seconds }
111  * minutes: { m, min, mins, minute, minutes }
112  * hours: { h, hour, hours }
113  * days: { day, days }
114  * months: { month, months }
115  * years: { year, years }
116  */
117 
118  _sParseTable["s"] = 1L;
119  _sParseTable["sec"] = 1L;
120  _sParseTable["secs"] = 1L;
121  _sParseTable["second"] = 1L;
122  _sParseTable["seconds"] = 1L;
123 
124  _sParseTable["m"] = _sSecsInMin;
125  _sParseTable["min"] = _sSecsInMin;
126  _sParseTable["mins"] = _sSecsInMin;
127  _sParseTable["minute"] = _sSecsInMin;
128  _sParseTable["minutes"] = _sSecsInMin;
129 
130  _sParseTable["h"] = _sSecsInHour;
131  _sParseTable["hour"] = _sSecsInHour;
132  _sParseTable["hours"] = _sSecsInHour;
133 
134  _sParseTable["day"] = _sSecsInDay;
135  _sParseTable["days"] = _sSecsInDay;
136 
137  _sParseTable["week"] = _sSecsInWeek;
138  _sParseTable["weeks"] = _sSecsInWeek;
139 
140  _sParseTable["month"] = _sSecsInMonth;
141  _sParseTable["months"] = _sSecsInMonth;
142 
143  _sParseTable["year"] = _sSecsInYear;
144  _sParseTable["years"] = _sSecsInYear;
145 
146  _sInited = true;
147  }
148 
149 }
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...
#define L
Definition: avltree.h:36
static bool parseIntoSeconds(long &seconds, const std::string &duration)
Parse the string in duration and to calculate the (approximate) number of seconds it represents...