9
9
*/
10
10
11
11
#include " eckit/utils/Translator.h"
12
-
12
+ # include " eckit/utils/Tokenizer.h "
13
13
#include " eckit/types/Date.h"
14
- #include " metkit/mars/MarsRequest.h"
15
14
15
+ #include " metkit/mars/MarsRequest.h"
16
16
#include " metkit/mars/TypesFactory.h"
17
17
#include " metkit/mars/TypeDate.h"
18
18
#include " eckit/utils/StringTools.h"
@@ -24,6 +24,24 @@ namespace metkit::mars {
24
24
25
25
// ----------------------------------------------------------------------------------------------------------------------
26
26
27
+ static const char * months[] = {
28
+ " jan" , " feb" , " mar" , " apr" , " may" , " jun" , " jul" , " aug" , " sep" , " oct" , " nov" , " dec" ,
29
+ };
30
+
31
+ int month (const std::string& value) {
32
+ if (value.size () == 3 ) {
33
+ std::string month = eckit::StringTools::lower (value).substr (0 ,3 );
34
+ for (size_t i=0 ; i<12 ; i++) { // check month name
35
+ if (months[i] == month) {
36
+ return i+1 ;
37
+ }
38
+ }
39
+ }
40
+
41
+ eckit::Translator<std::string, int > s2i;
42
+ return s2i (value);
43
+ }
44
+
27
45
TypeDate::TypeDate (const std::string &name, const eckit::Value& settings) :
28
46
Type (name, settings), TypeToByList<eckit::Date, long >(name, settings) {
29
47
@@ -42,18 +60,35 @@ void TypeDate::pass2(const MarsExpandContext& ctx, MarsRequest& request) {
42
60
43
61
bool TypeDate::expand (const MarsExpandContext& ctx, std::string &value) const {
44
62
if (!value.empty ()) {
63
+ eckit::Translator<std::string, long > s2l;
45
64
eckit::Translator<long , std::string> l2s;
46
65
if (value[0 ] == ' 0' || value[0 ] == ' -' ) {
47
- eckit::Translator<std::string, long > s2l;
48
66
long n = s2l (value);
49
67
if (n <= 0 ) {
50
68
eckit::Date now (n);
51
69
value = l2s (now.yyyymmdd ());
52
70
}
53
71
}
54
72
else {
55
- eckit::Date date (value);
56
- value = l2s (date.yyyymmdd ());
73
+ eckit::Tokenizer t (" -" );
74
+ std::vector<std::string> tokens = t.tokenize (value);
75
+
76
+ if (tokens.size () == 2 ) { // TypeClimateDaily
77
+ int m = month (tokens[0 ]);
78
+ long d = s2l (tokens[1 ]);
79
+
80
+ value = l2s (100 *m + d);
81
+ }
82
+ else {
83
+ if (tokens[0 ].size () <= 3 ) {
84
+ int m = month (tokens[0 ]);
85
+ value = l2s (m);
86
+ }
87
+ else {
88
+ eckit::Date date (value);
89
+ value = l2s (date.yyyymmdd ());
90
+ }
91
+ }
57
92
}
58
93
}
59
94
return true ;
0 commit comments