Skip to content

Commit c82edb9

Browse files
committed
modify get timestamp
1 parent 551ce38 commit c82edb9

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

include/tigerapi/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <cpprest/details/basic_types.h>
1212
#include "win32.h"
1313

14+
OPENAPI_EXPORT utility::string_t convert_str(std::string s);
15+
1416
OPENAPI_EXPORT utility::string_t get_timestamp();
1517

1618
OPENAPI_EXPORT time_t date_string_to_timestamp(const utility::string_t &date_string);

src/utils.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ using namespace std;
1212
using namespace web;
1313
using namespace websocketpp;
1414

15+
utility::string_t convert_str(std::string s) {
16+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
17+
return utility::conversions::utf8_to_utf16(s);
18+
#else
19+
return s;
20+
#endif
21+
}
22+
1523
utility::string_t get_timestamp() {
16-
time_t t = time(NULL);
17-
utility::char_t tmp[32];
18-
strftime((char*)tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t));
19-
return utility::string_t(tmp);
20-
// struct tm tm;
2124
// time_t t = time(NULL);
22-
// gmtime_r(&t, &tm);
23-
// char timestamp[32];
24-
// strftime(timestamp, sizeof(timestamp), U("%Y-%m-%d %H:%M:%S"), &tm);
25-
// return utility::string_t(timestamp);
25+
// utility::char_t tmp[32];
26+
// strftime((char*)tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t));
27+
// return utility::string_t(tmp);
28+
auto now = std::chrono::system_clock::now();
29+
auto in_time_t = std::chrono::system_clock::to_time_t(now);
30+
std::stringstream ss;
31+
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
32+
std::string result = ss.str();
33+
return convert_str(result);
2634
}
2735

2836
time_t date_string_to_timestamp(const utility::string_t &date_string) {

0 commit comments

Comments
 (0)