time.cpp 904 Bytes
Newer Older
Matteo's avatar
update  
Matteo committed
1
2
3
4
5
6
7
8
9
#include "time.h"

/**
 * @brief Convert an int representing milliseconds to the corresponding Time Label string.
 *
 * @param ms the number of milliseconds.
 * @param delim the time separator
 * @return string the corresponding Time Label string.
 */
Matteo's avatar
update    
Matteo committed
10
std::string getTimeLabel(int ms, std::string delim) {
Matteo's avatar
update  
Matteo committed
11
12
13
14
15
16
17

	int mil = ms % 1000;
	int sec = ms / 1000;
	int min = (sec / 60) % 60;
	int hours = sec / 3600;
	sec = sec % 60;

Matteo's avatar
update    
Matteo committed
18
	std::string hoursStr = std::to_string(hours), minStr = std::to_string(min), secStr = std::to_string(sec), milStr = std::to_string(mil);
Matteo's avatar
update  
Matteo committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	if (hours < 10)
		hoursStr = "0" + hoursStr;
	if (min < 10)
		minStr = "0" + minStr;
	if (sec < 10)
		secStr = "0" + secStr;
	if (mil < 100) {
		if (mil < 10) {
			milStr = "00" + milStr;
		} else {
			milStr = "0" + milStr;
		}
	}

Matteo's avatar
update    
Matteo committed
33
    std::string timeLabel = hoursStr + delim + minStr + delim + secStr + delim + milStr;
Matteo's avatar
update  
Matteo committed
34
35
36

	return timeLabel;
}