Skip to content

Commit aa71b03

Browse files
authored
Merge pull request #74 from Magisus/remove-double
(maint) Remove the ability to return durations as doubles
2 parents 136c140 + 4c165d3 commit aa71b03

File tree

3 files changed

+22
-76
lines changed

3 files changed

+22
-76
lines changed

lib/inc/hocon/config.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,6 @@ namespace hocon {
599599

600600
// TODO: memory parsing
601601

602-
/**
603-
* Gets a value as a decimal number of the specified units.
604-
* Correctly handles durations within the range +/-2^63 seconds.
605-
* @param path the path to the time value
606-
* @param unit the units of the number returned
607-
* @return a double representing the value converted to the requested units
608-
*/
609-
virtual double get_duration_as_double(std::string const& path, time_unit unit) const;
610-
611602
/**
612603
* Gets a value as an integer number of the specified units.
613604
* If the result would have a fractional part, the number is truncated.
@@ -616,7 +607,7 @@ namespace hocon {
616607
* @param unit the units of the number returned
617608
* @return a 64-bit integer representing the value converted to the requested units
618609
*/
619-
virtual int64_t get_duration_as_long(std::string const& path, time_unit unit) const;
610+
virtual int64_t get_duration(std::string const& path, time_unit unit) const;
620611

621612
/**
622613
* Clone the config with only the given path (and its children) retained;

lib/src/config.cc

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -328,41 +328,7 @@ namespace hocon {
328328
}
329329
}
330330

331-
double config::get_duration_as_double(string const& path, time_unit unit) const {
332-
auto timespan = get_duration(path);
333-
double result = 0.0;
334-
switch (unit) {
335-
case time_unit::NANOSECONDS:
336-
result = (timespan.first * 1000000000.0) + timespan.second;
337-
break;
338-
case time_unit::MICROSECONDS:
339-
result = (timespan.first * 1000000.0) + (timespan.second / 1000.0);
340-
break;
341-
case time_unit::MILLISECONDS:
342-
result = (timespan.first * 1000.0) + (timespan.second / 1000000.0);
343-
break;
344-
case time_unit::SECONDS:
345-
result = timespan.first + (timespan.second / 1000000000.0);
346-
break;
347-
case time_unit::MINUTES:
348-
result = (timespan.first + (timespan.second / 1000000000.0)) / 60.0;
349-
break;
350-
case time_unit::HOURS:
351-
result = (timespan.first + (timespan.second / 1000000000.0)) / 3600.0;
352-
break;
353-
case time_unit::DAYS:
354-
result = (timespan.first + (timespan.second / 1000000000.0)) / 84600.0;
355-
break;
356-
default:
357-
throw config_exception(_("Not a valid time_unit"));
358-
}
359-
if (!isnormal(result)) {
360-
throw config_exception(_("as_double: Overflow occurred during time conversion"));
361-
}
362-
return result;
363-
}
364-
365-
int64_t config::get_duration_as_long(string const& path, time_unit unit) const {
331+
int64_t config::get_duration(string const& path, time_unit unit) const {
366332
auto timespan = get_duration(path);
367333
int64_t result = 0;
368334
switch (unit) {

lib/tests/config_test.cc

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -119,44 +119,33 @@ TEST_CASE("should correctly parse durations", "[config]") {
119119
auto conf = config::parse_file_any_syntax(TEST_FILE_DIR + string("/fixtures/test01.conf"))->resolve();
120120

121121
SECTION("should be able to fetch number nodes as durations", "[config]") {
122-
REQUIRE(1 == conf->get_duration_as_long("durations.secondAsNumber", time_unit::SECONDS));
123-
REQUIRE(1000000000.0 == conf->get_duration_as_double("durations.secondAsNumber", time_unit::NANOSECONDS));
122+
REQUIRE(1 == conf->get_duration("durations.secondAsNumber", time_unit::SECONDS));
124123
}
125124

126125
SECTION("should be able to get durations in specific units", "[config]") {
127126
// Get as a long
128-
REQUIRE(1 == conf->get_duration_as_long("durations.second", time_unit::SECONDS));
129-
REQUIRE(500 == conf->get_duration_as_long("durations.halfSecond", time_unit::MILLISECONDS));
130-
REQUIRE(1 == conf->get_duration_as_long("durations.millis", time_unit::MILLISECONDS));
131-
REQUIRE(1000 == conf->get_duration_as_long("durations.second", time_unit::MILLISECONDS));
132-
REQUIRE(60 == conf->get_duration_as_long("durations.minute", time_unit::SECONDS));
133-
REQUIRE(60 == conf->get_duration_as_long("durations.hour", time_unit::MINUTES));
134-
REQUIRE(24 == conf->get_duration_as_long("durations.day", time_unit::HOURS));
135-
REQUIRE(-4 == conf->get_duration_as_long("durations.minusSeconds", time_unit::SECONDS));
136-
REQUIRE(43 == conf->get_duration_as_long("durations.secondWithFractional", time_unit::SECONDS));
137-
REQUIRE(43200 == conf->get_duration_as_long("durations.secondWithFractional", time_unit::MILLISECONDS));
138-
REQUIRE(9223372036854775807 == conf->get_duration_as_long("durations.largeNanos", time_unit::NANOSECONDS));
139-
REQUIRE(-9223372036854775807 == conf->get_duration_as_long("durations.minusLargeNanos", time_unit::NANOSECONDS));
127+
REQUIRE(1 == conf->get_duration("durations.second", time_unit::SECONDS));
128+
REQUIRE(500 == conf->get_duration("durations.halfSecond", time_unit::MILLISECONDS));
129+
REQUIRE(1 == conf->get_duration("durations.millis", time_unit::MILLISECONDS));
130+
REQUIRE(1000 == conf->get_duration("durations.second", time_unit::MILLISECONDS));
131+
REQUIRE(60 == conf->get_duration("durations.minute", time_unit::SECONDS));
132+
REQUIRE(60 == conf->get_duration("durations.hour", time_unit::MINUTES));
133+
REQUIRE(24 == conf->get_duration("durations.day", time_unit::HOURS));
134+
REQUIRE(-4 == conf->get_duration("durations.minusSeconds", time_unit::SECONDS));
135+
REQUIRE(43 == conf->get_duration("durations.secondWithFractional", time_unit::SECONDS));
136+
REQUIRE(43200 == conf->get_duration("durations.secondWithFractional", time_unit::MILLISECONDS));
137+
REQUIRE(9223372036854775807 == conf->get_duration("durations.largeNanos", time_unit::NANOSECONDS));
138+
REQUIRE(-9223372036854775807 == conf->get_duration("durations.minusLargeNanos", time_unit::NANOSECONDS));
140139
// getting as a long truncates when casting to a larger value
141-
REQUIRE(0 == conf->get_duration_as_long("durations.minute", time_unit::HOURS));
142-
REQUIRE(9223372036 == conf->get_duration_as_long("durations.largeNanos", time_unit::SECONDS));
143-
REQUIRE(153722867 == conf->get_duration_as_long("durations.largeNanos", time_unit::MINUTES));
144-
REQUIRE(2562047 == conf->get_duration_as_long("durations.largeNanos", time_unit::HOURS));
145-
REQUIRE(106751 == conf->get_duration_as_long("durations.largeNanos", time_unit::DAYS));
146-
147-
// Get as a double
148-
REQUIRE(43.2 == conf->get_duration_as_double("durations.secondWithFractional", time_unit::SECONDS));
149-
REQUIRE(43200.0 == conf->get_duration_as_double("durations.secondWithFractional", time_unit::MILLISECONDS));
150-
REQUIRE(-4.0 == conf->get_duration_as_double("durations.minusSeconds", time_unit::SECONDS));
151-
REQUIRE(0.5 == conf->get_duration_as_double("durations.halfSecond", time_unit::SECONDS));
152-
REQUIRE(1000.0 == conf->get_duration_as_double("durations.second", time_unit::MILLISECONDS));
153-
// getting as a double retains fractional part
154-
REQUIRE((1.0 / 60.0) == conf->get_duration_as_double("durations.minute", time_unit::HOURS));
140+
REQUIRE(0 == conf->get_duration("durations.minute", time_unit::HOURS));
141+
REQUIRE(9223372036 == conf->get_duration("durations.largeNanos", time_unit::SECONDS));
142+
REQUIRE(153722867 == conf->get_duration("durations.largeNanos", time_unit::MINUTES));
143+
REQUIRE(2562047 == conf->get_duration("durations.largeNanos", time_unit::HOURS));
144+
REQUIRE(106751 == conf->get_duration("durations.largeNanos", time_unit::DAYS));
155145
}
156146

157147
SECTION("should throw an exception when overflow occurs", "[config]") {
158-
REQUIRE_THROWS(conf->get_duration_as_long("durations.largeDays", time_unit::DAYS));
159-
REQUIRE_THROWS(conf->get_duration_as_double("durations.largeDays", time_unit::NANOSECONDS));
160-
REQUIRE_THROWS(conf->get_duration_as_long("durations.largeDays", time_unit::NANOSECONDS));
148+
REQUIRE_THROWS(conf->get_duration("durations.largeDays", time_unit::DAYS));
149+
REQUIRE_THROWS(conf->get_duration("durations.largeDays", time_unit::NANOSECONDS));
161150
}
162151
}

0 commit comments

Comments
 (0)