admin
2025-04-08 5c9991be21f57781573f04961ec511ac2938ea3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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, &timestamp);
        // ¸ñʽ»¯Ê±¼äΪ×Ö·û´®
        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;
    }
 
};