问题标题: 找到个获取当前时间可至微秒的程序

1
2
已解决
刘仁杰
刘仁杰
资深守护
资深守护

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<cctype>
#include<Windows.h>
#include<cmath>
#include<conio.h>
#include<ctime>
#include<cstdlib>
#include<ratio>
#include<chrono>
using namespace std;
std::time_t getTimeStamp() {
    std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>tp = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
    auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(tp.time_since_epoch());
    std::time_t timestamp = tmp.count();
    return timestamp;
}
struct SystemTime {
    int Year, Month, Day;
    string Week;
    int Hour, Minute, Second, millisecond;
};
SystemTime GetsTheSystemTime(bool Year = 1, bool Month = 1, bool Day = 1, bool Week = 1, bool Hour = 1, bool Minute = 1, bool Second = 1, bool Millisecond = 1) {
    time_t NowTime;
    time(&NowTime);
    SystemTime Time;
    tm TM;
    clock_t start, ends;
    start = clock();
    ends = clock();
    localtime_s(&TM, &NowTime);
    if (Year == 1) Time.Year = TM.tm_year + 1900;
    if (Month == 1) Time.Month = TM.tm_mon + 1;
    if (Day == 1) Time.Day = TM.tm_mday;
    if (Hour == 1) Time.Hour = TM.tm_hour;
    if (Minute == 1) Time.Minute = TM.tm_min;
    if (Second == 1) Time.Second = TM.tm_sec;
    string week[15] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
    if (Week == 1) Time.Week = week[TM.tm_wday];
    std::time_t milli = getTimeStamp();
    auto mTime = std::chrono::microseconds(milli);
    auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::microseconds>(mTime);
    auto tt = std::chrono::system_clock::to_time_t(tp);
    std::tm* now = std::gmtime(&tt);
    if (Millisecond == 1) Time.millisecond = milli % 1000;
    return Time;
}
void HideOrShowTheConsoleCursor(bool Flag = true) {
    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO CursorInfo;
    GetConsoleCursorInfo(Handle, &CursorInfo);
    CursorInfo.bVisible = Flag;
    SetConsoleCursorInfo(Handle, &CursorInfo);
}
void HighSpeedScreenClearing() {
    HANDLE SpeedScreen = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD HighScreen = { 0, 0 };
    SetConsoleCursorPosition(SpeedScreen, HighScreen);
}
int main() {
    HideOrShowTheConsoleCursor(false);
    while (true) {
        SystemTime Time=GetsTheSystemTime();
        cout << Time.Year << "/" << Time.Month << "/" << Time.Day << " " << Time.Week << " " << Time.Hour << ":" << Time.Minute << ":";
        if (Time.Second <= 9) {
            cout << "0";
        }
        cout << Time.Second << "." << Time.millisecond;
        HighSpeedScreenClearing();
    }
    return 0;
}

//似乎Dev C++会报错 visual studio可以


0
已采纳
王耀斌
王耀斌
高级守护
高级守护

刘仁杰

刘仁杰

中级守护
中级守护

把豆给我吧!!!!

0
吴皓轩
吴皓轩
新手天翼
新手天翼

额…………………………………………

0
0
0
0
0
0
薛乘志
薛乘志
初级启示者
初级启示者

这么麻烦?

用c++标准库中的chrono就行了啊

0
0
0
0
我要回答