Created
January 29, 2019 19:42
-
-
Save ProjectInitiative/d98b8fb4db6e5e95b8e75f30ddc35e79 to your computer and use it in GitHub Desktop.
Used to format nanoseconds into HH:MM:SS.ms formatting as well as managing the actual elapsed time in between pauses relative to the system clock time regardless of how many times the user decides to pause and resume. Current functions did not return enough detail to achieve the required accuracy. Provided files are in Java and C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
Created by Kyle on 3/8/2015. | |
Note: This class is simply a calculation/String formatting class designed to calculate the elapsed time between two clock readings. | |
To fully integrate this into a practical use please use a thread to call either getElapsedTime or getElapsedMilliTime and display that text to the screen. | |
*/ | |
namespace CommonLibrary.Helpers | |
{ | |
public class Stopwatch | |
{ | |
public long startTime { protected get; set; } | |
public long oldTime = 0; | |
private long _elapsedT; | |
public long elapsedT | |
{ | |
get | |
{ | |
long curTime = DateTime.Now.Ticks; | |
_elapsedT = (oldTime + (curTime - startTime)); | |
hrs = (_elapsedT / (36000000000)); | |
remain = (_elapsedT % (36000000000)); | |
mins = (remain / (600000000)); | |
remain = (remain % (600000000)); | |
secs = (remain / 10000000); | |
remain = (remain % 10000000); | |
millis = remain; | |
tenths = millis / 10000; | |
return _elapsedT; | |
} | |
set { _elapsedT = value; } | |
} | |
private long millis; | |
private long tenths; | |
private long secs; | |
private long mins; | |
private long hrs; | |
private long remain; | |
public long pace { get; set; } = 0; | |
private String elapsedTime; | |
public void ResetTime() | |
{ | |
startTime = DateTime.Now.Ticks; | |
oldTime = 0; | |
} | |
public String GetElapsedTime() | |
{ | |
elapsedT = elapsedT; | |
elapsedTime = FormatTime(_elapsedT); | |
return elapsedTime; | |
} | |
public String GetElapsedMilliTime() | |
{ | |
elapsedT = elapsedT; | |
elapsedTime = FormatMilliTime(_elapsedT); | |
return elapsedTime; | |
} | |
//Returns a String formatted in: H:MM:SS | |
public static String FormatTime(long time) | |
{ | |
long hrs = (time / (36000000000)); | |
long remain = (time % (36000000000)); | |
long mins = (remain / (600000000)); | |
remain = (remain % (600000000)); | |
long secs = (remain / 10000000); | |
remain = (remain % 10000000); | |
long millis = remain; | |
long tenths = millis / 100000; | |
String elapsedTime; | |
if (hrs > 0) | |
{ | |
if (mins < 10 && secs < 10) | |
elapsedTime = (hrs + ":0" + mins + ":0" + secs); | |
else if (mins < 10) | |
elapsedTime = (hrs + ":0" + mins + ":" + secs); | |
else if (secs < 10) | |
elapsedTime = (hrs + ":" + mins + ":0" + secs); | |
else | |
elapsedTime = (hrs + ":" + mins + ":" + secs); | |
} | |
else | |
{ | |
if (mins < 10 && secs < 10) | |
elapsedTime = ("0" + mins + ":0" + secs); | |
else if (mins < 10) | |
elapsedTime = ("0" + mins + ":" + secs); | |
else if (secs < 10) | |
elapsedTime = (mins + ":0" + secs); | |
else | |
elapsedTime = (mins + ":" + secs); | |
} | |
return elapsedTime; | |
} | |
//Returns a String formatted in: H:MM:SS.ms | |
public static String FormatMilliTime(long time) | |
{ | |
long hrs = (time / (36000000000)); | |
long remain = (time % (36000000000)); | |
long mins = (remain / (600000000)); | |
remain = (remain % (600000000)); | |
long secs = (remain / 10000000); | |
remain = (remain % 10000000); | |
long millis = remain; | |
long tenths = millis / 100000; | |
String elapsedTime; | |
if (hrs > 0) | |
{ | |
if (mins < 10 && secs < 10 && tenths < 10) | |
elapsedTime = (hrs + ":0" + mins + ":0" + secs + ".0" + tenths); | |
else if (mins < 10 && tenths < 10) | |
elapsedTime = (hrs + ":0" + mins + ":" + secs + ".0" + tenths); | |
else if (secs < 10 && tenths < 10) | |
elapsedTime = (hrs + ":" + mins + ":0" + secs + ".0" + tenths); | |
else if (mins < 10 && secs < 10) | |
elapsedTime = (hrs + ":0" + mins + ":0" + secs + "." + tenths); | |
else if (mins < 10) | |
elapsedTime = (hrs + ":0" + mins + ":" + secs + "." + tenths); | |
else if (secs < 10) | |
elapsedTime = (hrs + ":" + mins + ":0" + secs + "." + tenths); | |
else if (tenths < 10) | |
elapsedTime = (hrs + ":" + mins + ":" + secs + ".0" + tenths); | |
else | |
elapsedTime = (hrs + ":" + mins + ":" + secs + "." + tenths); | |
} | |
else | |
{ | |
if (mins < 10 && secs < 10 && tenths < 10) | |
elapsedTime = ("0" + mins + ":0" + secs + ".0" + tenths); | |
else if (mins < 10 && tenths < 10) | |
elapsedTime = ("0" + mins + ":" + secs + ".0" + tenths); | |
else if (secs < 10 && tenths < 10) | |
elapsedTime = (mins + ":0" + secs + ".0" + tenths); | |
else if (mins < 10 && secs < 10) | |
elapsedTime = ("0" + mins + ":0" + secs + "." + tenths); | |
else if (mins < 10) | |
elapsedTime = ("0" + mins + ":" + secs + "." + tenths); | |
else if (secs < 10) | |
elapsedTime = (mins + ":0" + secs + "." + tenths); | |
else if (tenths < 10) | |
elapsedTime = (mins + ":" + secs + ".0" + tenths); | |
else | |
elapsedTime = (mins + ":" + secs + "." + tenths); | |
} | |
return elapsedTime; | |
} | |
public void Pause() | |
{ | |
oldTime = _elapsedT; | |
} | |
public void Resume(long newStartTime) | |
{ | |
startTime = newStartTime; | |
} | |
} | |
} | |
/*MIT License | |
Copyright (c) [year] [fullname] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
/* | |
Created by Kyle on 3/8/2015. | |
Note: This class is simply a calculation/String formatting class designed to calculate the elapsed time between two clock readings. | |
To fully integrate this into a practical use please use a thread to call either getElapsedTime or getElapsedMilliTime and display that text to the screen. | |
*/ | |
public class Stopwatch implements Serializable | |
{ | |
private long startTime, | |
oldTime = 0, | |
elapsedT; | |
private long millis, | |
tenths, | |
secs, | |
mins, | |
hrs, | |
remain; | |
private long pace=0; | |
private String elapsedTime; | |
public void setStartTime(long startTime) | |
{ | |
this.startTime=startTime; | |
} | |
public void resetTime() | |
{ | |
setStartTime(System.currentTimeMillis()); | |
oldTime=0; | |
} | |
public String getElapsedTime() | |
{ | |
elapsedT=getElapsedT(); | |
elapsedTime=formatTime(elapsedT); | |
return elapsedTime; | |
} | |
public String getElapsedMilliTime() | |
{ | |
elapsedT=getElapsedT(); | |
elapsedTime=formatMilliTime(elapsedT); | |
return elapsedTime; | |
} | |
public long getElapsedT() | |
{ | |
long curTime=System.currentTimeMillis(); | |
elapsedT=(oldTime+(curTime-startTime)); | |
hrs=(elapsedT/(3600000)); | |
remain=(elapsedT%(3600000)); | |
mins=(remain/(60000)); | |
remain=(remain%(60000)); | |
secs=(remain/1000); | |
remain=(remain%1000); | |
millis=remain; | |
tenths=millis/10; | |
return elapsedT; | |
} | |
//Returns a String formatted in: H:MM:SS | |
public static String formatTime(long time) | |
{ | |
long hrs=(time/(3600000)); | |
long remain=(time%(3600000)); | |
long mins=(remain/(60000)); | |
remain=(remain%(60000)); | |
long secs=(remain/1000); | |
remain=(remain%1000); | |
long millis=remain; | |
long tenths=millis/10; | |
String elapsedTime; | |
if(hrs>0) | |
{ | |
if(mins<10 && secs<10) | |
elapsedTime=(hrs+":0"+mins+":0"+secs); | |
else if(mins<10) | |
elapsedTime=(hrs+":0"+mins+":"+secs); | |
else if(secs<10) | |
elapsedTime=(hrs+":"+mins+":0"+secs); | |
else | |
elapsedTime=(hrs+":"+mins+":"+secs); | |
} | |
else | |
{ | |
if(mins<10 && secs<10) | |
elapsedTime=("0"+mins+":0"+secs); | |
else if(mins<10) | |
elapsedTime=("0"+mins+":"+secs); | |
else if(secs<10) | |
elapsedTime=(mins+":0"+secs); | |
else | |
elapsedTime=(mins+":"+secs); | |
} | |
return elapsedTime; | |
} | |
//Returns a String formatted in: H:MM:SS.ms | |
public static String formatMilliTime(long time) | |
{ | |
long hrs=(time/(3600000)); | |
long remain=(time%(3600000)); | |
long mins=(remain/(60000)); | |
remain=(remain%(60000)); | |
long secs=(remain/1000); | |
remain=(remain%1000); | |
long millis=remain; | |
long tenths=millis/10; | |
String elapsedTime; | |
if(hrs>0) | |
{ | |
if(mins<10 && secs<10 && tenths<10) | |
elapsedTime=(hrs+":0"+mins+":0"+secs+".0"+tenths); | |
else if(mins<10 && tenths<10) | |
elapsedTime=(hrs+":0"+mins+":"+secs+".0"+tenths); | |
else if(secs<10 && tenths<10) | |
elapsedTime=(hrs+":"+mins+":0"+secs+".0"+tenths); | |
else if(tenths<10) | |
elapsedTime=(hrs+":"+mins+":"+secs+".0"+tenths); | |
else if(mins<10 && secs<10) | |
elapsedTime=(hrs+":0"+mins+":0"+secs+"."+tenths); | |
else if(mins<10) | |
elapsedTime=(hrs+":0"+mins+":"+secs+"."+tenths); | |
else if(secs<10) | |
elapsedTime=(hrs+":"+mins+":0"+secs+"."+tenths); | |
else | |
elapsedTime=(hrs+":"+mins+":"+secs+"."+tenths); | |
} | |
else | |
{ | |
if(mins<10 && secs<10 && tenths<10) | |
elapsedTime=("0"+mins+":0"+secs+".0"+tenths); | |
else if(mins<10 && tenths<10) | |
elapsedTime=("0"+mins+":"+secs+".0"+tenths); | |
else if(secs<10 && tenths<10) | |
elapsedTime=(mins+":0"+secs+".0"+tenths); | |
else if(tenths<10) | |
elapsedTime=(mins+":"+secs+".0"+tenths); | |
else if(mins<10 && secs<10) | |
elapsedTime=("0"+mins+":0"+secs+"."+tenths); | |
else if(mins<10) | |
elapsedTime=("0"+mins+":"+secs+"."+tenths); | |
else if(secs<10) | |
elapsedTime=(mins+":0"+secs+"."+tenths); | |
else | |
elapsedTime=(mins+":"+secs+"."+tenths); | |
} | |
return elapsedTime; | |
} | |
public void pause() | |
{ | |
oldTime=getElapsedT(); | |
} | |
public void resume(Long newStartTime) | |
{ | |
startTime=newStartTime; | |
} | |
} | |
/* | |
MIT License | |
Copyright (c) [year] [fullname] | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment