Skip to content

Commit df23829

Browse files
committed
Create concept of CalendarDate/JulianDay
1 parent 7376e74 commit df23829

File tree

9 files changed

+229
-72
lines changed

9 files changed

+229
-72
lines changed

Viewer/ecflowUI/src/VRepeatAttr.cpp

+9-6
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ int VRepeatDateAttr::endIndex() const {
195195
if (node_ptr node = parent_->node()) {
196196
const Repeat& r = node->repeat();
197197
if (r.step() > 0) {
198-
long jStart = ecf::calendar_date_to_julian_day(r.start());
199-
long jEnd = ecf::calendar_date_to_julian_day(r.end());
198+
long jStart = ecf::CalendarDate(r.start()).as_julian_day().value();
199+
long jEnd = ecf::CalendarDate(r.end()).as_julian_day().value();
200200

201201
int index = (jEnd - jStart) / r.step();
202202
long val = jStart + index * r.step();
@@ -213,7 +213,9 @@ int VRepeatDateAttr::endIndex() const {
213213
int VRepeatDateAttr::currentIndex() const {
214214
if (node_ptr node = parent_->node()) {
215215
const Repeat& r = node->repeat();
216-
int cur = (ecf::calendar_date_to_julian_day(r.index_or_value()) - ecf::calendar_date_to_julian_day(r.start())) / r.step();
216+
int cur = (ecf::CalendarDate(r.index_or_value()).as_julian_day().value() -
217+
ecf::CalendarDate(r.start()).as_julian_day().value()) /
218+
r.step();
217219
return cur;
218220
}
219221
return 0;
@@ -239,7 +241,8 @@ std::string VRepeatDateAttr::value(int index) const {
239241
std::stringstream ss;
240242
if (node_ptr node = parent_->node()) {
241243
const Repeat& r = node->repeat();
242-
ss << (ecf::julian_day_to_calendar_date(ecf::calendar_date_to_julian_day(r.start()) + index * r.step()));
244+
auto date = ecf::CalendarDate(r.start()) + (index * r.step());
245+
ss << date.value();
243246
}
244247

245248
return ss.str();
@@ -252,7 +255,7 @@ int VRepeatDateAttr::currentPosition() const {
252255
return -1;
253256
else if (r.value() == r.start())
254257
return 0;
255-
else if (r.value() == r.end() || ecf::calendar_date_to_julian_day(r.value()) + r.step() > ecf::calendar_date_to_julian_day(r.end()))
258+
else if (r.value() == r.end() || ecf::CalendarDate(r.value()) + r.step() > ecf::CalendarDate(r.end()))
256259
return 2;
257260
else
258261
return 1;
@@ -272,7 +275,7 @@ int VRepeatDateTimeAttr::endIndex() const {
272275
auto& r = node->repeat();
273276
auto rng = ecf::make_range<RepeatDateTime>(r);
274277
auto idx = rng.end();
275-
idx = std::min(idx, rng.size() - 1); // ensure idx is within range [0, size-1]
278+
idx = std::min(idx, rng.size() - 1); // ensure idx is within range [0, size-1]
276279
return idx;
277280
}
278281
return 0;

libs/attribute/src/ecflow/attribute/RepeatAttr.cpp

+16-33
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,7 @@ void RepeatDate::update_repeat_genvar_value() const {
260260
dow_.set_value(ecf::convert_to<std::string>(day_of_week));
261261

262262
long last_value = last_valid_value();
263-
long julian = ecf::calendar_date_to_julian_day(last_value);
264-
julian_.set_value(ecf::convert_to<std::string>(julian));
263+
julian_.set_value(ecf::convert_to<std::string>(ecf::CalendarDate(last_value).as_julian_day().value()));
265264
}
266265
catch (std::exception& e) {
267266
std::stringstream ss;
@@ -304,17 +303,13 @@ long RepeatDate::valid_value(long value) const {
304303
}
305304

306305
long RepeatDate::last_valid_value_minus(int val) const {
307-
long last_value = last_valid_value();
308-
long julian = ecf::calendar_date_to_julian_day(last_value);
309-
julian -= val;
310-
return ecf::julian_day_to_calendar_date(julian);
306+
auto result = ecf::CalendarDate(last_valid_value()) - val;
307+
return result.value();
311308
}
312309

313310
long RepeatDate::last_valid_value_plus(int val) const {
314-
long last_value = last_valid_value();
315-
long julian = ecf::calendar_date_to_julian_day(last_value);
316-
julian += val;
317-
return ecf::julian_day_to_calendar_date(julian);
311+
auto result = ecf::CalendarDate(last_valid_value()) + val;
312+
return result.value();
318313
}
319314

320315
void RepeatDate::reset() {
@@ -384,11 +379,8 @@ std::string RepeatDate::value_as_string(int index) const {
384379
}
385380

386381
std::string RepeatDate::next_value_as_string() const {
387-
long val = last_valid_value();
388-
389-
long julian = ecf::calendar_date_to_julian_day(val);
390-
julian += delta_;
391-
val = ecf::julian_day_to_calendar_date(julian);
382+
auto result = ecf::CalendarDate(last_valid_value()) + delta_;
383+
long val = result.value();
392384

393385
try {
394386
return ecf::convert_to<std::string>(valid_value(val));
@@ -399,11 +391,8 @@ std::string RepeatDate::next_value_as_string() const {
399391
}
400392

401393
std::string RepeatDate::prev_value_as_string() const {
402-
long val = last_valid_value();
403-
404-
long julian = ecf::calendar_date_to_julian_day(val);
405-
julian -= delta_;
406-
val = ecf::julian_day_to_calendar_date(julian);
394+
auto result = ecf::CalendarDate(last_valid_value()) - delta_;
395+
long val = result.value();
407396

408397
try {
409398
return ecf::convert_to<std::string>(valid_value(val));
@@ -414,9 +403,8 @@ std::string RepeatDate::prev_value_as_string() const {
414403
}
415404

416405
void RepeatDate::increment() {
417-
long julian = ecf::calendar_date_to_julian_day(value_);
418-
julian += delta_;
419-
set_value(ecf::julian_day_to_calendar_date(julian));
406+
auto result = ecf::CalendarDate(last_valid_value()) + delta_;
407+
set_value(result.value());
420408
}
421409

422410
void RepeatDate::change(const std::string& newdate) {
@@ -469,8 +457,8 @@ void RepeatDate::changeValue(long the_new_date) {
469457
}
470458

471459
// Check new value is in step. ECFLOW-325 repeat date 7
472-
long julian_new_date = ecf::calendar_date_to_julian_day(the_new_date);
473-
long julian_start = ecf::calendar_date_to_julian_day(start_);
460+
long julian_new_date = ecf::CalendarDate(the_new_date).as_julian_day().value();
461+
long julian_start = ecf::CalendarDate(start_).as_julian_day().value();
474462
long diff = julian_new_date - julian_start;
475463
if (diff % delta_ != 0) {
476464
std::stringstream ss;
@@ -913,8 +901,7 @@ void RepeatDateList::update_repeat_genvar_value() const {
913901
dom_.set_value(ecf::convert_to<std::string>(day_of_month));
914902
dow_.set_value(ecf::convert_to<std::string>(day_of_week));
915903

916-
long last_value = last_valid_value();
917-
long julian = ecf::calendar_date_to_julian_day(last_value);
904+
long julian = CalendarDate(last_valid_value()).as_julian_day().value();
918905
julian_.set_value(ecf::convert_to<std::string>(julian));
919906
}
920907
catch (std::exception& e) {
@@ -1001,19 +988,15 @@ long RepeatDateList::last_valid_value_minus(int val) const {
1001988
if (last_value == 0)
1002989
return 0;
1003990

1004-
long julian = ecf::calendar_date_to_julian_day(last_value);
1005-
julian -= val;
1006-
return ecf::julian_day_to_calendar_date(julian);
991+
return (CalendarDate(last_value) - val).value();
1007992
}
1008993

1009994
long RepeatDateList::last_valid_value_plus(int val) const {
1010995
long last_value = last_valid_value();
1011996
if (last_value == 0)
1012997
return 0;
1013998

1014-
long julian = ecf::calendar_date_to_julian_day(last_value);
1015-
julian += val;
1016-
return ecf::julian_day_to_calendar_date(julian);
999+
return (CalendarDate(last_value) + val).value();
10171000
}
10181001

10191002
void RepeatDateList::setToLastValue() {

libs/attribute/src/ecflow/attribute/RepeatRange.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,25 @@ struct Range<RepeatDate>
5555

5656
iterator begin() const { return 0; }
5757
iterator end() const {
58-
int i = ecf::calendar_date_to_julian_day(r_.start());
59-
int j = ecf::calendar_date_to_julian_day(r_.end());
58+
int i = ecf::CalendarDate(r_.start()).as_julian_day().value();
59+
int j = ecf::CalendarDate(r_.end()).as_julian_day().value();
6060
int s = r_.step();
6161
int d = (j - i) + 1;
6262
return (d / s) + ((d % s == 0) ? 0 : 1);
6363
}
6464
iterator current_index() const {
65-
auto i = ecf::calendar_date_to_julian_day(r_.start());
65+
auto i = ecf::CalendarDate(r_.start()).as_julian_day().value();
6666
auto s = r_.step();
67-
auto v = ecf::calendar_date_to_julian_day(r_.value());
67+
auto v = ecf::CalendarDate(r_.value()).as_julian_day().value();
6868
auto idx = (v - i) / s;
6969
return idx;
7070
}
7171

7272
value_type current_value() const { return r_.value(); }
7373

7474
value_type at(iterator i) const {
75-
int j = ecf::calendar_date_to_julian_day(r_.start());
76-
return ecf::julian_day_to_calendar_date(j + i * r_.step());
75+
int j = ecf::CalendarDate(r_.start()).as_julian_day().value();
76+
return ecf::JulianDay(j + i * r_.step()).as_calendar_date().value();
7777
}
7878

7979
size_type size() const { return end() - begin(); }

libs/attribute/test/TestRepeat.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ BOOST_AUTO_TEST_CASE(generated_variables) {
530530
{
531531
const Variable& var = rep.find_gen_variable("YMD_JULIAN");
532532
BOOST_CHECK_MESSAGE(!var.empty(), "Did not find generated variable YMD_JULIAN");
533-
std::string expected = ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20090916));
533+
std::string expected = ecf::convert_to<std::string>(ecf::CalendarDate(20090916).as_julian_day().value());
534534
BOOST_CHECK_MESSAGE(var.theValue() == expected, "expected " << expected << " but found " << var.theValue());
535535
}
536536
}
@@ -597,13 +597,13 @@ BOOST_AUTO_TEST_CASE(more_generated_variables) {
597597
expected_day_of_week.emplace_back("5");
598598

599599
std::vector<std::string> expected_julian;
600-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20161231)));
601-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170101)));
602-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170102)));
603-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170103)));
604-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170104)));
605-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170105)));
606-
expected_julian.push_back(ecf::convert_to<std::string>(ecf::calendar_date_to_julian_day(20170106)));
600+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20161231).as_julian_day().value()));
601+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170101).as_julian_day().value()));
602+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170102).as_julian_day().value()));
603+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170103).as_julian_day().value()));
604+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170104).as_julian_day().value()));
605+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170105).as_julian_day().value()));
606+
expected_julian.push_back(ecf::convert_to<std::string>(ecf::CalendarDate(20170106).as_julian_day().value()));
607607

