Skip to content

Commit 17c1e96

Browse files
committed
add Timestamp.opCmp
1 parent 979e39f commit 17c1e96

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

source/mir/timestamp.d

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,47 @@ struct Timestamp
271271
with(datetime) this(year, month, day);
272272
}
273273

274+
int opCmp(Timestamp rhs) const @safe pure nothrow @nogc
275+
{
276+
import std.meta: AliasSeq;
277+
static foreach (member; [
278+
"year",
279+
"month",
280+
"day",
281+
"hour",
282+
"minute",
283+
"second",
284+
])
285+
if (auto d = int(__traits(getMember, this, member)) - int(__traits(getMember, rhs, member)))
286+
return d;
287+
int frel = this.fractionExponent;
288+
int frer = rhs.fractionExponent;
289+
ulong frcl = this.fractionCoefficient;
290+
ulong frcr = rhs.fractionCoefficient;
291+
while(frel > frer)
292+
{
293+
frel--;
294+
frcl *= 10;
295+
}
296+
while(frer > frel)
297+
{
298+
frer--;
299+
frcr *= 10;
300+
}
301+
if (frcl < frcr) return -1;
302+
if (frcl > frcr) return +1;
303+
if (auto d = int(this.fractionExponent) - int(rhs.fractionExponent))
304+
return d;
305+
return int(this.offset) - int(rhs.offset);
306+
}
307+
274308
///
275309
version (mir_test)
276310
@safe unittest {
277311
import mir.date : Date;
278312
auto dt = Date(1982, 4, 1);
279313
Timestamp ts = dt;
314+
assert(ts.opCmp(ts) == 0);
280315
assert(dt.toISOExtString == ts.toString);
281316
assert(dt == cast(Date) ts);
282317
}

0 commit comments

Comments
 (0)