Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
November 29, 2015 DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
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. |
November 29, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | 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 */
}
----
|
November 29, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Sun, 29 Nov 2015 23:25:14 +0000, 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. Duration is defined in core.time: https://dlang.org/phobos/ core_time.html#Duration Unfortunately, ddoc doesn't automatically cross-reference these for you, which results in confusion. (As if it weren't confusing enough to have everything wrapped in templates with filters rather than simply using const(Duration).) |
November 30, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Sunday, 29 November 2015 at 23:52:05 UTC, anonymous wrote:
> 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 */
> }
> ----
Thanks. I'll add these as examples.
|
November 30, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Wright | On Sunday, 29 November 2015 at 23:53:41 UTC, Chris Wright wrote:
> Duration is defined in core.time: https://dlang.org/phobos/ core_time.html#Duration
>
> Unfortunately, ddoc doesn't automatically cross-reference these for you, which results in confusion. (As if it weren't confusing enough to have everything wrapped in templates with filters rather than simply using const(Duration).)
Yeah, there has to be a better way to document these functions. I guess I could have figured out that there is a Duration struct from this
opBinary(string op, D)(in D duration) if ((op == "+" || op == "-") && (is(Unqual!D == Duration) || is(Unqual!D == TickDuration)))
but (string op, D) -> (in D duration) -> is(Unqual!D == Duration) is something that would be rejected from Boost because it's too complicated for C++.
|
November 30, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Wright | On Sunday, November 29, 2015 23:53:41 Chris Wright via Digitalmars-d-learn wrote:
> Unfortunately, ddoc doesn't automatically cross-reference these for you, which results in confusion. (As if it weren't confusing enough to have everything wrapped in templates with filters rather than simply using const(Duration).)
Once TickDuration finally goes away, then functions like DateTime's opBinary can be simplified to just take Duration.
But until TickDuration and the few things that use it in their API have gone through the deprecation process, we're stuck with it in places like this.
- Jonathan M Davis
|
November 30, 2015 Re: DateTime.opBinary | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Mon, 30 Nov 2015 01:30:28 -0800, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Sunday, November 29, 2015 23:53:41 Chris Wright via Digitalmars-d-learn wrote:
>> Unfortunately, ddoc doesn't automatically cross-reference these for
>> you,
>> which results in confusion. (As if it weren't confusing enough to have
>> everything wrapped in templates with filters rather than simply using
>> const(Duration).)
>
> Once TickDuration finally goes away, then functions like DateTime's opBinary can be simplified to just take Duration.
>
> But until TickDuration and the few things that use it in their API have gone through the deprecation process, we're stuck with it in places like this.
>
> - Jonathan M Davis
Or with explicit overloads, which would be easier to document and easier to read and as easy to maintain.
|
Copyright © 1999-2021 by the D Language Foundation