|  | 
				
					
	
		
			
 			Posted on 2008-11-12 19:50 chefZ  阅读(294) 评论(0)  编辑 收藏 引用   C/C++ code
Include head file time.h, though it's a C include file, C++ certainly can use it.
 Under C++, you can include <ctime> instead of <time.h>
 _____________________________________________________
 
 time.h
 
 
 @函数名称: localtime
 函数原型: struct tm *localtime(const time_t *timer)
 函数功能: 返回一个以tm结构表达的机器时间信息
 函数返回: 以tm结构表达的时间,结构tm定义如下:
 struct tm{
 int tm_sec;
 int tm_min;
 int tm_hour;
 int tm_mday;
 int tm_mon;
 int tm_year;
 int tm_wday;
 int tm_yday;
 int tm_isdst;
 };
 参数说明: timer-使用time()函数获得的机器时间
 所属文件: <time.h>
 
 #include <time.h>
 #include <stdio.h>
 #include <dos.h>
 int main()
 {
 time_t timer;
 struct tm *tblock;
 timer=time(NULL);
 tblock=localtime(&timer);
 printf("Local time is: %s",asctime(tblock));
 return 0;
 }
 
 
 @函数名称: asctime
 函数原型: char* asctime(struct tm * ptr)
 函数功能: 得到机器时间(日期时间转换为ASCII码)
 函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年
 参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到
 所属文件: <time.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 int main()
 {
 struct tm t;
 char str[80];
 t.tm_sec=1;
 t.tm_min=3;
 t.tm_hour=7;
 t.tm_mday=22;
 t.tm_mon=11;
 t.tm_year=56;
 t.tm_wday=4;
 t.tm_yday=0;
 t.tm_isdst=0;
 strcpy(str,asctime(&t));
 printf("%s",str);
 return 0;
 }
 
 
 @函数名称: ctime
 函数原型: char *ctime(long time)
 函数功能: 得到日历时间
 函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年
 参数说明: time-该参数应由函数time获得
 所属文件: <time.h>
 
 #include <stdio.h>
 #include <time.h>
 int main()
 {
 time_t t;
 time(&t);
 printf("Today's date and time: %s",ctime(&t));
 return 0;
 }
 
 
 @函数名称: difftime
 函数原型: double difftime(time_t time2, time_t time1)
 函数功能: 得到两次机器时间差,单位为秒
 函数返回: 时间差,单位为秒
 参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得
 所属文件: <time.h>
 
 #include <time.h>
 #include <stdio.h>
 #include <dos.h>
 #include <conio.h>
 int main()
 {
 time_t first, second;
 clrscr();
 first=time(NULL);
 delay(2000);
 second=time(NULL);
 printf("The difference is: %f seconds",difftime(second,first));
 getch();
 return 0;
 }
 
 
 @函数名称: gmtime
 函数原型: struct tm *gmtime(time_t *time)
 函数功能: 得到以结构tm表示的时间信息
 函数返回: 以结构tm表示的时间信息指针
 参数说明: time-用函数time()得到的时间信息
 所属文件: <time.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 #include <dos.h>
 char *tzstr="TZ=PST8PDT";
 int main()
 {
 time_t t;
 struct tm *gmt, *area;
 putenv(tzstr);
 tzset();
 t=time(NULL);
 area=localtime(&t);
 printf("Local time is:%s", asctime(area));
 gmt=gmtime(&t);
 printf("GMT is:%s", asctime(gmt));
 return 0;
 }
 
 
 @函数名称: time
 函数原型: time_t time(time_t *timer)
 函数功能: 得到机器的日历时间或者设置日历时间
 函数返回: 机器日历时间
 参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型
 所属文件: <time.h>
 
 #include <time.h>
 #include <stdio.h>
 #include <dos.h>
 int main()
 {
 time_t t;
 t=time();
 printf("The number of seconds since January 1,1970 is %ld",t);
 return 0;
 }
 
 
 @函数名称: tzset
 函数原型: void tzset(void)
 函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途
 函数返回:
 参数说明:
 所属文件: <time.h>
 
 #include <time.h>
 #include <stdlib.h>
 #include <stdio.h>
 int main()
 {
 time_t td;
 putenv("TZ=PST8PDT");
 tzset();
 time(&td);
 printf("Current time=%s",asctime(localtime(&td)));
 return 0;
 }
C/C++ code
/方案— 优点:仅使用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;
 }
