Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. How to convert minutes into milliseconds...

How to convert minutes into milliseconds...

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpai-codingtutorialquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tim Cools 0
    wrote on last edited by
    #1

    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

    OriginalGriffO S A 3 Replies Last reply
    0
    • T Tim Cools 0

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • T Tim Cools 0

        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

        S Offline
        S Offline
        supercat9
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Tim Cools 0

          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

          A Offline
          A Offline
          Andrew Rissing
          wrote on last edited by
          #4

          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:

          P 1 Reply Last reply
          0
          • A Andrew Rissing

            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:

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Andrew Rissing wrote:

            it will be unreliable

            Yes, but you can rely on it to be unreliable.

            A L 2 Replies Last reply
            0
            • P PIEBALDconsult

              Andrew Rissing wrote:

              it will be unreliable

              Yes, but you can rely on it to be unreliable.

              A Offline
              A Offline
              Andrew Rissing
              wrote on last edited by
              #6

              Indeed. Lets just hope it doesn't become unreliable to rely on its unreliability.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                Andrew Rissing wrote:

                it will be unreliable

                Yes, but you can rely on it to be unreliable.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.


                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.


                  A Offline
                  A Offline
                  Andrew Rissing
                  wrote on last edited by
                  #8

                  Nice little collection of useful DateTime snippets. I added that to my bookmarks. Thanks. :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups