www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Milliseconds

reply Giovanni Di Maria <calimero22 yahoo.it> writes:
Hi
I have read much, before to write here.
How can i store, to an int variable, the milliseconds of the 
current time?
It's simple, but i don't find the solution.
Thank you very much.

Giovanni Di Maria
Jul 11 2019
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Friday, July 12, 2019 12:51:28 AM MDT Giovanni Di Maria via Digitalmars-
d-learn wrote:
 Hi
 I have read much, before to write here.
 How can i store, to an int variable, the milliseconds of the
 current time?
 It's simple, but i don't find the solution.
 Thank you very much.

 Giovanni Di Maria
You mean if the time is currently 15:46:52.7205007, you want an integer value that's 720? The way to get the current wall-clock time would be Clock.currTime in std.datetime.systime (or you can just import the entire package if you prefer). That returns a SysTime with the current time (and by default, it's in the current time zone, though you can give it a different time zone if you want). If you want to get the portion of the time that's less than a second, that's in the fracSecs property. It returns a Duration (which is from core.time). If you want the Duration in milliseconds, then you can use its total property with the template argument "msecs". It does however return a long, not an int, so if you want an int, then you'll need to cast or use to!int (though the code would never return more than 999 or less than -999 for milliseconds, so the extre checks in std.conv.to aren't particularly useful in this case). So, the code would look something like SysTime st = Clock.currTime(); Duration fracSecs = st.fracSecs; immutable msecs = cast(int)fracSecs.total!"msecs"; or immutable msecs = cast(int)Clock.currTime().fracSecs.total!"msecs"; https://dlang.org/phobos/std_datetime_systime.html#.Clock.currTime https://dlang.org/phobos/std_datetime_systime.html#.SysTime https://dlang.org/phobos/core_time.html#Duration https://dlang.org/phobos/core_time.html#.Duration.total It probably would have been easier to figure that out on your own previously, but after I split the module up, the package and module-level documentation talked like it was a single module and needed to be updated. So, someone went and ripped most of it out on the theory that what was there was worse than nothing, meaning that there's a distinct lack of module-level documentation, and the package-level documentation is rather poor. I've been meaning to go back and redo that top-level documenation and generally go over the std.datetime documentation as a whole again, but I haven't gotten around to it yet. - Jonathan M Davis
Jul 12 2019
parent Giovanni Di Maria <calimero22 yahoo.it> writes:
On Friday, 12 July 2019 at 07:21:58 UTC, Jonathan M Davis wrote:
 On Friday, July 12, 2019 12:51:28 AM MDT Giovanni Di Maria via 
 Digitalmars- d-learn wrote:
 [...]
You mean if the time is currently 15:46:52.7205007, you want an integer value that's 720? [...]
Perfect Jonathan . Thank you very very much!!!!! Giovanni
Jul 12 2019