TDME2  1.9.200
Time.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tdme/tdme.h>
5 
6 #include <chrono>
7 #include <ctime>
8 #include <string>
9 
10 using std::chrono::duration_cast;
11 using std::chrono::high_resolution_clock;
12 using std::chrono::milliseconds;
13 using std::string;
14 
15 /**
16  * Time utility class
17  * @author mahula
18  */
20 {
21 public:
22 
23  /**
24  * Retrieve current time in milliseconds
25  * @return int64_t
26  */
27  inline static int64_t getCurrentMillis() {
28  return high_resolution_clock::now().time_since_epoch() / milliseconds(1);
29  }
30 
31  /**
32  * Get date/time as string
33  * @param format format, see strftime
34  * @return date/time as string
35  */
36  inline static string getAsString(const string& format = "%Y-%m-%d %H:%M:%S") {
37  // see: https://stackoverflow.com/questions/34963738/c11-get-current-date-and-time-as-string
38  std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
39  char timeString[256] = {0};
40  std::strftime(timeString, sizeof(timeString), format.c_str(), std::localtime(&now));
41  return string(timeString);
42  }
43 };
Time utility class.
Definition: Time.h:20
static string getAsString(const string &format="%Y-%m-%d %H:%M:%S")
Get date/time as string.
Definition: Time.h:36
static int64_t getCurrentMillis()
Retrieve current time in milliseconds.
Definition: Time.h:27