百度空间 | 百度首页 
 
查看文章
 
C++中获得当前系统时间
2008-06-10 19:42

//方案— 优点:仅使用C标准库;缺点:只能精确到秒级

#include <time.h>
#include <string.h>

int main( void )
{
    time_t t = time( 0 );
    char tmp[64];
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",localtime(&t) );
    puts( tmp );
    return 0;
}

//方案二 优点:能精确到毫秒级;缺点:使用了windows API

#include <time.h>
#include <stdio.h>

int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);
return 0;
}

//方案三,优点:利用系统函数,还能修改系统时间

#include <time.h>
#include <windows.h>

using namespace std;
void main()
{
    system("time");
}

//方案四,将当前时间折算为秒级,再通过相应的时间换算即可

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
time_t now_time;
now_time = time(NULL);
cout< return 0;
}

//另附测试自己程序运行时间的代码;

#include <string>
#include <ctime>
#include <windows.h>
using namespace std;

int main()
{
/*

方法一:

double start = (double)time(NULL)/CLOCKS_PER_SEC;

Sleep(3000);
double end = (double)time(NULL)/CLOCKS_PER_SEC;
cout << end-start << endl;
*/

//方法二:
time_t b = time(NULL);
Sleep(3000);
time_t e = time(NULL);
cout << b << endl << e << endl;
cout << e - b << endl;
return 0;
}


类别:c++ | 添加到搜藏 | 浏览() | 评论 (0)
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     
 
精彩相册
   
     

©2009 Baidu