您的位置:首页 > 编程语言 > C语言/C++

[C++STDlib基础]关于日期时间的操作——C++标准库头文件<ctime>

2013-06-21 18:09 573 查看
网上教程

总结

/*
A.头文件<ctime>
#if _GLOBAL_USING && !defined(RC_INVOKED)
_STD_BEGIN
1.四个数据类型
using _CSTD clock_t; using _CSTD size_t;
using _CSTD time_t; using _CSTD tm;
2.九个时间函数
using _CSTD asctime; using _CSTD clock; using _CSTD ctime;
using _CSTD difftime; using _CSTD gmtime; using _CSTD localtime;
using _CSTD mktime; using _CSTD strftime; using _CSTD time;
_STD_END
#endif // _GLOBAL_USING //
B.四个数据类型
1.clock_t: <==>长整数long。多少秒,计时单位 。clock_t clock( void );
2.size_t: <==>正整数unsigned int。与系统有关的正整数,如sizeof()返回的就是size_t,常常表示字节长度。size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );
3.time_t: <==>长整数long  ;time_t mktime( struct tm *time );time_t time( time_t *time );
4.tm:时间结构体(年月日时分秒星期); tm *gmtime( const time_t *time ); tm *localtime( const time_t *time );

clock()返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,而sleep(5)并不占用cpu资源,导致start1和end1返回的值一样。time(&temp)返回从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。总之,用time_t计时才是人们正常意识上的秒数,而clock_t计时所表示的是占用CPU的时钟单元。

C.九个时间函数
char *asctime( const struct tm *ptr ); 转换日期和时间为相应的ASCII码,返回字符串格式:星期,月,日,小时,分,秒,年
char *ctime( const time_t *time ); 把日期和时间转换为字符串;返回字符串格式:星期,月,日,小时,分,秒,年;与asctime()只是参数不同
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time ); 将时间格式化成字符串
clock_t clock( void ); 返回值是硬件滴答数。 函数返回自程序开始运行的处理器时间,如果无可用信息,返回-1。 转换返回值以秒记, 返回值除以CLOCKS_PER_SECOND.
double difftime( time_t time2, time_t time1 ); 计算两个时刻之间的时间差(单位秒)。
struct tm *gmtime( const time_t *time );把时刻(日期和时间)转换为格林威治(GMT)时间
struct tm *localtime( const time_t *time );返回本地日历时间. 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtimes函数转换后的时间没有经过时区变换,是UTC时间 。
time_t mktime( struct tm *time ); 将时间转换为自1970年1月1日以来失去时间的秒数
time_t time( time_t *time ); 返回系统的当前日历时间;然后调用localtime将time_t所表示的CUT时间转换为本地时间
日历时间time_t,是用“从一个标准时间点(1970年1月1日00:00:00)到此时的时间经过的秒数”来表示的时间。
time() : 1270271767
time()->localtime()->mktime():1270271767
*/

时间操作: clock_t -------tm:转换

clock_t clock( void ); 

 Clock program (function )

double difftime( time_t time2, time_t time1 ); 

 Return difference between two times (function )

time_t mktime( _inout_ tm *time );

 Convert tm structure to time_t (function )

time_t time( _out_ time_t *time );

 Get current time (function )

时间转换: clock_t-----tm ------ char*: 转换

char *asctime( const struct tm *ptr );

Convert tm structure to string (function )

char *ctime( const time_t *time );

Convert time_t value to string (function )

struct tm *gmtime( const time_t *time );

Convert time_t to tm as UTC time (function )

struct tm *localtime( const time_t *time );

Convert time_t to tm as local time (function )

size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

Format time as string (function )

 使用函数顺序有:
1.time()-->ctime()
2.time()-->localtime()/gmtime()-->asctime()

3.time()-->localtime()->mktime()/strftime()

实例

#include <conio.h>
#include <cmath>
#include <ctime>
#include <iostream>
using namespace std;
void test0()//计算时间差。计数2000年到现在的秒数
{
time_t timer;
time(&timer);// <==>timer=time(NULL);
tm y2k;
y2k.tm_year=100;//int tm_year; /* years since 1900 */
y2k.tm_mon=y2k.tm_hour=y2k.tm_min=y2k.tm_sec=0;
y2k.tm_mday=1;
double sconds=difftime(timer,mktime(&y2k));
printf("%f seconds since January 1,2000 int the current timezone\n",sconds);//425141662.000000
printf("%.f seconds since January 1,2000 int the current timezone\n",sconds);//425141662
}
int GetPrimeNum(int n)//求质数个数
{
int freq=n-1;
for (int i=2;i <= n;i++)
{
for(int j=sqrt((float)i); j > 1;j--)
{
if (i % j == 0)
{
freq --;
break;
}
}
}
return freq;
}
void test1()//计算时间差(毫秒级的)。计算复杂函数所用的时间(单位秒)。当然也可以计算到毫秒,clock()返回的值本身就是毫秒级的
{
clock_t t=clock();
printf("<1234567的质数,num=%d\n",GetPrimeNum(1234567));//95360
t=clock()-t;
printf("It took me %d clicks(%.f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);//10735  11
}
void test2()//计算1989-04-30是星期几
{
const char* weekday[]={"日","一","二","三","四","五","六"};
tm date;
time_t t=time(NULL);
date=*localtime(&t);
printf("当前的日期时间:%s",ctime(&t));
printf("当前的日期时间:%s",asctime(&date));
date.tm_year =1989-1900;
date.tm_mon =4-1;
date.tm_mday=30;
mktime(&date);/* call mktime: timeinfo->tm_wday will be set */
printf("1989-04-30的这一天是星期%s\n%s",weekday[date.tm_wday],asctime(&date));
}
void test3()//显示当前日期时间
{
time_t t;
t=time(NULL);
cout<<"当前本地的日期时间:"<<ctime(&t)<<endl;//Fri Jun 21 16:57:18 2013
cout<<"当前本地的日期时间:"<<asctime(localtime(&t))<<endl;//Fri Jun 21 16:57:18 2013
cout<<"当前UTC的日期时间:"<<asctime(gmtime(&t))<<endl;//Fri Jun 21 08:57:18 2013
char buffer[80];
tm* timer=localtime(&t);
strftime(buffer,80,"习惯表示:%Y-%m-%d %H:%M:%S %A",timer);//2013-06-21 17:28:32 Friday
cout<<buffer<<endl;
strftime(buffer,80,"习惯表示:%x %X %a",timer);//06/21/13 17:28:32 Fri
cout<<buffer<<endl;
strftime(buffer,80,"习惯表示:%m/%d/%y %I:%M:%S %p",timer);//06/21/13 05:28:32 PM
cout<<buffer<<endl;
}
void Test(char h)
{
cout<<"press key===="<<h<<endl;
switch(h)
{
case '0':  test0();break;
case '1':  test1();break;
case '2':  test2();break;
case '3':  test3();break;
case 27:
case 'q':exit(0);break;
default:cout<<"default "<<h<<endl;break;
}
}
void main()
{
while(1)
{
Test(getch());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: