Mania++
 All Classes Functions Variables Pages
Time.h
1 #ifndef TIME_H_
2 #define TIME_H_
3 
4 #include <string>
5 #include <sstream>
6 #include <math.h>
7 
8 //* Time
12 class Time
13 {
14 public:
20  static std::string FormatTime(int time)
21  {
22  std::string formattedTime = "0:00.000";
23 
24  if(time > 0)
25  {
26  std::stringstream timeAsStringstream;
27  timeAsStringstream << time;
28  std::string timeAsString = timeAsStringstream.str();
29 
30  int minutes = floor(time / (1000 * 60));
31  int seconds = floor((time - (minutes * 60 * 1000)) / 1000);
32  int tseconds = time;
33  if(timeAsString.length() > 3)
34  {
35  tseconds = atoi(timeAsString.substr((timeAsString.length() - 3)).c_str());
36  }
37 
38  char formattedCharArray[8];
39  sprintf(formattedCharArray, "%d:%02d.%03d", minutes, seconds, tseconds);
40 
41  return std::string(formattedCharArray);
42  }
43 
44  return formattedTime;
45  }
46 
50  static std::string Current()
51  {
52  char buffer[20];
53  time_t now = time(NULL);
54  strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
55 
56  return std::string(buffer);
57  }
58 };
59 
60 #endif // TIME_H_
Contains utilities to format map times.
Definition: Time.h:12
static std::string FormatTime(int time)
Formats time to 0:00.000 format.
Definition: Time.h:20
static std::string Current()
Returns the current time in yyyy-mm-dd hh:mm:ss format.
Definition: Time.h:50