Showing with 62 additions and 0 deletions.
  1. +62 −0 std/datetime.d
62 changes: 62 additions & 0 deletions std/datetime.d
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,68 @@ public:
//assert(ist.opCmp(ist) == 0);
}

/**
* Returns: A hash of the $(LREF SysTime)
*/
size_t toHash() const @nogc pure nothrow @safe
{
static if (is(size_t == ulong))
{
return _stdTime;
}
else
{
// MurmurHash2
enum ulong m = 0xc6a4a7935bd1e995UL;
enum ulong n = m * 16;
enum uint r = 47;

ulong k = _stdTime;
k *= m;
k ^= k >> r;
k *= m;

ulong h = n;
h ^= k;
h *= m;

return cast(size_t) h;
}
}

@safe unittest
{
assert(SysTime(0).toHash == SysTime(0).toHash);
assert(SysTime(DateTime(2000, 1, 1)).toHash == SysTime(DateTime(2000, 1, 1)).toHash);
assert(SysTime(DateTime(2000, 1, 1)).toHash != SysTime(DateTime(2000, 1, 2)).toHash);

// test that timezones aren't taken into account
assert(SysTime(0, LocalTime()).toHash == SysTime(0, LocalTime()).toHash);
assert(SysTime(0, LocalTime()).toHash == SysTime(0, UTC()).toHash);
assert(
SysTime(
DateTime(2000, 1, 1), LocalTime()
).toHash == SysTime(
DateTime(2000, 1, 1), LocalTime()
).toHash
);
immutable zone = new SimpleTimeZone(dur!"minutes"(60));
assert(
SysTime(
DateTime(2000, 1, 1, 1), zone
).toHash == SysTime(
DateTime(2000, 1, 1), UTC()
).toHash
);
assert(
SysTime(
DateTime(2000, 1, 1), zone
).toHash != SysTime(
DateTime(2000, 1, 1), UTC()
).toHash
);
}

/++
Year of the Gregorian Calendar. Positive numbers are A.D. Non-positive
are B.C.
Expand Down