首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在C++中获取当前时间和日期?

如何在C++中获取当前时间和日期?
EN

Stack Overflow用户
提问于 2018-02-07 00:05:48
回答 10查看 0关注 0票数 0

是否有支持跨平台的C++ 方法来获取当前日期和时间?

EN

Stack Overflow用户

发布于 2018-02-07 08:23:23

是的,你开业使用输入当前时区所指定的时间日期规则来完成这个操作。

代码语言:javascript
复制
#include <iostream>
#include <iterator>
#include <string>

class timefmt
{
public:
    timefmt(std::string fmt)
        : format(fmt) { }

    friend std::ostream& operator <<(std::ostream &, timefmt const &);

private:
    std::string format;
};

std::ostream& operator <<(std::ostream& os, timefmt const& mt)
{
    std::ostream::sentry s(os);

    if (s)
    {
        std::time_t t = std::time(0);
        std::tm const* tm = std::localtime(&t);
        std::ostreambuf_iterator<char> out(os);

        std::use_facet<std::time_put<char>>(os.getloc())
            .put(out, os, os.fill(),
                 tm, &mt.format[0], &mt.format[0] + mt.format.size());
    }

    os.width(0);

    return os;
}

int main()
{
    std::cout << timefmt("%c");
}

输出:Fri Sep 6 20:33:31 2013

票数 0
EN
查看全部 10 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档