www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SysTime.add!"days" missing

reply "Spacen Jasset" <spacenjasset mailrazer.com> writes:
I would like to subtract 60 days from a SysTime, but find that 
SysTime.add!"days" is not available, unlike Systime.roll!"days" 
which is available.

main.d(18): Error: template instance add!("days") add!("days") 
does not match te
mplate declaration add(string units)(long value, AllowDayOverflow 
allowOverflow
= AllowDayOverflow.yes) if (units == "years" || units == "months")



Why is "days" add missing? How might I get round this?

Regards,

Spacen.
Mar 17 2014
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 17 March 2014 at 11:11:26 UTC, Spacen Jasset wrote:
 I would like to subtract 60 days from a SysTime, but find that 
 SysTime.add!"days" is not available, unlike Systime.roll!"days" 
 which is available.
You can do this in a simpler way: t -= 60.days; SysTime.add likely specializes on durations the length of which varies depending from their starting point, due to the varying number of days in a month or in a year.
Mar 17 2014
parent reply "Spacen Jasset" <spacenjasset mailrazer.com> writes:
On Monday, 17 March 2014 at 11:52:08 UTC, Vladimir Panteleev 
wrote:
 On Monday, 17 March 2014 at 11:11:26 UTC, Spacen Jasset wrote:
 I would like to subtract 60 days from a SysTime, but find that 
 SysTime.add!"days" is not available, unlike 
 Systime.roll!"days" which is available.
You can do this in a simpler way: t -= 60.days; SysTime.add likely specializes on durations the length of which varies depending from their starting point, due to the varying number of days in a month or in a year.
Thanks. What devilish magic allows for the syntax 60.days? (how does it work)
Mar 17 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
 Thanks. What devilish magic allows for the syntax 60.days? (how 
 does it work)
There's a function in core.time: Duration days(int n); D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments). This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.
Mar 17 2014
parent reply "Spacen Jasset" <spacenjasset mailrazer.com> writes:
On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
 On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
 Thanks. What devilish magic allows for the syntax 60.days? 
 (how does it work)
There's a function in core.time: Duration days(int n); D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments). This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.
Thanks Adam, is there a good explanation anywhere? It must be newish.
Mar 17 2014
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
On 3/17/14, 12:11 PM, Spacen Jasset wrote:
 On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
 On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
 Thanks. What devilish magic allows for the syntax 60.days? (how does
 it work)
There's a function in core.time: Duration days(int n); D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments). This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.
Thanks Adam, is there a good explanation anywhere? It must be newish.
http://dlang.org/function.html#pseudo-member
Mar 17 2014
parent reply "Spacen Jasset" <spacenjasset mailrazer.com> writes:
On Monday, 17 March 2014 at 15:24:22 UTC, Ary Borenszweig wrote:
 On 3/17/14, 12:11 PM, Spacen Jasset wrote:
 On Monday, 17 March 2014 at 14:39:16 UTC, Adam D. Ruppe wrote:
 On Monday, 17 March 2014 at 14:31:54 UTC, Spacen Jasset wrote:
 Thanks. What devilish magic allows for the syntax 60.days? 
 (how does
 it work)
There's a function in core.time: Duration days(int n); D functions f(x, t...) can also be called x.f(t) (or x.f without parameters if there's no additional arguments). This works on all types, it is called uniform function call syntax, or UFCS. It lets us extend other things with new methods.
Thanks Adam, is there a good explanation anywhere? It must be newish.
http://dlang.org/function.html#pseudo-member
Thanks but I still can't see how it fully explains this: writeln("60 days: ", 60.days); There is some type magic going on somewhere, because 60 is an int and days is a random method that happens to belong to Duration.
Mar 17 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 17 March 2014 at 16:16:20 UTC, Spacen Jasset wrote:
 int and days is a random method that happens to belong to 
 Duration.
There's a separate function days https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L928 that one belongs to Duration https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L1414 this one doesn't. In the 60.days instance, it is calling the second function.
Mar 17 2014
parent "Spacen Jasset" <spacenjasset mailrazer.com> writes:
On Monday, 17 March 2014 at 16:33:34 UTC, Adam D. Ruppe wrote:
 On Monday, 17 March 2014 at 16:16:20 UTC, Spacen Jasset wrote:
 int and days is a random method that happens to belong to 
 Duration.
There's a separate function days https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L928 that one belongs to Duration https://github.com/D-Programming-Language/druntime/blob/master/src/core/time.d#L1414 this one doesn't. In the 60.days instance, it is calling the second function.
Thanks Adam, I see how the magic happens now.
Mar 17 2014
prev sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 03/17/2014 08:11 AM, Spacen Jasset wrote:

 Thanks Adam, is there a good explanation anywhere?
Here is another one: http://ddili.org/ders/d.en/ufcs.html Ali
Mar 17 2014