Re: Get milliseconds from time and construct time based on milliseconds
On Tuesday, 28 May 2024 at 23:18:46 UTC, Steven Schveighoffer wrote: On Tuesday, 28 May 2024 at 18:41:02 UTC, bauss wrote: On Tuesday, 28 May 2024 at 18:29:17 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 28 May 2024 at 17:37:42 UTC, bauss wrote: I have two questions that I can't seem to find a solution to after looking at std.datetime. First question is how do I get the current time but in milliseconds? Second is how do I construct a time ex. systime or datetime based on milliseconds? Thanks Unixtime might be what you want: import std; import std.datetime; import std.stdio; void main() { // Get the current time in the UTC time zone auto currentTime = Clock.currTime(); // Convert the time to the Unix epoch (1970-01-01T00:00:00Z) Duration unixTime = currentTime - SysTime(DateTime(1970, 1, 1), UTC()); You can do `SysTime(unixTimeToStdTime(0))` to get a SysTime that is at the unix epoch. Also figured out the second question based on your result. Simply doing: ``` SysTime(DateTime(1970, 1, 1), UTC()) + dur!"msecs"(milliseconds) ``` Seems to work. Note there is an `msecs` function: ```d SysTime(unixTimeToStdTime(0)) + milliseconds.msecs; ``` https://dlang.org/phobos/std_datetime_systime.html#unixTimeToStdTime https://dlang.org/phobos/core_time.html#msecs -Steve Thanks! That's a lot cleaner
Re: Get milliseconds from time and construct time based on milliseconds
On Tuesday, 28 May 2024 at 18:41:02 UTC, bauss wrote: On Tuesday, 28 May 2024 at 18:29:17 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 28 May 2024 at 17:37:42 UTC, bauss wrote: I have two questions that I can't seem to find a solution to after looking at std.datetime. First question is how do I get the current time but in milliseconds? Second is how do I construct a time ex. systime or datetime based on milliseconds? Thanks Unixtime might be what you want: import std; import std.datetime; import std.stdio; void main() { // Get the current time in the UTC time zone auto currentTime = Clock.currTime(); // Convert the time to the Unix epoch (1970-01-01T00:00:00Z) Duration unixTime = currentTime - SysTime(DateTime(1970, 1, 1), UTC()); You can do `SysTime(unixTimeToStdTime(0))` to get a SysTime that is at the unix epoch. Also figured out the second question based on your result. Simply doing: ``` SysTime(DateTime(1970, 1, 1), UTC()) + dur!"msecs"(milliseconds) ``` Seems to work. Note there is an `msecs` function: ```d SysTime(unixTimeToStdTime(0)) + milliseconds.msecs; ``` https://dlang.org/phobos/std_datetime_systime.html#unixTimeToStdTime https://dlang.org/phobos/core_time.html#msecs -Steve
Re: Get milliseconds from time and construct time based on milliseconds
On Tuesday, 28 May 2024 at 18:29:17 UTC, Ferhat Kurtulmuş wrote: On Tuesday, 28 May 2024 at 17:37:42 UTC, bauss wrote: I have two questions that I can't seem to find a solution to after looking at std.datetime. First question is how do I get the current time but in milliseconds? Second is how do I construct a time ex. systime or datetime based on milliseconds? Thanks Unixtime might be what you want: import std; import std.datetime; import std.stdio; void main() { // Get the current time in the UTC time zone auto currentTime = Clock.currTime(); // Convert the time to the Unix epoch (1970-01-01T00:00:00Z) Duration unixTime = currentTime - SysTime(DateTime(1970, 1, 1), UTC()); // Get the total milliseconds long milliseconds = unixTime.total!"msecs"; // Print the Unix time in milliseconds writeln("Unix time in milliseconds: ", milliseconds); } Thanks a lot. Also figured out the second question based on your result. Simply doing: ``` SysTime(DateTime(1970, 1, 1), UTC()) + dur!"msecs"(milliseconds) ``` Seems to work.
Re: Get milliseconds from time and construct time based on milliseconds
On Tuesday, 28 May 2024 at 17:37:42 UTC, bauss wrote: I have two questions that I can't seem to find a solution to after looking at std.datetime. First question is how do I get the current time but in milliseconds? Second is how do I construct a time ex. systime or datetime based on milliseconds? Thanks Unixtime might be what you want: import std; import std.datetime; import std.stdio; void main() { // Get the current time in the UTC time zone auto currentTime = Clock.currTime(); // Convert the time to the Unix epoch (1970-01-01T00:00:00Z) Duration unixTime = currentTime - SysTime(DateTime(1970, 1, 1), UTC()); // Get the total milliseconds long milliseconds = unixTime.total!"msecs"; // Print the Unix time in milliseconds writeln("Unix time in milliseconds: ", milliseconds); }
Get milliseconds from time and construct time based on milliseconds
I have two questions that I can't seem to find a solution to after looking at std.datetime. First question is how do I get the current time but in milliseconds? Second is how do I construct a time ex. systime or datetime based on milliseconds? Thanks
Re: Milliseconds
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
Re: Milliseconds
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
Milliseconds
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
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On Fri, 30 Jan 2015 15:26:11 -0500, Steven Schveighoffer wrote: D absolutely needs a way to say this is ONLY for implementation, it's not part of the API. private fits this bill EXACTLY. yep. every sane person recognizing D private symbols as hidden. and then... BOOM! The Hidden Gems Of D. signature.asc Description: PGP signature
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On 1/30/15 12:49 PM, Jonathan M Davis via Digitalmars-d-learn wrote: On Friday, January 30, 2015 12:30:35 FG via Digitalmars-d-learn wrote: On 2015-01-30 at 12:08, Vladimir Panteleev wrote: On Friday, 30 January 2015 at 11:04:47 UTC, FG wrote: Bug or correct behaviour? Bug: https://issues.dlang.org/show_bug.cgi?id=1238 https://github.com/D-Programming-Language/dmd/pull/3743 The fix is pretty much a one-liner. Probably 2.067 will already include it, right? Last I heard, no one had been able to convince Walter that private symbols should be hidden. They aren't in C++, but C++ doesn't have access levels for anything other than classes, so the effect is _very_ different. Another HUGE difference is that C++ generally splits API from implementation. When you import .h files, you don't also import private symbols which may be defined or used in the .cpp file. D absolutely needs a way to say this is ONLY for implementation, it's not part of the API. private fits this bill EXACTLY. Please do it. -Steve
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On Friday, January 30, 2015 12:30:35 FG via Digitalmars-d-learn wrote: On 2015-01-30 at 12:08, Vladimir Panteleev wrote: On Friday, 30 January 2015 at 11:04:47 UTC, FG wrote: Bug or correct behaviour? Bug: https://issues.dlang.org/show_bug.cgi?id=1238 https://github.com/D-Programming-Language/dmd/pull/3743 The fix is pretty much a one-liner. Probably 2.067 will already include it, right? Last I heard, no one had been able to convince Walter that private symbols should be hidden. They aren't in C++, but C++ doesn't have access levels for anything other than classes, so the effect is _very_ different. Though maybe someone convinced Walter that the status quo is stupid, and I didn't see it. I don't know. Pretty much everyone else thinks that it should be changed, so it'll probably be changed at some point, but who knows when. The fact that there's a PR for it will help, but it obviously isn't being dealt with particularly quickly, so there's really no way to know when it'll be merged (and it doesn't even fix the whole problem with private symbols - just some of it). It could be merged tomorrow, or it could be months from now. - Jonathan M Davis
Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
foreach(f; files)) { if (canFind(to!string(f), )) { writeln(whitespace found:); writeln(f); Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds } else continue; } Error: module app struct std.regex.Thread(DataIndex) is private Error: no property 'sleep' for type 'void' What's wrong? Why sleeping do not work?
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On 2015-01-30 at 12:08, Vladimir Panteleev wrote: On Friday, 30 January 2015 at 11:04:47 UTC, FG wrote: Bug or correct behaviour? Bug: https://issues.dlang.org/show_bug.cgi?id=1238 https://github.com/D-Programming-Language/dmd/pull/3743 The fix is pretty much a one-liner. Probably 2.067 will already include it, right?
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
Error: module app struct std.regex.Thread(DataIndex) is private Did you import core.thread?
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On 2015-01-30 at 11:55, FG wrote: Error: module app struct std.regex.Thread(DataIndex) is private Did you import core.thread? This is silly. Thread is internal to std.regex, yet when importing both std.regex and core.thread, you still get an error: src.d(10): Error: core.thread.Thread at ..\thread.d(514) conflicts with std.regex.Thread(Dat aIndex) at ..\src\phobos\std\regex.d(4588) The way around is of course the use of a fully qualified name: core.thread.Thread.sleep( dur!(msecs)( 50 ) ); but there really should be no need for this, since std.regex.Thread is private. Bug or correct behaviour?
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On Friday, January 30, 2015 10:39:44 Suliman via Digitalmars-d-learn wrote: foreach(f; files)) { if (canFind(to!string(f), )) { writeln(whitespace found:); writeln(f); Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds } else continue; } Error: module app struct std.regex.Thread(DataIndex) is private Error: no property 'sleep' for type 'void' What's wrong? Why sleeping do not work? Did you import std.regex but not core.thread? Or did you import std.regex with a local import and core.thread with a module-level import? Unfortunately, private symbols are visible and can cause symbol conflicts (even though they can't actually be used), so sometimes we end up with conflicts due to private symbols. Being more specific - e.g. core.Thread.sleep() - should fix the problem. But it's also possible that you failed to import core.thread in the first place, in which case, Thread.sleep isn't even visible to your code. - Jonathan M Davis
Re: Thread.sleep( dur!(msecs)( 50 ) ); // sleep for 50 milliseconds
On Friday, 30 January 2015 at 11:04:47 UTC, FG wrote: Bug or correct behaviour? Bug: https://issues.dlang.org/show_bug.cgi?id=1238