在c++中是否有跨平台的方法来获取当前的日期和时间?
当前回答
我需要一种方法在每次更新列表时插入当前日期-时间。 简单地说,这似乎很有效。
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
int main()
{ //initialize variables
time_t now;
//blah..blah
/*each time I want the updated stamp*/
now=time(0);cout<<ctime(&now)<<"blah_blah";
}
其他回答
#include <Windows.h>
void main()
{
//Following is a structure to store date / time
SYSTEMTIME SystemTime, LocalTime;
//To get the local time
int loctime = GetLocalTime(&LocalTime);
//To get the system time
int systime = GetSystemTime(&SystemTime)
}
std C库提供time()。 这是从纪元开始的秒数,可以使用标准C函数转换为日期和H:M:S。Boost还有一个时间/日期库供您检查。
time_t timev;
time(&timev);
ctime std::
为什么到目前为止只有评论提到了ctime ?
#include <ctime>
#include <iostream>
int main()
{
std::time_t result = std::time(nullptr);
std::cout << std::ctime(&result);
}
输出
周二 12月 27 5:21:29 PM 2011
c++与C共享它的日期/时间函数。tm结构可能是c++程序员最容易使用的结构——下面打印今天的日期:
#include <ctime>
#include <iostream>
int main() {
std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t);
std::cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< "\n";
}
(给谷歌人)
还有Boost::date_time:
#include <boost/date_time/posix_time/posix_time.hpp>
boost::posix_time::ptime date_time = boost::posix_time::microsec_clock::universal_time();
推荐文章
- 为什么这个结合赋值和相等检查的if语句返回true?
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- 如何减去日期/时间在JavaScript?
- c++枚举类可以有方法吗?
- 将Date对象转换为日历对象
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 将缺失的日期添加到熊猫数据框架中
- 如何防止ifelse()从日期对象转化为数字对象
- 在函数的签名中添加关键字
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?