#pragma once
|
#include <iostream>
|
#include <ctime>
|
#include <string>
|
#include <sstream>
|
#include <vector>
|
|
class TimeUtil {
|
public:
|
static std::string getNowTime() {
|
std::time_t now = std::time(nullptr);
|
|
// ½«Ê±¼ä´Áת»»Îª±¾µØÊ±¼ä
|
std::tm local_time;
|
localtime_s(&local_time , &now);
|
|
// ¸ñʽ»¯Ê±¼äΪ×Ö·û´®
|
char time_str[20];
|
//%Y-%m-%d %H:%M:%S
|
std::strftime(time_str, sizeof(time_str), "%H:%M:%S", &local_time);
|
// Êä³öµ±Ç°Ê±¼ä
|
return std::string(time_str);
|
}
|
|
/// <summary>
|
/// »ñÈ¡µ±Ç°Ê±¼ä´Á(ºÁÃë)
|
/// </summary>
|
/// <returns></returns>
|
static long getNowTimestamp() {
|
return GetTickCount64();
|
}
|
|
static std::string getNowTime(std::string format) {
|
std::time_t now = std::time(nullptr);
|
|
// ½«Ê±¼ä´Áת»»Îª±¾µØÊ±¼ä
|
std::tm local_time;
|
localtime_s(&local_time, &now);
|
|
// ¸ñʽ»¯Ê±¼äΪ×Ö·û´®
|
char time_str[20];
|
//%Y-%m-%d %H:%M:%S
|
std::strftime(time_str, sizeof(time_str), format.c_str(), &local_time);
|
// Êä³öµ±Ç°Ê±¼ä
|
return std::string(time_str);
|
}
|
|
static std::string toFormatString(time_t timestamp ,std::string format) {
|
// ½«Ê±¼ä´Áת»»Îª±¾µØÊ±¼ä
|
std::tm local_time;
|
localtime_s(&local_time, ×tamp);
|
// ¸ñʽ»¯Ê±¼äΪ×Ö·û´®
|
char time_str[20];
|
//%Y-%m-%d %H:%M:%S
|
std::strftime(time_str, sizeof(time_str), format.c_str(), &local_time);
|
// Êä³öµ±Ç°Ê±¼ä
|
return std::string(time_str);
|
}
|
|
|
static time_t toTimestamp(string str_time, std::string format) {
|
std::tm timeinfo = {};
|
std::istringstream ss(str_time);
|
ss >> std::get_time(&timeinfo, format.c_str());
|
time_t timestamp = mktime(&timeinfo);
|
return timestamp;
|
}
|
|
// ʱ¼äÏà¼õ£¬·µ»ØÏà²îµÄs
|
static int trade_time_sub(string time_str_1, string time_str_2) {
|
int time_1[3], time_2[3];
|
// Çиîʱ¼ä
|
std::string token;
|
std::vector<std::string> results = StringUtil::split(time_str_1,":");
|
int index = 0;
|
for (std::vector<std::string>::iterator e = results.begin(); e != results.end(); e++) {
|
time_1[index] = stoi(*e);
|
index++;
|
}
|
results = StringUtil::split(time_str_2, ":");
|
index = 0;
|
for (std::vector<std::string>::iterator e = results.begin(); e != results.end(); e++) {
|
time_2[index] = stoi(*e);
|
index++;
|
}
|
|
int split_time = 11 * 3600 + 30 * 60 + 0;
|
int time_1_int = time_1[0] * 3600 + time_1[1] * 60 + time_1[2];
|
int time_2_int = time_2[0] * 3600 + time_2[1] * 60 + time_2[2];
|
if (split_time > time_1_int && split_time < time_2_int) {
|
time_2_int = time_2_int - 90 * 60;
|
}
|
else if (split_time > time_2_int && split_time < time_1_int) {
|
time_2_int = time_2_int + 90 * 60;
|
}
|
return time_1_int - time_2_int;
|
}
|
|
};
|