我写了一个函数来获取格式的当前日期和时间:DD-MM-YYYY HH:MM:SS.它可以工作,但让我们说,它非常难看.我怎么能做同样的事情,但更简单?
string currentDateToString()
{
time_t now = time(0);
tm *ltm = localtime(&now);
string dateString = "", tmp = "";
tmp = numToString(ltm->tm_mday);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1 + ltm->tm_mon);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1900 + ltm->tm_year);
dateString += tmp;
dateString += " ";
tmp = numToString(ltm->tm_hour);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp; …Run Code Online (Sandbox Code Playgroud) 我一直在升级一些旧代码,并且尽可能地尝试更新到c ++ 11.以下代码是我用来在程序中显示时间和日期的方法
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
const std::string return_current_time_and_date() const
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
Run Code Online (Sandbox Code Playgroud)
我想使用std :: chrono(或类似的)以类似的格式输出当前时间和日期,但我不确定如何这样做.任何帮助将不胜感激.谢谢
在C++ 11中是否有一种简单的方法可以使用与正在使用的ostream相关联的语言环境的相应格式规则来打印当前的挂起时间?
我真正想做的是这样的事情:
myStream << std::chrono::system_clock::now();
Run Code Online (Sandbox Code Playgroud)
并根据与之相关的任何区域设置打印日期和时间myStream.C++ 11提供put_time,但它需要格式化字符串,我希望格式由与流关联的语言环境确定.还有time_put和time_put_byname,但基于在cppreference.com的例子中,这些功能配合使用put_time.
有没有简单的方法来打印时间点值而不手动格式化它?
假设我有time_t和tm结构.我不能使用Boost而是使用MFC.我怎样才能使它像下面的字符串?
Mon Apr 23 17:48:14 2012
Run Code Online (Sandbox Code Playgroud)
使用sprintf是唯一的方法吗?
是使用std :: get_time解析字符串所需的分隔符吗?我无法找到它的说法.我正在尝试解析ISO日期/时间字符串,例如"20140105T123456" - 例如:
例如,
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <ctime>
int main(int argc, char* argv[])
{
std::tm t = { 0 };
// fails
std::istringstream ss("20141105T123456");
ss >> std::get_time(&t, "%Y%m%dT%H%M%S");
// works
//std::istringstream ss("2014 11 05 T 12 34 56");
//ss >> std::get_time(&t, "%Y %m %d T %H %M %S");
std::ostringstream os;
std::cout << std::put_time(&t, "%c") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2013.我尝试在Linux上构建,但我最新版本的GCC是4.7.3,它似乎还不支持get_time.
我的愚蠢错误或需要分隔符?
我在新编译器上遇到了一个老问题.尝试使用时打印当前时间std::chrono我收到以下错误:
src/main.cpp:51:30: error: ‘put_time’ is not a member of ‘std’
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
^~~
Run Code Online (Sandbox Code Playgroud)
违规片段没什么特别之处:
#include <chrono>
...
auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
Run Code Online (Sandbox Code Playgroud)
这看起来非常像GCC 4.x系列中返回的错误,如同所引用的那样:
这个理论唯一的问题是我的GCC是现代的:
$ g++ --version
g++ (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS …Run Code Online (Sandbox Code Playgroud) 是否有一种简单的方法可以获得适用于iOS,OSX,Linux和Windows的时间(17:30,01:20 ......等)?
如果没有Windows方式和posix方式或东西?
谢谢