608608
for (int i = 0; i < 7; i++) {
609609

libs/core/src/ecflow/core/Calendar.cpp

+111-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ using namespace boost::posix_time;
3232

3333
namespace ecf {
3434

35-
long julian_day_to_calendar_date(long jdate) {
35+
namespace implementation {
36+
37+
CalendarDate::value_t julian_day_to_calendar_date(long jdate) {
3638
long x = 0, y = 0, d = 0, m = 0, e = 0;
3739
long day = 0, month = 0, year = 0;
3840

@@ -62,7 +64,7 @@ long julian_day_to_calendar_date(long jdate) {
6264
return year * 10000 + month * 100 + day;
6365
}
6466

65-
long calendar_date_to_julian_day(long ddate) {
67+
JulianDay::value_t calendar_date_to_julian_day(long ddate) {
6668
long m1 = 0, y1 = 0, a = 0, b = 0, c = 0, d = 0, j1 = 0;
6769

6870
long month = 0, day = 0, year = 0;
@@ -90,6 +92,113 @@ long calendar_date_to_julian_day(long ddate) {
9092
return j1;
9193
}
9294

95+
} // namespace implementation
96+
97+
JulianDay CalendarDate::as_julian_day() const {
98+
return JulianDay(implementation::calendar_date_to_julian_day(value_));
99+
}
100+
101+
CalendarDate JulianDay::as_calendar_date() const {
102+
return CalendarDate(implementation::julian_day_to_calendar_date(value_));
103+
}
104+
105+
bool operator==(const CalendarDate& lhs, const CalendarDate& rhs) {
106+
return lhs.value() == rhs.value();
107+
}
108+
109+
bool operator==(const CalendarDate& lhs, const JulianDay& rhs) {
110+
return lhs.as_julian_day() == rhs;
111+
}
112+
113+
bool operator==(const JulianDay& lhs, const CalendarDate& rhs) {
114+
return lhs == rhs.as_julian_day();
115+
}
116+
117+
bool operator==(const JulianDay& lhs, const JulianDay& rhs) {
118+
return lhs.value() == rhs.value();
119+
}
120+
121+
bool operator<(const CalendarDate& lhs, const CalendarDate& rhs) {
122+
return lhs.as_julian_day() < rhs.as_julian_day();
123+
}
124+
125+
bool operator<=(const CalendarDate& lhs, const CalendarDate& rhs) {
126+
return lhs.as_julian_day() <= rhs.as_julian_day();
127+
}
128+
129+
bool operator>(const CalendarDate& lhs, const CalendarDate& rhs) {
130+
return lhs.as_julian_day() > rhs.as_julian_day();
131+
}
132+
133+
bool operator>=(const CalendarDate& lhs, const CalendarDate& rhs) {
134+
return lhs.as_julian_day() >= rhs.as_julian_day();
135+
}
136+
137+
bool operator<(const JulianDay& lhs, const JulianDay& rhs) {
138+
return lhs.value() < rhs.value();
139+
}
140+
141+
bool operator<=(const JulianDay& lhs, const JulianDay& rhs) {
142+
return lhs.value() <= rhs.value();
143+
}
144+
145+
bool operator>(const JulianDay& lhs, const JulianDay& rhs) {
146+
return lhs.value() > rhs.value();
147+
}
148+
149+
bool operator>=(const JulianDay& lhs, const JulianDay& rhs) {
150+
return lhs.value() >= rhs.value();
151+
}
152+
153+
CalendarDate& operator+=(CalendarDate& lhs, CalendarDate::value_t rhs) {
154+
auto jd = lhs.as_julian_day() + rhs;
155+
lhs.value_ = jd.as_calendar_date().value();
156+
return lhs;
157+
}
158+
159+
CalendarDate& operator-=(CalendarDate& lhs, CalendarDate::value_t rhs) {
160+
auto jd = lhs.as_julian_day() - rhs;
161+
lhs.value_ = jd.as_calendar_date().value();
162+
return lhs;
163+
}
164+
165+
CalendarDate operator+(const CalendarDate& lhs, CalendarDate::value_t rhs) {
166+
auto result = lhs;
167+
return result += rhs;
168+
}
169+
170+
CalendarDate operator-(const CalendarDate& lhs, CalendarDate::value_t rhs) {
171+
auto result = lhs;
172+
return result -= rhs;
173+
}
174+
175+
JulianDay& operator+=(JulianDay& lhs, JulianDay::value_t rhs) {
176+
lhs.value_ += rhs;
177+
return lhs;
178+
}
179+
180+
JulianDay& operator-=(JulianDay& lhs, JulianDay::value_t rhs) {
181+
lhs.value_ -= rhs;
182+
return lhs;
183+
}
184+
185+
JulianDay operator+(const JulianDay& lhs, JulianDay::value_t rhs) {
186+
return JulianDay(lhs.value() + rhs);
187+
}
188+
189+
JulianDay operator-(const JulianDay& lhs, JulianDay::value_t rhs) {
190+
return JulianDay(lhs.value() - rhs);
191+
}
192+
193+
std::ostream& operator<<(std::ostream& os, const CalendarDate& cd) {
194+
os << cd.value();
195+
return os;
196+
}
197+
std::ostream& operator<<(std::ostream& os, const JulianDay& jd) {
198+
os << jd.value();
199+
return os;
200+
}
201+
93202
Calendar::Calendar() = default;
94203

95204
Calendar::Calendar(const Calendar& rhs) {

0 commit comments

Comments
 (0)