您的位置:首页 > 其它

如何获得系统时间

2008-04-19 22:16 176 查看
 

如何获得系统时间
方案1 优点:仅使用C标准库;缺点:只能精确到秒级
#include <time.h>
#include <stdio.h>
int main( void )
{
    char tempstr[100];
    _strdate(tempstr);
    cout<<tempstr<<endl;
    time_t t = time( 0 );
    char tmp[64];
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z", localtime(&t) );
    cout<<tmp<<endl;
    char time[100];
    _strtime(time);
    cout<<time<<endl;
}
输出:
01/15/08
2008/01/15 10:27:38 Tuesday 本年第015天 中国标准时间
10:27:38
-------------------------------------------------------------------------------
方案2 优点:能精确到毫秒级;缺点:使用了windows API
#include <windows.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;
}
输出:
2008/01/15 10:28:33.984 星期2
-------------------------------------------------------------------------------
方案3 优点:利用系统函数
#include<stdlib.h>
#include<iostream>
using namespace std;
void main(){
     system("time");
}
可以改变电脑的时间设定
-------------------------------------------------------------------------------
方案4:
#include <stdio.h>
#include <time.h>
void main ()
{
    time_t rawtime;
    struct tm * timeinfo;
   time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "/007The current date/time is: %s", asctime (timeinfo) );
    printf( "%4d-%02d-%02d%02d-%02d-%02d/n",
         1900+timeinfo->tm_year,1+timeinfo->tm_mon,
         timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
}
输出:
The current date/time is: Tue Jan 15 11:14:10 2008
2008-01-15 11-14-10
-------------------------------------------------------------------------------
方案5
VC中:
CTime CurrentTime=CTime::GetCurrentTime();
CString strTime;  
strTime.Format("%d:%d:%d",CurrentTime.GetHour(), CurrentTime.GetMinute(),CurrentTime.GetSecond());
输出:
2008/01/15 3 11:23:47
-------------------------------------------------------------------------------
方案6:获得间隔时间:
#include <iostream.h>
#include <time.h>
int main(int argc[],char*args[])
{
    double tBegin = clock();
    cout<<"起始时间:"<<tBegin<<endl;
    for(int i=0;i<100000000;i++)
    {
    }
    double tEnd = clock();
    cout<<"结束时间:"<<tEnd<<endl;
    cout<<"耗时:"<<(tEnd - tBegin)/1000<<"秒!"<<endl;
    return 0;
}
输出:
起始时间:0
结束时间:437
耗时:0.437秒!

#include<time.h>  //C语言的头文件
#include<stdio.h>  //C语言的I/O
void main()
{
time_t now;    //实例化time_t结构
struct tm  *timenow;    //实例化tm结构指针
time(&now);
//time函数读取现在的时间(国际标准时间非北京时间),然后传值给now

timenow = localtime(&now);
//localtime函数把从time取得的时间now换算成你电脑中的时间(就是你设置的地区)

printf("Local time is %s/n",asctime(timenow));
//上句中asctime函数把时间转换成字符,通过printf()函数输出
}
注释:time_t是一个在time.h中定义好的结构体。而tm结构体的原形如下:
struct tm
{
  int tm_sec;//seconds 0-61
  int tm_min;//minutes 1-59
  int tm_hour;//hours 0-23
  int tm_mday;//day of the month 1-31
  int tm_mon;//months since jan 0-11
  int tm_year;//years from 1900
  int tm_wday;//days since Sunday, 0-6
  int tm_yday;//days since Jan 1, 0-365
  int tm_isdst;//Daylight Saving time indicator
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息