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  formattedTime = std::string(formattedCharArray);
42  }
43 
44  return formattedTime;
45  }
46 
52  static std::string FormatSeconds(int time)
53  {
54  std::string formattedTime = "00:00:00";
55 
56  if(time > 0)
57  {
58  int hours = floor(time / 60 / 60);
59  int minutes = floor((time - (hours * 60 * 60)) / 60);
60  int seconds = (time - (hours * 60 * 60) - (minutes * 60));
61 
62  if(hours > 999)
63  {
64  char formattedCharArray[10];
65  sprintf(formattedCharArray, "%04d:%02d:%02d", hours, minutes, seconds);
66  formattedTime = std::string(formattedCharArray);
67  }
68  else if(hours > 99)
69  {
70  char formattedCharArray[9];
71  sprintf(formattedCharArray, "%03d:%02d:%02d", hours, minutes, seconds);
72  formattedTime = std::string(formattedCharArray);
73  }
74  else
75  {
76  char formattedCharArray[8];
77  sprintf(formattedCharArray, "%02d:%02d:%02d", hours, minutes, seconds);
78  formattedTime = std::string(formattedCharArray);
79  }
80  }
81 
82  return formattedTime;
83  }
84 
88  static std::string Current()
89  {
90  char buffer[20];
91  time_t now = time(NULL);
92  strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
93 
94  return std::string(buffer);
95  }
96 };
97 
98 #endif // TIME_H_
static std::string FormatSeconds(int time)
Formats time to 00:00:00 format.
Definition: Time.h:52
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:88