Linux 下c++程序中打印系统当前时间
2017-04-03 11:58
399 查看
//方案一,将当前时间折算为秒级,再通过相应的时间换算即可
//此文件必须是c++文件
/*
#include<iostream>
#include<ctime>
using namespace std;
string now() {
time_t t = time(0);
char buffer[9] = {0};
strftime(buffer, 9, "%H:%M:%S", localtime(&t));
return string(buffer);
}
int main()
{
cout<<now()<<endl;
return 0;
}
*/
方法二:boost库中的:
#include <stdio.h> // for sprintf()
#include <iostream> // for console output
#include <string> // for std::string
#include <boost/date_time/posix_time/posix_time.hpp>
//-----------------------------------------------------------------------------
// Format current time (calculated as an offset in current day) in this form:
//
// "hh:mm:ss.SSS" (where "SSS" are milliseconds)
//-----------------------------------------------------------------------------
std::string now_str()
{
// Get current time from the clock, using microseconds resolution
const boost::posix_time::ptime now =
boost::posix_time::microsec_clock::local_time();
// Get the time offset in current day
const boost::posix_time::time_duration td = now.time_of_day();
//
// Extract hours, minutes, seconds and milliseconds.
//
// Since there is no direct accessor ".milliseconds()",
// milliseconds are computed _by difference_ between total milliseconds
// (for which there is an accessor), and the hours/minutes/seconds
// values previously fetched.
//
const long hours = td.hours();
const long minutes = td.minutes();
const long seconds = td.seconds();
const long milliseconds = td.total_milliseconds() - ((hours * 3600 + minutes * 60 + seconds) * 1000);
//
// Format like this:
//
// hh:mm:ss.SSS
//
// e.g. 02:15:40:321
//
// ^ ^
// | |
// 123456789*12
// ---------10- --> 12 chars + \0 --> 13 chars should suffice
//
char buf[40];
sprintf(buf, "%02ld:%02ld:%02ld.%03ld",
hours, minutes, seconds, milliseconds);
return buf;
}
测试:
int main()
{
std::cout << now_str() << '\n';
}
///////////////////////////////////////////////////////////////////////////////
//此文件必须是c++文件
/*
#include<iostream>
#include<ctime>
using namespace std;
string now() {
time_t t = time(0);
char buffer[9] = {0};
strftime(buffer, 9, "%H:%M:%S", localtime(&t));
return string(buffer);
}
int main()
{
cout<<now()<<endl;
return 0;
}
*/
方法二:boost库中的:
#include <stdio.h> // for sprintf()
#include <iostream> // for console output
#include <string> // for std::string
#include <boost/date_time/posix_time/posix_time.hpp>
//-----------------------------------------------------------------------------
// Format current time (calculated as an offset in current day) in this form:
//
// "hh:mm:ss.SSS" (where "SSS" are milliseconds)
//-----------------------------------------------------------------------------
std::string now_str()
{
// Get current time from the clock, using microseconds resolution
const boost::posix_time::ptime now =
boost::posix_time::microsec_clock::local_time();
// Get the time offset in current day
const boost::posix_time::time_duration td = now.time_of_day();
//
// Extract hours, minutes, seconds and milliseconds.
//
// Since there is no direct accessor ".milliseconds()",
// milliseconds are computed _by difference_ between total milliseconds
// (for which there is an accessor), and the hours/minutes/seconds
// values previously fetched.
//
const long hours = td.hours();
const long minutes = td.minutes();
const long seconds = td.seconds();
const long milliseconds = td.total_milliseconds() - ((hours * 3600 + minutes * 60 + seconds) * 1000);
//
// Format like this:
//
// hh:mm:ss.SSS
//
// e.g. 02:15:40:321
//
// ^ ^
// | |
// 123456789*12
// ---------10- --> 12 chars + \0 --> 13 chars should suffice
//
char buf[40];
sprintf(buf, "%02ld:%02ld:%02ld.%03ld",
hours, minutes, seconds, milliseconds);
return buf;
}
测试:
int main()
{
std::cout << now_str() << '\n';
}
///////////////////////////////////////////////////////////////////////////////
相关文章推荐
- linux获取系统当前时间
- linux创建文件名添加当前系统日期时间的方法
- linux查看和修改当前系统时间
- shell 脚本每隔一段时间打印当前系统时间
- linux→查看当前系统时间和修改系统当前时间
- Linux下修改系统当前时间
- linux创建文件名添加当前系统日期时间的方法
- DTrace tricks and tips (1) - 打印当前系统时间
- linux下以系统当前时间前一天命名文件并打包压缩
- Linux系统环境编程(系统当前时间)
- Linux C++显示系统当前时间 time
- Linux获取系统当前时间(精确到毫秒)
- linux驱动层获取当前的系统时间
- Linux学习 设置、查询 当前系统时间
- linux java/jsp 获取系统当前时间问题
- Linux下用C获取当前系统时间
- Linux 获取当前系统时间(year-month-day h:m:s weekday )
- django 获取系统当前时间 和linux 系统当前时间不一致 问题处理。
- Linux获取系统当前时间(精确到毫秒)
- 【Linux学习】 写一个简单的Makefile编译源码获取当前系统时间