www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to add time to Clock.currTime

reply "JohnnyK" <johnnykinsey comcast.net> writes:
Hi All,
   I did search but I cannot find it anywhere.  All I want to do 
is add 300 seconds to the output of Clock.currTime.  So basically 
if Clock.currTime equals 2013-Oct-04 17:19:31.3338333 then I want 
to subtract 300 seconds from that to get the time that it was 300 
seconds ago.  In other languages I would convert the time value 
to seconds and subtract the 300 seconds then convert it back to 
what ever time value it was before. I don't know how to convert 
300 seconds to hnsecs and I have never actually heard of hnsecs 
before.  Anyway if someone could help I would appreciate it.
Oct 04 2013
parent reply "Brad Anderson" <eco gnuk.net> writes:
On Friday, 4 October 2013 at 21:46:43 UTC, JohnnyK wrote:
 Hi All,
   I did search but I cannot find it anywhere.  All I want to do 
 is add 300 seconds to the output of Clock.currTime.  So 
 basically if Clock.currTime equals 2013-Oct-04 17:19:31.3338333 
 then I want to subtract 300 seconds from that to get the time 
 that it was 300 seconds ago.  In other languages I would 
 convert the time value to seconds and subtract the 300 seconds 
 then convert it back to what ever time value it was before. I 
 don't know how to convert 300 seconds to hnsecs and I have 
 never actually heard of hnsecs before.  Anyway if someone could 
 help I would appreciate it.
auto t = Clock.currTime; t -= 300.seconds; t += 300.seconds;
Oct 04 2013
parent reply "Brad Anderson" <eco gnuk.net> writes:
On Friday, 4 October 2013 at 21:50:31 UTC, Brad Anderson wrote:
 On Friday, 4 October 2013 at 21:46:43 UTC, JohnnyK wrote:
 Hi All,
  I did search but I cannot find it anywhere.  All I want to do 
 is add 300 seconds to the output of Clock.currTime.  So 
 basically if Clock.currTime equals 2013-Oct-04 
 17:19:31.3338333 then I want to subtract 300 seconds from that 
 to get the time that it was 300 seconds ago.  In other 
 languages I would convert the time value to seconds and 
 subtract the 300 seconds then convert it back to what ever 
 time value it was before. I don't know how to convert 300 
 seconds to hnsecs and I have never actually heard of hnsecs 
 before.  Anyway if someone could help I would appreciate it.
auto t = Clock.currTime; t -= 300.seconds; t += 300.seconds;
And just a little bit more info, another way to write this is: auto t = Clock.currTime; t -= seconds(300); t += seconds(300); The first way I wrote is just the UFCS version of that. You could also do: auto t = Clock.currTime; t -= dur!"seconds"(300); t += dur!"seconds"(300); seconds() is just an alias for the dur template with the "seconds" template argument.
Oct 04 2013
parent reply "JohnnyK" <johnnykinsey comcast.net> writes:
On Friday, 4 October 2013 at 21:54:19 UTC, Brad Anderson wrote:
 On Friday, 4 October 2013 at 21:50:31 UTC, Brad Anderson wrote:
 On Friday, 4 October 2013 at 21:46:43 UTC, JohnnyK wrote:
 Hi All,
 I did search but I cannot find it anywhere.  All I want to do 
 is add 300 seconds to the output of Clock.currTime.  So 
 basically if Clock.currTime equals 2013-Oct-04 
 17:19:31.3338333 then I want to subtract 300 seconds from 
 that to get the time that it was 300 seconds ago.  In other 
 languages I would convert the time value to seconds and 
 subtract the 300 seconds then convert it back to what ever 
 time value it was before. I don't know how to convert 300 
 seconds to hnsecs and I have never actually heard of hnsecs 
 before.  Anyway if someone could help I would appreciate it.
auto t = Clock.currTime; t -= 300.seconds; t += 300.seconds;
And just a little bit more info, another way to write this is: auto t = Clock.currTime; t -= seconds(300); t += seconds(300); The first way I wrote is just the UFCS version of that. You could also do: auto t = Clock.currTime; t -= dur!"seconds"(300); t += dur!"seconds"(300); seconds() is just an alias for the dur template with the "seconds" template argument.
Wow I appreciate the quick response. Ok I have seen this before. What is the dur? Where is dur defined? Also I am confused how 300.seconds would work. How can a literal number have properties?
Oct 04 2013
next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Saturday, 5 October 2013 at 01:31:35 UTC, JohnnyK wrote:
 Wow I appreciate the quick response.  Ok I have seen this 
 before.
  What is the dur?  Where is dur defined?  Also I am confused 
 how 300.seconds would work.  How can a literal number have 
 properties?
300.seconds is using UFCS (Uniform Function Call Syntax) since int does not have a property "seconds" the compiler will rewrite the call to seconds(300).
Oct 04 2013
prev sibling parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Saturday, October 05, 2013 03:31:33 JohnnyK wrote:
 Wow I appreciate the quick response. Ok I have seen this before.
 What is the dur? Where is dur defined? Also I am confused how
 300.seconds would work. How can a literal number have properties?
dur is in core.time as are the aliases for each of the units: http://dlang.org/phobos/core_time.html#dur 300.seconds works thanks to UFCS (Universal Function Call Syntax). Any time that the compiler sees x.foo(args); and foo is not a member function of x, it converts it to foo(x, args); So, if there's a free function with that name which will work with those arguments, then it will be called (otherwise, you'll get an error, because there is no matching function). The parens can be dropped on 300.seconds, because the parens are optional on function calls that take no arguments. seconds is not actually a property function (it's an alias for dur!"seconds, which is not a property function either), but the fact that the parens are optional makes it so that you can use it with the same syntax that would be used for a getter property. If you want an overview of std.datetime and the time stuff in Phobos, I suggest that you read http://dlang.org/intro-to-datetime.html - Jonathan M Davis
Oct 04 2013
parent "JohnnyK" <johnnykinsey comcast.net> writes:
On Saturday, 5 October 2013 at 02:42:49 UTC, Jonathan M Davis 
wrote:
 On Saturday, October 05, 2013 03:31:33 JohnnyK wrote:
 Wow I appreciate the quick response. Ok I have seen this 
 before.
 What is the dur? Where is dur defined? Also I am confused how
 300.seconds would work. How can a literal number have 
 properties?
dur is in core.time as are the aliases for each of the units: http://dlang.org/phobos/core_time.html#dur 300.seconds works thanks to UFCS (Universal Function Call Syntax). Any time that the compiler sees x.foo(args); and foo is not a member function of x, it converts it to foo(x, args); So, if there's a free function with that name which will work with those arguments, then it will be called (otherwise, you'll get an error, because there is no matching function). The parens can be dropped on 300.seconds, because the parens are optional on function calls that take no arguments. seconds is not actually a property function (it's an alias for dur!"seconds, which is not a property function either), but the fact that the parens are optional makes it so that you can use it with the same syntax that would be used for a getter property. If you want an overview of std.datetime and the time stuff in Phobos, I suggest that you read http://dlang.org/intro-to-datetime.html - Jonathan M Davis
I appreciate everyone's responses and yes Jonathan that article did help. I know I looked under the article section but I must have overlooked this article.
Oct 07 2013