#include <iostream.h> #include <windows.h>
 #include <stdlib.h>
 
 
 class Clock
 {
 int Hour;
 int Minute;
 int Second;
 public:
 Clock(int NewHour,int NewMinute,int NewSecond)
 {
 Hour=NewHour;
 Minute=NewMinute;
 Second=NewSecond;
 }
 bool setTime()
 {
 while(1)
 {
 cout < <"Do you want to set time?y/n:";
 char c=0;
 cin>>c;
 if(c=='y')
 {
 int hour,minute,second;
 cout < <"please set time:";
 cin>>hour>>minute>>second;
 Hour=hour;
 Minute=minute;
 Second=second;
 return 1;
 }
 else if(c=='n') return 0;
 else cout < <"Sorry,I can't understand it!" < <endl;
 }
 }
 
 
 
 
 void ShowTime()
 {
 cout < <Hour < <":" < <Minute < <":" < <Second < <endl;
 }
 void operator++(int)
 {
 Second++;
 if(Second>=60)
 {
 Second-=60;
 Minute++;
 if(Minute>=60)
 {
 Minute-=60;
 Hour++;
 Hour%=24;
 }
 }
 }
 };
 void main()
 {
 SYSTEMTIME s;
 GetLocalTime(&s);
 Clock clock(s.wHour,s.wMinute,s.wSecond);
 while(1)
 {
 
 Sleep(965);
 system("cls");
 clock++;
 clock.ShowTime();
 
 }
 }
 
 
 如果是MFC可以用CTime类,如果不是就用如下介绍的方法了
 很多情况下我们必须取得系统时间(像是交作业...),
 但是系统时间要如何取得呢?
 
 TurboC提供了time(); (被定义在 <time.h>中)这个函式供我们取得系统时间,
 当我们执行了time(NULL);时,该函式便会传回一个time_t型态(被定义在 <time.h>中)
 的数字,单位是"秒",所以我们应该先用time_t宣告一个变数,来接收time()的传回值:
 
 time_t T; //宣告变数T为time_t型态//
 T = time(NULL); //变数T等于time(NULL)的传回值//
 
 于是变数T便接收到了一个单位为"秒"的超大整数,而这个整数所代表的意义就是:
 从西元1970年1月1日0点0分0秒到目前所经过的秒数,
 哇!这不就是我们所要的系统时间吗....
 但是如果不能换成我们所熟悉的 <年/月/日时:分:秒>表示法就没意义了.
 
 <如何将系统时间转成一般表示法>
 
 好不容易取得的系统时间居然长这副德性,数学好的同学可能会想要排除万难,
 利用精湛的除法,解决闰年的问题,一个个将年/月/日/时/分/秒算出来...
 不过这样一来考试题目可能得带回家写,
 TurboC又提供了一个localtime(); (被定义在 <time.h>中)这个函式就有分割
 time_t型态资料的功能,根据on-line Help的介绍大致如下:
 
 struct tm * localtime( time_t * );
 ----------- --------- --------
 从这里可以看出localtime需要知道一个time_t型态的资料的位址,
 然后传回一个struct tm结构的资料的位址.
 
 这下问题来了,要告诉localtime() time_t变数的位址很简单,方法如下:
 time_t T; //宣告一个time_t型态的变数T //
 T = time(NULL); // T =系统时间//
 localtime(&T); //将T的位址&T丢给localtime() //
 
 这样就好了,但是struct tm到底是什么呢?
 (struct就是结构,用法类似class但无Member function )
 在on-line Help中看到tm的介绍如下:
 <TIME.H> //被 <time.h>定义//
 struct tm {
 int tm_sec; /*秒(0--59) */
 int tm_min; /*分(0--59) */
 int tm_hour; /*时(0--23) */
 int tm_mday; /*日of month (1--31) */
 int tm_mon; /*月(0--11记得自己 1) */
 int tm_year; /*年(要把这个值 1900才是西元年纪喔) */
 int tm_wday; /*日of Week (0--6; Sunday = 0) */
 int tm_yday; /*日of year (0--365) */
 int tm_isdst; /* 0 if daylight savings time is not in effect) */
 };
 看到这个就大概可以知道, localtime()先将我们给的time_t资料换算成
 年/月/日/时/分/秒,然后依照结构tm的顺序放在记忆体中,
 并将该记忆体位址传回.
 原来如此,那我们不就只要宣告一个tm型态的指标,然后将该指标指向
 localtime()的传回值,然后再读取该指标的内容就好啦^^马上试试:
 
 #include <time.h>
 #include <iostream.h>
 
 void main(void)
 {
 int year, mon, day; //要用来存日期的//
 int hour, min, sec; //要用来存时间的//
 time_t T; //要用来存系统时间的//
 struct tm *TimeP; //要用来指到localtime()传回的位址的//
 
 T = time(NULL); //将系统时间存到T //
 
 TimeP = localtime( &T ); // TimeP指到localtime算好的资料所在位址//
 
 //接下来就来看TimeP所指到的地方放了什么^^ //
 
 year = ( TimeP->tm_year ) 1900;
 mon = ( TimeP->tm_mon ) 1;
 day = ( TimeP->tm_mday );
 
 hour = ( TimeP->tm_hour );
 min = ( TimeP->tm_min );
 sec = ( TimeP->tm_sec );
 
 cout  < < year  < < "/"  < < mon  < < "/"  < < day  < < endl;
 cout  < < hour  < < ":"  < < min  < < ":"  < < sec  < < endl;
 }
 
 结果果然localtime()已经帮我们把T的资料换算好了(感动~~),
 而我们从TimeP指出去的各成员也都传回正确的数据,连日期都没问题^^
 
