时间相关的函数

1    标准C中的时间函数

Coordinated Universal Time(UTC):世界标准时间,即Greenwich Mean Time,GMT。标准时间通过tm类型表示:

structtm

{

int tm_sec;          // 秒,[0,59]

int tm_min;        // 分,[0,59]

int tm_hour;       // 时,[0,23]

int tm_mday;     // 日期,[1,31]

int tm_mon;       // 月份,[0,11]

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

int tm_wday;      // 星期,[0,6],0代表星期天,1代表星期一

int tm_yday;       // 天数,[0,365],其中0代表1月1日

};

 

Calendar Time:日历时间,是一个相对时间,表示从一个标准时间点到现在经过的秒数。这个标准时间点对不同的编译器来说会有所不同。日历时间通过time_t类型表示:

typedeflong time_t;

 

 

常用的两种时间结构:

#include<time.h>                                        #include<sys/time.h>

structtimespec                                            structtimeval

{                                                                       {

time_t    tv_sec;          //seconds                      time_t                  tv_sec;          // seconds

long       tv_nsec;        // nanoseconds            suseconds_t      tv_usec;       // microseconds

}                                                                        }

1.1         时间格式转化

time_tmktime( struct tm * timeptr );              // 将tm结构的时间转化为日历时间

structtm * gmtime( const time_t *timer );      //将日历时间转化为世界标准时间

structtm * localtime( const time_t * timer );   //将日历时间转化为本地标准时间

 

asctime( )和ctime( )将日历时间用标准的格式显示出来:

char* asctime( const struct tm * timeptr );

char* ctime( const time_t *timeptr );

返回的标准时间格式为:星期   月份      日期      时:分:秒       年\n\0

 

strftime( )将时间转化为指定的格式:

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

返回strDest中存放的字符数,最多存放maxsize个字符。

%S        Secondas decimal number (00 – 59)

%M      Minuteas decimal number (00 – 59)

%I        Hourin 12-hour format (01 – 12)

%p        Currentlocale's A.M./P.M. indicator for 12-hour clock

%H       Hourin 24-hour format (00 – 23)

 

%a        Abbreviatedweekday name

%A       Fullweekday name

%w       Weekdayas decimal number (0 – 6; Sunday is 0)

%U       Weekof year as decimal number, with Sunday as first day of week (00 – 53)

%W      Weekof year as decimal number, with Monday as first day of week (00 – 53)

 

%d        Dayof month as decimal number (01 – 31)

%j         Dayof year as decimal number (001 – 366)

 

%b        Abbreviatedmonth name

%B       Fullmonth name

%m       Monthas decimal number (01 – 12)

 

%y        Yearwithout century, as decimal number (00 – 99)

%Y       Yearwith century, as decimal number

 

%c        Dateand time representation appropriate for locale

%x        Daterepresentation for current locale

%X       Timerepresentation for current locale

例如:

char tmpbuf[128];

strftime( tmpbuf, 128, "Today is %A,day %d of %B in the year %Y.\n", &today );

 

1.2         获得时间

time_ttime( time_t * timer );

既可以通过参数timer返回日历时间,也可以通过函数的返回值来返回日历时间。

 

clock_tclock( void );

返回从本进程开始到调用clock( )函数之间的clocktick数,称之为挂钟时间(wal-clock),在系统头文件中会define一秒钟内的clock tick数:CLOCKS_PER_SEC。

 

doubledifftime(time_t time1, time_t time0);

返回time1和time0之间的时间间隔,单位是秒。

 

void main()

{

      tm          * tUTCTime;

      tm          * tLocalTime;

      time_t    tTime;

 

      char       * strTime             =new char[100];

      char       * strUTCTime     = new char[100];

      char       * strLocalTime    = new char[100];

 

      tTime     = time( NULL );                              // 获得时间

 

      tUTCTime    = gmtime( &tTime );                // 将获得的时间转化为世界标准时间

      tLocalTime   = localtime( &tTime );              // 将获得的时间转化为本地标准时间

 

      strTime                = ctime( &tTime );             // 将获得的时间转化为可读的字符串

      strUTCTime        = asctime( tUTCTime );    // 将获得的世界标准时间转化为可读的字符串

      strLocalTime       = asctime( tLocalTime );   // 将获得的本地标准时间转化为可读的字符串

 

      cout<<"Time(time_t):"<<strTime<<endl;

      cout<<"LocalTime(tm): "<<strLocalTime<<endl;

      cout<<"UTCTime(tm): "<<strUTCTime<<endl;

}

2    Windows的时间

2.1         Count/Frequency #include <Windows.h>

BOOLQueryPerformanceFrequency( LARGE_INTEGER *lpFrequency );

BOOLQueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount );

先调用QueryPerformanceFrequency( )获得机器内部定时器的时钟频率,然后在事件发生之前和发生之后分别调用QueryPerformanceCounter( )获得的count之差,从而可以计算出事件经历的精确时间。

typedef union _LARGE_INTEGER

{

struct

{

DWORD LowPart;

LONG HighPart;

};

struct

{

DWORD LowPart;

LONG HighPart;

} u;

LONGLONG QuadPart;

} LARGE_INTEGER, *PLARGE_INTEGER;

 

void main()

{

      LARGE_INTEGERlgFre;

      LARGE_INTEGERlgBegCnt, lgEndCnt;

 

      QueryPerformanceFrequency(&lgFre );

      QueryPerformanceCounter(&lgBegCnt );

      QueryPerformanceCounter(&lgEndCnt );

      cout<<(lgEndCnt.QuadPart- lgBegCnt.QuadPart)/lgFre.QuadPart<<endl;

}

2.2         GetTickCount( ) #include <Windows.h>

The GetTickCount function retrieves thenumber of milliseconds that have elapsed since the system was started.

DWORDGetTickCount(void);

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值