`
dawning126
  • 浏览: 44944 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C++获取系统时间

    博客分类:
  • C++
阅读更多

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

#include <time.h>

#include <stdio.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;

}

size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);

根据格式字符串生成字符串。

struct tm *localtime(const time_t *timer);

取得当地时间,localtime获取的结果由结构tm返回

返回的字符串可以依下列的格式而定:

%a 星期几的缩写。Eg:Tue

%A 星期几的全名。 Eg: Tuesday

%b 月份名称的缩写。

%B 月份名称的全名。

%c 本地端日期时间较佳表示字符串。

%d 用数字表示本月的第几天 (范围为 00 至 31)。日期

%H 用 24 小时制数字表示小时数 (范围为 00 至 23)。

%I 用 12 小时制数字表示小时数 (范围为 01 至 12)。

%j 以数字表示当年度的第几天 (范围为 001 至 366)。

%m 月份的数字 (范围由 1 至 12)。

%M 分钟。

%p 以 ''AM'' 或 ''PM'' 表示本地端时间。

%S 秒数。

%U 数字表示为本年度的第几周,第一个星期由第一个周日开始。

%W 数字表示为本年度的第几周,第一个星期由第一个周一开始。

%w 用数字表示本周的第几天 ( 0 为周日)。

%x 不含时间的日期表示法。

%X 不含日期的时间表示法。 Eg: 15:26:30

%y 二位数字表示年份 (范围由 00 至 99)。

%Y 完整的年份数字表示,即四位数。 Eg:2008

%Z(%z) 时区或名称缩写。Eg:中国标准时间

%% % 字符。

//方案二 优点:能精确到毫秒级;缺点:使用了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;

}

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

//此文件必须是c++文件

#include<stdlib.h>

#include<iostream>

using namespace std;

void main()

{

system("time");

}

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

//此文件必须是c++文件

#include<iostream>

#include<ctime>

using namespace std;

int main()

{

time_t now_time;

now_time = time(NULL);

cout<<now_time;

return 0;

}

1,时间的获取:

通过time()函数来获得日历时间(Calendar Time),其原型为:time_t time(time_t * timer);

#include "stdafx.h"

#include "time.h"

#include "stdio.h"

#include "stdlib.h"

int main(void)

{

struct tm t;            //定义tm时间结构,用来存储时间格式的数据信息

time_t t_of_day;     //定义time_t时间结构

t.tm_year=2006-1900;//以1900年为标准计算时间

t.tm_mon=6;                 //为结构体成员赋值

t.tm_mday=1;

t.tm_hour=0;

t.tm_min=0;

t.tm_sec=1;

t.tm_isdst=0;

t_of_day=mktime(&t);

// 使用mktime()函数将用tm结构表示的时间转化为日历时间:time_t型变量。其函数原型如下:time_t mktime(struct tm * timeptr);ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,返回值是char*型的字符串。

return 0;

}

2,时间的储存,通过预定义的两种结构来存储:

1,日历时间(Calendar Time)是通过time_t数据类型来表示的,用time_t表示的时间(日历时间)是从一个时间点(例如:1970年1月1日0时0分0秒)到此时的秒数。在time.h中,我们也可以看到time_t是一个长整型数:

#ifndef _TIME_T_DEFINED

typedef long time_t;         /* 时间值 */

#define _TIME_T_DEFINED      /* 避免重复定义 time_t */

#endif

2,在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

struct tm {

        int tm_sec;     /* 秒 – 取值区间为[0,59] */

        int tm_min;     /* 分 - 取值区间为[0,59] */

        int tm_hour;    /* 时 - 取值区间为[0,23] */

        int tm_mday;    /* 一个月中的日期 - 取值区间为[1,31] */

        int tm_mon;     /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

        int tm_year;    /* 年份,其值等于实际年份减去1900 */

        int tm_wday;    /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */

        int tm_yday;    /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */

        int tm_isdst;   /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/

        };

3,时间的显示:

time.h 头文件中提供了asctime()函数(参数为tm结构指针)和ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,两者的返回值 都是char*型的字符串。返回的时间格式为:星期几 月份 日期 时:分:秒 年\n\0;time.h还提供了两种不同的函数将日历时间(一个用time_t表示的整数)转换为我们平时看到的把年月日时分秒分开显示的时间格式 tm:

struct tm * gmtime(const time_t *timer);

gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间

struct tm * localtime(const time_t * timer);localtime()函数是将日历时间转化为本地时间

#include <stdafx.h>

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

       struct tm *local,*ptr; //定义tm结构指针存储时间信息

       time_t t;                       //时间结构或者对象

       t=time(NULL);                     //获取当前系统的日历时间

       //通过time()函数来获得日历时间(Calendar Time),

       //其原型为:time_t time(time_t * timer);

       local=localtime(&t);//localtime()函数是将日历时间转化为本地时间

       printf("Local hour is: %d\n",local->tm_hour);//输出tm结构体的时间成员

       printf("UTC hour is: %d\n",local->tm_hour);

       //local=gmtime(&t);

       //gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),

       //并返回一个tm结构体来保存这个时间 

       ptr=gmtime(&t);//将日历时间转化为世界标准时间

       printf("The UTC time is %s\n",asctime(ptr)); //格式化输出世界标准时间

       printf("The local time is %s\n",ctime(&t));//输出本地时间

       /*asctime()函数(参数为tm结构指针)和ctime()函数(参数为time_t结构)将时间以固定的格式显示出来,两者的返回值都是char*型的字符串。返回的时间格式为:星期几 月份 日期 时:分:秒 年\n\0 */

       return 0;

}

4,时间差的计算:

所用函数:C/C++中的计时函数是clock(),而与其相关的数据类型是clock_t。在MSDN中对clock函数定义如下:

clock_t clock( void );函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,clock_t是一个长整形数,保存时间的数据类型。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一 秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

每 过千分之一秒(1毫秒),调用clock()函数返回的值就加1,时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变 CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。 double difftime(time_t time1, time_t time0);这个函数来计算时间差。

#include "stdafx.h"

#include "time.h"

#include "stdio.h"

#include "stdlib.h"

int main(void)

{

    time_t c_start,t_start, c_end,t_end; 

    c_start = clock();

       t_start = time(NULL) ;

    system("pause") ;

       c_end = clock();

    t_end = time(NULL) ;

    printf("The pause used %f ms by time().\n",difftime(c_end,c_start)) ;

       printf("The pause used %f s by clock().\n",difftime(t_end,t_start)) ;

    system("pause");

    return 0;

}

5,时间的其他用途

用作随机数的种子,由于时间获得的实际上是一个double类型的长整数,通过time(NULL)函数获得,作为srand(time(NULL))的种子产生随机数比较好。

#include "stdafx.h"

#include "time.h"

#include "stdio.h"

#include "stdlib.h"

int main(void)

{

       srand(time(NULL));

       //设置种子,如果将这个函数注释掉,每次运行程序得到的随机数十相同的

       for(int i=0;i<100;i++)

       {

              printf("%d\t",rand());

       }

    system("pause");

    return 0;

}
分享到:
评论
1 楼 ZeaLoVe 2011-11-26  
所以C++真是蛋疼,好多东西支持的都不给力啊。为了一个时间去用WindowsAPI也太扯了。。

相关推荐

Global site tag (gtag.js) - Google Analytics