On 30.11.2015 00:25, bachmeier wrote:
I was just reading through the documentation for std.datetime.
DateTime.opBinary looks pretty interesting:

http://dlang.org/phobos/std_datetime.html#.DateTime.opBinary

Does anyone know how to use it? You certainly can't learn anything from
the documentation, because duration is a mystery. If someone knows, I
can submit a PR with that information added.

You can add a Duration to a DateTime, giving you a new DateTime. And you can subtract a DateTime from another, giving you the Duration between them.

Example:
----
import std.datetime, std.stdio;
void main()
{
    DateTime oldYear = DateTime(2015, 12, 31, 23, 59, 59);
    writeln(oldYear); /* 2015-Dec-31 23:59:59 */
DateTime newYear = oldYear + 1.seconds; /* adding Duration to DateTime */
    writeln(newYear); /* 2016-Jan-01 00:00:00 */
Duration diff = newYear - oldYear; /* subtracting DateTime from DateTime */
    writeln(diff); /* 1 sec */
}
----

Reply via email to