bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
SimpleTimeParser.cc
1
2// 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
34using std::map;
35using std::string;
36using std::istringstream;
37
38namespace agg_util {
39const long SimpleTimeParser::_sSecsInMin = 60L;
40const long SimpleTimeParser::_sSecsInHour = 60L * SimpleTimeParser::_sSecsInMin;
41const long SimpleTimeParser::_sSecsInDay = 24L * SimpleTimeParser::_sSecsInHour;
42const long SimpleTimeParser::_sSecsInWeek = 7L * SimpleTimeParser::_sSecsInDay;
43const long SimpleTimeParser::_sSecsInMonth = 31L * SimpleTimeParser::_sSecsInDay;
44const long SimpleTimeParser::_sSecsInYear = 365L * SimpleTimeParser::_sSecsInDay;
45
46map<string, long> SimpleTimeParser::_sParseTable = std::map<string, long>();
47bool SimpleTimeParser::_sInited = false;
48
49SimpleTimeParser::SimpleTimeParser()
50{
51}
52
53SimpleTimeParser::~SimpleTimeParser()
54{
55}
56
57bool SimpleTimeParser::parseIntoSeconds(long& seconds, const string& duration)
58{
59 bool success = true;
60
61 if (!_sInited) {
62 initParseTable();
63 }
64
65 istringstream iss;
66 iss.str(duration);
67 iss >> seconds;
68 if (iss.fail()) {
69 success = false;
70 }
71 else // we got the numerical portion, now parse the units.
72 {
73 string units;
74 iss >> units;
75 if (iss.fail()) {
76 success = false;
77 }
78 else {
79 std::map<std::string, long>::iterator foundIt = _sParseTable.find(units);
80 if (foundIt == _sParseTable.end()) {
81 success = false;
82 }
83 else {
84 seconds *= foundIt->second;
85 }
86 }
87 }
88
89 if (!success) {
90 seconds = -1;
91 }
92 return success;
93}
94
95void SimpleTimeParser::initParseTable()
96{
97 /*
98 * seconds: { s, sec, secs, second, seconds }
99 * minutes: { m, min, mins, minute, minutes }
100 * hours: { h, hour, hours }
101 * days: { day, days }
102 * months: { month, months }
103 * years: { year, years }
104 */
105
106 _sParseTable["s"] = 1L;
107 _sParseTable["sec"] = 1L;
108 _sParseTable["secs"] = 1L;
109 _sParseTable["second"] = 1L;
110 _sParseTable["seconds"] = 1L;
111
112 _sParseTable["m"] = _sSecsInMin;
113 _sParseTable["min"] = _sSecsInMin;
114 _sParseTable["mins"] = _sSecsInMin;
115 _sParseTable["minute"] = _sSecsInMin;
116 _sParseTable["minutes"] = _sSecsInMin;
117
118 _sParseTable["h"] = _sSecsInHour;
119 _sParseTable["hour"] = _sSecsInHour;
120 _sParseTable["hours"] = _sSecsInHour;
121
122 _sParseTable["day"] = _sSecsInDay;
123 _sParseTable["days"] = _sSecsInDay;
124
125 _sParseTable["week"] = _sSecsInWeek;
126 _sParseTable["weeks"] = _sSecsInWeek;
127
128 _sParseTable["month"] = _sSecsInMonth;
129 _sParseTable["months"] = _sSecsInMonth;
130
131 _sParseTable["year"] = _sSecsInYear;
132 _sParseTable["years"] = _sSecsInYear;
133
134 _sInited = true;
135}
136
137}
static bool parseIntoSeconds(long &seconds, const std::string &duration)
Helper class for temporarily hijacking an existing dhi to load a DDX response for one particular file...