#include <iostream.h> #include <windows.h>
 #include <stdlib.h>
 
 
 class Clock
 {
 int Hour;
 int Minute;
 int Second;
 public:
 Clock(int NewHour,int NewMinute,int NewSecond)
 {
 Hour=NewHour;
 Minute=NewMinute;
 Second=NewSecond;
 }
 bool setTime()
 {
 while(1)
 {
 cout < <"Do you want to set time?y/n:";
 char c=0;
 cin>>c;
 if(c=='y')
 {
 int hour,minute,second;
 cout < <"please set time:";
 cin>>hour>>minute>>second;
 Hour=hour;
 Minute=minute;
 Second=second;
 return 1;
 }
 else if(c=='n') return 0;
 else cout < <"Sorry,I can't understand it!" < <endl;
 }
 }
 
 
 
 
 void ShowTime()
 {
 cout < <Hour < <":" < <Minute < <":" < <Second < <endl;
 }
 void operator++(int)
 {
 Second++;
 if(Second>=60)
 {
 Second-=60;
 Minute++;
 if(Minute>=60)
 {
 Minute-=60;
 Hour++;
 Hour%=24;
 }
 }
 }
 };
 void main()
 {
 SYSTEMTIME s;
 GetLocalTime(&s);
 Clock clock(s.wHour,s.wMinute,s.wSecond);
 while(1)
 {
 
 Sleep(965);
 system("cls");
 clock++;
 clock.ShowTime();
 
 }
 }
 
 
#include  <stdio.h>    #include  <time.h>    int main()    {    time_t t;    time(&t);    printf("Today's date and time: %s",ctime(&t));    return 0;    }        @函数名称: difftime    函数原型: double difftime(time_t time2, time_t time1)    函数功能: 得到两次机器时间差,单位为秒    函数返回: 时间差,单位为秒    参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得    所属文件:  <time.h>      #include  <time.h>    #include  <stdio.h>    #include  <dos.h>    #include  <conio.h>    int main()    {    time_t first, second;    clrscr();    first=time(NULL);    delay(2000);    second=time(NULL);    printf("The difference is: %f seconds",difftime(second,first));    getch();    return 0;    }        @函数名称: gmtime    函数原型: struct tm *gmtime(time_t *time)    函数功能: 得到以结构tm表示的时间信息    函数返回: 以结构tm表示的时间信息指针    参数说明: time-用函数time()得到的时间信息    所属文件:  <time.h>      #include  <stdio.h>    #include  <stdlib.h>    #include  <time.h>    #include  <dos.h>    char *tzstr="TZ=PST8PDT";    int main()    {    time_t t;    struct tm *gmt, *area;    putenv(tzstr);    tzset();    t=time(NULL);    area=localtime(&t);    printf("Local time is:%s", asctime(area));    gmt=gmtime(&t);    printf("GMT is:%s", asctime(gmt));    return 0;    }        @函数名称: time    函数原型: time_t time(time_t *timer)    函数功能: 得到机器的日历时间或者设置日历时间    函数返回: 机器日历时间    参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型    所属文件:  <time.h>      #include  <time.h>    #include  <stdio.h>    #include  <dos.h>    int main()    {    time_t t;    t=time();    printf("The number of seconds since January 1,1970 is %ld",t);    return 0;    }        @函数名称: tzset    函数原型: void tzset(void)    函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途    函数返回:    参数说明:    所属文件:  <time.h>      #include  <time.h>    #include  <stdlib.h>    #include  <stdio.h>    int main()    {    time_t td;    putenv("TZ=PST8PDT");    tzset();    time(&td);    printf("Current time=%s",asctime(localtime(&td)));    return 0;   
 
 
	    
    
 |