How to convert minutes into milliseconds...
-
Found in production code: The standardTimer is the number of minutes. The result is the number of millideconds (standardTimer * 60000) // Specified delay... Start every xx minutes System.TimeSpan duration = new System.TimeSpan(0, 0, standardTimer, 0); DateTime dtStart = new DateTime(); dtStart = DateTime.Now + duration; string sDate = dtStart.ToString(); // Calcul delay string sCurrent = DateTime.Now.ToString("HH:mm:ss"); string sStart = dtStart.ToString("HH:mm:ss"); System.TimeSpan tsCurrent = System.TimeSpan.Parse(sCurrent); System.TimeSpan tsStart = System.TimeSpan.Parse(sStart); tsStart = tsStart - tsCurrent; return (int)tsStart.TotalMilliseconds;
blog: www.timcools.net >> code generation >> microframework
-
Found in production code: The standardTimer is the number of minutes. The result is the number of millideconds (standardTimer * 60000) // Specified delay... Start every xx minutes System.TimeSpan duration = new System.TimeSpan(0, 0, standardTimer, 0); DateTime dtStart = new DateTime(); dtStart = DateTime.Now + duration; string sDate = dtStart.ToString(); // Calcul delay string sCurrent = DateTime.Now.ToString("HH:mm:ss"); string sStart = dtStart.ToString("HH:mm:ss"); System.TimeSpan tsCurrent = System.TimeSpan.Parse(sCurrent); System.TimeSpan tsStart = System.TimeSpan.Parse(sStart); tsStart = tsStart - tsCurrent; return (int)tsStart.TotalMilliseconds;
blog: www.timcools.net >> code generation >> microframework
Nothing wrong with that code - perfectly good approach! :laugh: I particularly like the DateTime -> TimeSpan conversion via parse - classy. I do however feel that he has missed an opportunity to save some code space:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); TimeSpan duration = new TimeSpan(0, standardTimer, 0); sw.Start(); System.Threading.Sleep(duration); sw.Stop(); return sw.ElapsedMilliseconds;
Perhaps you should use this method instead?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
Found in production code: The standardTimer is the number of minutes. The result is the number of millideconds (standardTimer * 60000) // Specified delay... Start every xx minutes System.TimeSpan duration = new System.TimeSpan(0, 0, standardTimer, 0); DateTime dtStart = new DateTime(); dtStart = DateTime.Now + duration; string sDate = dtStart.ToString(); // Calcul delay string sCurrent = DateTime.Now.ToString("HH:mm:ss"); string sStart = dtStart.ToString("HH:mm:ss"); System.TimeSpan tsCurrent = System.TimeSpan.Parse(sCurrent); System.TimeSpan tsStart = System.TimeSpan.Parse(sStart); tsStart = tsStart - tsCurrent; return (int)tsStart.TotalMilliseconds;
blog: www.timcools.net >> code generation >> microframework
Perhaps the coder thought it was necessary to convert a DateTime into a TimeSpan before subtracting to yield a TimeSpan? In fact, the difference between two DateTime values is a TimeSpan. Not sure how to explain some of the other ugliness in the code, though. I can understand cases where one might want to e.g. calculate the number of milliseconds until the start of a minute 'n' minutes from now, but I'm unclear how any of the cases where this function wouldn't return StandardTimer*60000 would be useful.
-
Found in production code: The standardTimer is the number of minutes. The result is the number of millideconds (standardTimer * 60000) // Specified delay... Start every xx minutes System.TimeSpan duration = new System.TimeSpan(0, 0, standardTimer, 0); DateTime dtStart = new DateTime(); dtStart = DateTime.Now + duration; string sDate = dtStart.ToString(); // Calcul delay string sCurrent = DateTime.Now.ToString("HH:mm:ss"); string sStart = dtStart.ToString("HH:mm:ss"); System.TimeSpan tsCurrent = System.TimeSpan.Parse(sCurrent); System.TimeSpan tsStart = System.TimeSpan.Parse(sStart); tsStart = tsStart - tsCurrent; return (int)tsStart.TotalMilliseconds;
blog: www.timcools.net >> code generation >> microframework
Elegant or not, the code has a bug actually. It's using DateTime.Now in two places. Because the code is not saving that first value off to use later, it will be unreliable. :cool:
-
Elegant or not, the code has a bug actually. It's using DateTime.Now in two places. Because the code is not saving that first value off to use later, it will be unreliable. :cool:
Andrew Rissing wrote:
it will be unreliable
Yes, but you can rely on it to be unreliable.
-
Andrew Rissing wrote:
it will be unreliable
Yes, but you can rely on it to be unreliable.
Indeed. Lets just hope it doesn't become unreliable to rely on its unreliability.
-
Andrew Rissing wrote:
it will be unreliable
Yes, but you can rely on it to be unreliable.
it is a very mild form of unreliability, as you can perfectly predict when it will be shaky and when not. I did mention it in my datetime compendium[^] though. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
it is a very mild form of unreliability, as you can perfectly predict when it will be shaky and when not. I did mention it in my datetime compendium[^] though. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
Nice little collection of useful DateTime snippets. I added that to my bookmarks. Thanks. :)