admin
7 天以前 6cd92a169cbc0db35042f243a09d976fd3e1445c
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
#pragma once
#include <chrono>
class TimeUtil {
 
public:
    // »ñÈ¡µ±Ç°Ê±¼ä´Á
    static long getNowTimeStamp() {
        auto now = std::chrono::system_clock::now();
        // ×ª»»ÎªÊ±¼ä´Á
        auto now_s = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
        
        return now_s;
    }
 
    static string format(long timestamp, string format="%Y-%m-%d %H:%M:%S") {
        time_t rawtime = static_cast<time_t>(timestamp);
 
        // Ê¹ÓÃlocaltimeº¯Êý½«time_tÀàÐÍת»»Îªstruct tm½á¹¹Ì壬»ñÈ¡±¾µØÊ±¼ä
        struct tm* timeinfo;
        timeinfo = localtime(&rawtime);
    
        char buffer[80];
        strftime(buffer, sizeof(buffer), format.c_str(), timeinfo);
        // ½«charÊý×éת»»Îªstring
        return std::string(buffer);
    }
 
    static long timeStr2Seconds(string st) {
         int h = stoi(st.substr(0, 2));
        int m = stoi(st.substr(3, 2));
        int s = stoi(st.substr(6, 2));
        return h * 3600 + m * 60 + s;
    }
 
 
};