如何使用c++;我的时间计划? 是我在C++中的程序。请帮忙。谢谢 void Time::showTime() { cout << "Your time in 24 hours military standard is " << hour << ":" << minute << endl; } 无效时间::showTime() { cout<<“您在24小时内的时间军事标准为”<<小时<<):“<<分钟<<结束; } cout

如何使用c++;我的时间计划? 是我在C++中的程序。请帮忙。谢谢 void Time::showTime() { cout << "Your time in 24 hours military standard is " << hour << ":" << minute << endl; } 无效时间::showTime() { cout<<“您在24小时内的时间军事标准为”<<小时<<):“<<分钟<<结束; } cout,c++,C++,这是为strftime设计的任务类型。它消除了使用setfill、setw等的大量工作: #include <iostream> #include <ctime> #include <string> enum conv {UTC, LOCAL}; std::string fmt(char const *fmt, time_t p=time(NULL), conv c = LOCAL) { char buffer[512]; struct

这是为
strftime
设计的任务类型。它消除了使用
setfill
setw
等的大量工作:

#include <iostream>
#include <ctime>
#include <string>

enum conv {UTC, LOCAL};

std::string fmt(char const *fmt, time_t p=time(NULL), conv c = LOCAL) {
    char buffer[512];

    struct tm n = c == LOCAL ? *localtime(&p) : *gmtime(&p);
    strftime(buffer, sizeof(buffer), fmt, &n);
    return std::string(buffer);
}

int main() {
    std::cout << fmt("Your time in 24 hours military standard is %H:%M\n");
}
#包括
#包括
#包括
枚举conv{UTC,LOCAL};
std::string fmt(char const*fmt,time\u t p=time(NULL),conv c=LOCAL){
字符缓冲区[512];
struct tm n=c==LOCAL?*localtime(&p):*gmtime(&p);
strftime(缓冲区、sizeof(缓冲区)、fmt和n);
返回std::字符串(缓冲区);
}
int main(){

std::cout并使用time()和gmtime(),添加一个#include,这个问题不应该因为“不清楚”而关闭。这里的问题一点也不含糊。@Zaibis:哦,哇。@Jonh Hey非常感谢。尝试过了,效果很好。@Zaibis也感谢你的更正。
#include <iostream>
#include <ctime>
#include <string>

enum conv {UTC, LOCAL};

std::string fmt(char const *fmt, time_t p=time(NULL), conv c = LOCAL) {
    char buffer[512];

    struct tm n = c == LOCAL ? *localtime(&p) : *gmtime(&p);
    strftime(buffer, sizeof(buffer), fmt, &n);
    return std::string(buffer);
}

int main() {
    std::cout << fmt("Your time in 24 hours military standard is %H:%M\n");
}