1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2017 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 * 6 * This software is provided 'as-is', without any express or implied warranty. 7 * In no event will the authors be held liable for any damages arising from the 8 * use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not claim 15 * that you wrote the original software. If you use this software in a product, 16 * an acknowledgment in the product documentation would be appreciated but is 17 * not required. 18 * 19 * 2. Altered source versions must be plainly marked as such, and must not be 20 * misrepresented as being the original software. 21 * 22 * 3. This notice may not be removed or altered from any source distribution 23 */ 24 25 /** 26 * Clock is a lightweight class for measuring time. 27 * 28 * Its provides the most precise time that the underlying OS can achieve 29 * (generally microseconds or nanoseconds). It also ensures monotonicity, which 30 * means that the returned time can never go backward, even if the system time 31 * is changed. 32 * 33 * Example: 34 * --- 35 * auto clock = Clock(); 36 * ... 37 * Duration duration1 = clock.getElapsedTime(); 38 * ... 39 * Duration duration2 = clock.restart(); 40 * --- 41 * 42 * $(PARA The Duration value ($(I core.time.Duration)) returned by the clock can 43 * then be converted to a number of seconds, milliseconds or even microseconds.) 44 * 45 * See_Also: 46 * $(LINK2 https://dlang.org/library/core/time/duration.html, Duration) 47 */ 48 module dsfml.system.clock; 49 50 public import core.time; 51 52 /** 53 * Utility class that measures the elapsed time. 54 */ 55 class Clock 56 { 57 package MonoTime m_startTime; 58 private alias currTime = MonoTime.currTime; 59 60 /// Default constructor. 61 this() 62 { 63 m_startTime = currTime; 64 } 65 66 /// Destructor 67 ~this() 68 { 69 import dsfml.system.config; 70 mixin(destructorOutput); 71 } 72 73 /** 74 * Get the elapsed time. 75 * 76 * This function returns the time elapsed since the last call to `restart()` 77 * (or the construction of the instance if `restart()` has not been called). 78 * 79 * Returns: Time elapsed. 80 */ 81 Duration getElapsedTime() const 82 { 83 return currTime - m_startTime; 84 } 85 86 /** 87 * Restart the clock. 88 * 89 * This function puts the time counter back to zero. It also returns the 90 * time elapsed since the clock was started. 91 * 92 * Returns: Time elapsed. 93 */ 94 Duration restart() 95 { 96 MonoTime now = currTime; 97 auto elapsed = now - m_startTime; 98 m_startTime = now; 99 100 return elapsed; 101 } 102 } 103 104 unittest 105 { 106 version(DSFML_Unittest_System) 107 { 108 import std.stdio; 109 import dsfml.system.sleep; 110 import std.math; 111 112 writeln("Unit test for Clock"); 113 114 Clock clock = new Clock(); 115 116 writeln("Counting Time for 5 seconds.(rounded to nearest second)"); 117 118 while(clock.getElapsedTime().total!"seconds" < 5) 119 { 120 writeln(clock.getElapsedTime().total!"seconds"); 121 sleep(seconds(1)); 122 } 123 124 writeln(); 125 } 126 }