1 /* Invisible Vector Library
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // this essentially duplicates std.datetime.StopWatch, but meh...
27 State mState
= State
.Stopped
;
37 string
toString () const @trusted {
39 auto t
= toBuffer(buf
[]);
43 nothrow @trusted @nogc:
44 this (State initState
) @trusted {
45 if (initState
== State
.Running
) start();
48 this (bool startit
) @trusted {
52 @property const pure {
53 auto state () { pragma(inline
, true); return mState
; }
54 bool stopped () { pragma(inline
, true); return (mState
== State
.Stopped
); }
55 bool running () { pragma(inline
, true); return (mState
== State
.Running
); }
56 bool paused () { pragma(inline
, true); return (mState
== State
.Paused
); }
59 @property ulong micro () const {
60 final switch (mState
) {
61 case State
.Stopped
: case State
.Paused
: return mAccumMicro
;
62 case State
.Running
: return mAccumMicro
+(clockMicro
-mSTimeMicro
);
66 // this also pauses it
67 @property void micro (ulong value
) {
70 mState
= State
.Stopped
;
73 @property ulong milli () const { pragma(inline
, true); return micro
/1000; }
74 @property ulong milliRounded () const { pragma(inline
, true); return micro
/1000+(micro
%500 >= 500); }
78 mSTimeMicro
= clockMicro
;
83 mState
= State
.Running
;
84 mSTimeMicro
= clockMicro
;
89 mState
= State
.Running
;
90 mSTimeMicro
= clockMicro
;
94 if (mState
== State
.Running
) {
95 mAccumMicro
+= clockMicro
-mSTimeMicro
;
96 mState
= State
.Stopped
;
101 if (mState
== State
.Running
) {
102 mAccumMicro
+= clockMicro
-mSTimeMicro
;
103 mState
= State
.Paused
;
108 if (mState
== State
.Paused
) {
109 mState
= State
.Running
;
110 mSTimeMicro
= clockMicro
;
111 } else if (mState
== State
.Stopped
) {
116 // 128 chars should be enough for everyone
117 char[] toBuffer (char[] dest
) const nothrow @trusted @nogc {
118 import core
.stdc
.stdio
: snprintf
;
119 char[128] buf
= void;
121 final switch (mState
) {
122 case State
.Stopped
: case State
.Paused
: d
= mAccumMicro
; break;
123 case State
.Running
: d
= mAccumMicro
+(clockMicro
-mSTimeMicro
); break;
125 immutable uint micro
= cast(uint)(d
%1000);
127 immutable uint milli
= cast(uint)(d
%1000);
129 immutable uint seconds
= cast(uint)(d
%60);
131 immutable uint minutes
= cast(uint)(d
%60);
133 immutable uint hours
= cast(uint)d
;
135 if (hours
) len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%u:%02u:%02u.%03u", hours
, minutes
, seconds
, milli
);
136 else if (minutes
) len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%u:%02u.%03u", minutes
, seconds
, milli
);
137 else if (seconds
) len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%u.%03u", seconds
, milli
);
138 else if (milli
>= 10) len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%ums", milli
);
139 else if (micro
!= 0) len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%ums:%umcs", milli
, micro
);
140 else len
= cast(uint)snprintf(buf
.ptr
, buf
.length
, "%ums", milli
);
141 if (len
> dest
.length
) len
= cast(uint)dest
.length
;
142 dest
.ptr
[0..len
] = buf
.ptr
[0..len
];
143 return dest
.ptr
[0..len
];