Skip to content

Commit b1843f0

Browse files
committed
added to-by support for stepranges
1 parent 0fcf92b commit b1843f0

File tree

6 files changed

+81
-47
lines changed

6 files changed

+81
-47
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.11.9
1+
1.11.10

src/metkit/mars/StepRange.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ void StepRange::print(std::ostream& s) const
102102
eckit::Time t{static_cast<long>(to_*3600.), true};
103103

104104
TimeUnit unit = std::min(maxUnit(f), maxUnit(t));
105-
s << canonical(f, unit) << '-' << canonical(t, unit);
105+
// s << canonical(f, unit) << '-' << canonical(t, unit);
106+
s << canonical(f) << '-' << canonical(t);
106107
}
107108
}
108109

src/metkit/mars/StepRange.h

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ class StepRange {
6060

6161
operator std::string() const;
6262

63+
StepRange& operator+=(const eckit::Time& step) {
64+
from_ += step/3600.;
65+
to_ += step/3600.;
66+
return *this;
67+
}
68+
6369
bool operator==(const StepRange& other) const
6470
{ return from_ == other.from_ && to_ == other.to_; }
6571

@@ -69,6 +75,9 @@ class StepRange {
6975
bool operator<(const StepRange& other) const
7076
{ return (from_ != other.from_)?(from_<other.from_):(to_<other.to_); }
7177

78+
bool operator<=(const StepRange& other) const
79+
{ return (from_ != other.from_)?(from_<=other.from_):(to_<=other.to_); }
80+
7281
bool operator>(const StepRange& other) const
7382
{ return other < *this; }
7483

src/metkit/mars/TypeRange.cc

+14-11
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ void TypeRange::print(std::ostream &out) const {
4040
out << "TypeRange[name=" << name_ << "]";
4141
}
4242

43-
bool TypeRange::expand(const MarsExpandContext& ctx, std::string& value) const {
44-
43+
StepRange TypeRange::parse(std::string& value) const {
4544
eckit::Tokenizer parse("-");
4645
std::vector<std::string> result;
4746

4847
parse(value, result);
4948
switch (result.size()) {
5049
case 1: {
51-
value = StepRange(eckit::Time(result[0], true));
52-
return true;
50+
return StepRange(eckit::Time(result[0], true));
5351
}
5452
case 2: {
5553
eckit::Time start = eckit::Time(result[0], true);
@@ -60,15 +58,20 @@ bool TypeRange::expand(const MarsExpandContext& ctx, std::string& value) const {
6058
oss << name_ + ": initial value " << start << " cannot be greater that final value " << end;
6159
throw eckit::BadValue(oss.str());
6260
}
63-
value = StepRange(start, end);
64-
return true;
61+
return StepRange(start, end);
6562
}
6663
default:
6764
std::ostringstream oss;
6865
oss << name_ + ": invalid value " << value << " " << result.size();
6966
throw eckit::BadValue(oss.str());
70-
}
71-
return false;
67+
}
68+
}
69+
70+
bool TypeRange::expand(const MarsExpandContext& ctx, std::string& value) const {
71+
72+
value = parse(value);
73+
return true;
74+
7275
}
7376

7477
void TypeRange::expand(const MarsExpandContext& ctx, std::vector<std::string>& values) const {
@@ -93,10 +96,10 @@ void TypeRange::expand(const MarsExpandContext& ctx, std::vector<std::string>& v
9396
throw eckit::BadValue(oss.str());
9497
}
9598

96-
eckit::Time from = eckit::Time(values[i - 1], true);
99+
StepRange from = parse(values[i - 1]);
97100
// unit = maxUnit(from);
98101

99-
eckit::Time to = eckit::Time(values[i + 1], true);
102+
StepRange to = parse(values[i + 1]);
100103
eckit::Time by = by_;
101104

102105
if (i+2 < values.size() && eckit::StringTools::lower(values[i + 2]) == "by") {
@@ -121,7 +124,7 @@ void TypeRange::expand(const MarsExpandContext& ctx, std::vector<std::string>& v
121124
oss << name_ + ": 'by' value " << by << " must be a positive number";
122125
throw eckit::BadValue(name_ + ": 'by' value must be a positive number");
123126
}
124-
eckit::Time j = from;
127+
StepRange j = from;
125128
j += by;
126129
for (; j <= to; j += by) {
127130
newval.emplace_back(StepRange(j));

src/metkit/mars/TypeRange.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
namespace metkit {
2020
namespace mars {
2121

22+
class StepRange;
2223
//----------------------------------------------------------------------------------------------------------------------
2324

2425
class TypeRange : public Type {
@@ -36,6 +37,10 @@ class TypeRange : public Type {
3637
virtual void expand(const MarsExpandContext& ctx,
3738
std::vector<std::string>& values) const override;
3839

40+
StepRange parse(std::string& value) const;
41+
42+
private: // attributes
43+
3944
eckit::Time by_;
4045

4146
};

tests/test_expand.cc

+50-34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "metkit/mars/Type.h"
2121

2222
#include "eckit/testing/Test.h"
23+
#include "eckit/utils/Tokenizer.h"
2324

2425
using namespace eckit::testing;
2526

@@ -233,44 +234,59 @@ CASE( "test_metkit_expand_12_time" ) {
233234
void stepThrows(std::vector<std::string> values) {
234235
expandKeyThrows("step", values);
235236
}
236-
void step(std::vector<std::string> values, std::vector<std::string> expected) {
237+
void step(std::string valuesStr, std::string expectedStr) {
238+
eckit::Tokenizer parse("/");
239+
std::vector<std::string> values;
240+
std::vector<std::string> expected;
241+
242+
parse(valuesStr, values);
243+
parse(expectedStr, expected);
244+
237245
expandKey("step", values, expected);
238246
}
239247

240248
CASE( "test_metkit_expand_13_step" ) {
241-
step({"0"}, {"0"});
242-
step({"1"}, {"1"});
243-
step({"24"}, {"24"});
244-
step({"144"}, {"144"});
245-
step({"012"}, {"12"});
246-
step({"12:30"}, {"12h30m"});
247-
step({"1:00"}, {"1"});
248-
step({"1:0:0"}, {"1"});
249-
step({"1h"}, {"1"});
250-
step({"60m"}, {"1"});
251-
step({"1h60m"}, {"2"});
252-
step({"0:5"}, {"5m"});
253-
step({"0:05"}, {"5m"});
254-
step({"0:05:0"}, {"5m"});
255-
step({"0:06"}, {"6m"});
256-
step({"0:10"}, {"10m"});
257-
step({"0:12"}, {"12m"});
258-
step({"0:15"}, {"15m"});
259-
step({"0:20"}, {"20m"});
260-
step({"0:25"}, {"25m"});
261-
step({"0:30"}, {"30m"});
262-
step({"0:35"}, {"35m"});
263-
step({"0:40"}, {"40m"});
264-
step({"0:45"}, {"45m"});
265-
step({"0:50"}, {"50m"});
266-
step({"0:55"}, {"55m"});
267-
step({"0-24"}, {"0-24"});
268-
step({"0-24s"}, {"0s-24s"});
269-
step({"0-120s"}, {"0m-2m"});
270-
step({"0s-120m"}, {"0-2"});
271-
step({"1-2"}, {"1-2"});
272-
step({"30m-1"}, {"30m-60m"});
273-
249+
step("0", "0");
250+
step("1", "1");
251+
step("24", "24");
252+
step("144", "144");
253+
step("012", "12");
254+
step("12:30", "12h30m");
255+
step("1:00", "1");
256+
step("1:0:0", "1");
257+
step("1h", "1");
258+
step("60m", "1");
259+
step("1h60m", "2");
260+
step("0:5", "5m");
261+
step("0:05", "5m");
262+
step("0:05:0", "5m");
263+
step("0:06", "6m");
264+
step("0:10", "10m");
265+
step("0:12", "12m");
266+
step("0:15", "15m");
267+
step("0:20", "20m");
268+
step("0:25", "25m");
269+
step("0:30", "30m");
270+
step("0:35", "35m");
271+
step("0:40", "40m");
272+
step("0:45", "45m");
273+
step("0:50", "50m");
274+
step("0:55", "55m");
275+
step("0-24", "0-24");
276+
step("0-24s", "0-24s");
277+
step("0-120s", "0-2m");
278+
step("0s-120m", "0-2");
279+
step("1-2", "1-2");
280+
step("30m-1", "30m-1");
281+
step("30m-90m", "30m-1h30m");
282+
283+
step("0/to/24/by/3", "0/3/6/9/12/15/18/21/24");
284+
step("12/to/48/by/12", "12/24/36/48");
285+
step("12/to/47/by/12", "12/24/36");
286+
step("0/to/6/by/30m", "0/30m/1/1h30m/2/2h30m/3/3h30m/4/4h30m/5/5h30m/6");
287+
step("0-6/to/18-24/by/6", "0-6/6-12/12-18/18-24");
288+
step("0-24/to/48-72/by/24", "0-24/24-48/48-72");
289+
step("0/to/24/by/3/0-6/to/18-24/by/6", "0/3/6/9/12/15/18/21/24/0-6/6-12/12-18/18-24");
274290
}
275291

276292

0 commit comments

Comments
 (0)