Skip to content

Commit 92560f9

Browse files
gmgunterGitHub Enterprise
authored andcommitted
Loosen DateTime::isClose() tolerance (#1029)
1 parent 49f1830 commit 92560f9

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

cxx/isce3/core/DateTime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static const int DAYSPER400 = 146097;
162162
static const int DAYSPER4 = 1461;
163163
static const int MAXORDINAL = 3652059;
164164

165-
static const double TOL_SECONDS = 1e-10;
165+
inline constexpr double TOL_SECONDS = 1e-9;
166166

167167
// time section and fractional seconds are optional
168168
// to support trailing white space, add ( )* to the

python/extensions/pybind_isce3/core/DateTime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void addbinding(py::class_<DateTime> & pyDateTime)
9999
.def("is_close",
100100
(bool (DateTime::*)(const DateTime &, const TimeDelta &) const)&DateTime::isClose,
101101
py::arg("other"),
102-
py::arg("tol") = TimeDelta(1e-10))
102+
py::arg("tol") = TimeDelta(isce3::core::TOL_SECONDS))
103103
// .def("day_of_year", &DateTime::dayOfYear) // XXX not implemented
104104
.def("seconds_of_day", &DateTime::secondsOfDay)
105105
// .def("day_of_week", &DateTime::dayOfWeek) // XXX not implemented

tests/python/extensions/pybind/core/datetime_.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,24 @@ def test_isoformat():
187187
s = "2017-05-12T01:12:30.141592000"
188188
t = isce.core.DateTime(s)
189189
assert( t.isoformat() == s )
190+
191+
def test_isoformat_roundtrip():
192+
# DateTime.isoformat() only writes 9 digits, so choose eps < 1e-9
193+
eps = 5e-10
194+
195+
# Construct a sequence of DateTimes separated by fixed intervals. The
196+
# temporal spacing is not an integer number of nanoseconds.
197+
n = 5
198+
t0 = isce.core.DateTime(2022, 5, 4, 10, 3, 0.0)
199+
dt = isce.core.TimeDelta(1.0 + eps)
200+
times = [t0 + i * dt for i in range(n)]
201+
202+
# Converts DateTime -> str -> DateTime.
203+
def roundtrip(t):
204+
s = t.isoformat()
205+
return isce.core.DateTime(s)
206+
207+
# Check that the tolerance of `is_close()` is loose enough to accommodate
208+
# truncation of DateTimes due to serialization to text.
209+
for t in times:
210+
assert t.is_close(roundtrip(t))

0 commit comments

Comments
 (0)