time.h 详细介绍
来源:互联网 发布:万得数据 编辑:程序博客网 时间:2024/05/29 23:44
time.h 详细介绍
< ctime> (time.h)包含获得和使用日期和时间信息的函数的定义。
一、Macro constants(宏常量)
- CLOCKS_PER_SEC:滴答声/秒,时间的单位
- NULL:空指针
二、types(类型)
clock_t:时钟类型,表示时钟滴答数的基本数据类型
size_t:无符号整型
time_t:时间类型,表示时间
struct tm:时间结构,包含日历、时间
三、时间操作
1、clock_t clock (void);
描述:从特定时间开始消耗的时间,如果失败,返回-1。
/* clock example: frequency of primes */#include <stdio.h> /* printf */#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */#include <math.h> /* sqrt */int frequency_of_primes (int n) { int i,j; int freq=n-1; for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;} return freq;}int main (){ clock_t t; int f; t = clock(); printf ("Calculating...\n"); f = frequency_of_primes (99999); printf ("The number of primes lower than 100,000 is: %d\n",f); t = clock() - t; printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC); return 0;}
2、double difftime (time_t end, time_t beginning);
描述:计算从beginning到end的时间(end-beginning)(单位/秒)。
/* difftime example */#include <stdio.h> /* printf */#include <time.h> /* time_t, struct tm, difftime, time, mktime */int main (){ time_t now; struct tm newyear; double seconds; time(&now); /* get current time; same as: now = time(NULL) */ newyear = *localtime(&now); newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0; newyear.tm_mon = 0; newyear.tm_mday = 1; seconds = difftime(now,mktime(&newyear)); printf ("%.f seconds since new year in the current timezone.\n", seconds); return 0;}
3、time_t mktime (struct tm * timeptr);
描述:
返回timeptr指针描述的时间,如果时间未描述,返回-1。
localtime的逆变换
忽略结构成员tm_wday and tm_yday;其他成员及时超出有效范围,也将解释。
/* mktime example: weekday calculator */#include <stdio.h> /* printf, scanf */#include <time.h> /* time_t, struct tm, time, mktime */int main (){ time_t rawtime; struct tm * timeinfo; int year, month ,day; const char * weekday[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; /* prompt user for date */ printf ("Enter year: "); fflush(stdout); scanf ("%d",&year); printf ("Enter month: "); fflush(stdout); scanf ("%d",&month); printf ("Enter day: "); fflush(stdout); scanf ("%d",&day); /* get current timeinfo and modify it to the user's choice */ time ( &rawtime ); timeinfo = localtime ( &rawtime ); timeinfo->tm_year = year - 1900; timeinfo->tm_mon = month - 1; timeinfo->tm_mday = day; /* call mktime: timeinfo->tm_wday will be set */ mktime ( timeinfo ); printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]); return 0;}
4、time_t time (time_t* timer);
描述:
获取当前时间。
如果时间指针不为NULL,将返回其指向的时间。
不能取得时间,返回-1.
返回的时间基于: 00:00 hours, Jan 1, 1970 UTC
/* time example */#include <stdio.h> /* printf */#include <time.h> /* time_t, struct tm, difftime, time, mktime */int main (){ time_t timer; struct tm y2k = {0}; double seconds; y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0; y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; time(&timer); /* get current time; same as: timer = time(NULL) */ seconds = difftime(timer,mktime(&y2k)); printf ("%.f seconds since January 1, 2000 in the current timezone", seconds); return 0;}
四、转换
1、char* asctime (const struct tm * timeptr);
描述:
返回timeptr指针的时间,用C-字符串描述返回类型为:Www Mmm dd hh:mm:ss yyyy(星期 月 日 时分秒 年)输出在新一行,以空字符串结束
/* asctime example */#include <stdio.h> /* printf */#include <time.h> /* time_t, struct tm, time, localtime, asctime */int main (){ time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "The current date/time is: %s", asctime (timeinfo) ); return 0;}
2、char* ctime (const time_t * timer);
描述:
返回timer指针的时间,用C-字符串描述返回类型为:Www Mmm dd hh:mm:ss yyyy(星期 月 日 时分秒 年)输出在新一行,以空字符串结束和asctime(localtime(timer))一样
/* ctime example */#include <stdio.h> /* printf */#include <time.h> /* time_t, time, ctime */int main (){ time_t rawtime; time (&rawtime); printf ("The current local time is: %s", ctime (&rawtime)); return 0;}
3、struct tm * gmtime (const time_t * timer);
描述:
利用timer填充tm结构体把time_t时间转化为UTC time
/* gmtime example */#include <stdio.h> /* puts, printf */#include <time.h> /* time_t, struct tm, time, gmtime */#define MST (-7)#define UTC (0)#define CCT (+8)int main (){ time_t rawtime; struct tm * ptm; time ( &rawtime ); ptm = gmtime ( &rawtime ); puts ("Current time around the World:"); printf ("Phoenix, AZ (U.S.) : %2d:%02d\n", (ptm->tm_hour+MST)%24, ptm->tm_min); printf ("Reykjavik (Iceland) : %2d:%02d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min); printf ("Beijing (China) : %2d:%02d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min); return 0;}
4、struct tm * localtime (const time_t * timer);
描述:
利用timer填充tm结构体把time_t时间转化为 local time
/* localtime example */#include <stdio.h> /* puts, printf */#include <time.h> /* time_t, struct tm, time, localtime */int main (){ time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime (&rawtime); printf ("Current local time and date: %s", asctime(timeinfo)); return 0;}
5、·size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );
描述:
把timeptr中的时间,以特定的格式,复制到ptr中以format格式,复制时间timeptr到ptr,最多maxsize字符。ptr:目的数组,存储C-字符串maxsize:包括末尾空字符,复制到ptr的最大字符数format:格式返回值:返回复制到ptr的字符数(不包括末尾空字符),若超出范围maxsize,返回0
/* strftime example */#include <stdio.h> /* puts */#include <time.h> /* time_t, struct tm, time, localtime, strftime */int main (){ time_t rawtime; struct tm * timeinfo; char buffer [80]; time (&rawtime); timeinfo = localtime (&rawtime); strftime (buffer,80,"Now it's %I:%M%p.",timeinfo); puts (buffer); return 0;}
- time.h 详细介绍
- time.h 详细操作指南
- time.h 详细操作指南 (转)
- C 中的 time 相关函数详细介绍
- time.h
- time.h
- time.h
- time.h
- time.h
- time.h
- time.h
- time.h
- time.h
- time.h
- “time.h”
- time.h
- time.h
- time.h
- 把一个c类的网络地址192-168-1-0划分为6个子网,请计算出...
- 欢迎使用CSDN-markdown编辑器
- Python time strftime()方法
- vuex下state数据的存储
- 二叉树求序
- time.h 详细介绍
- Lua 开篇
- vim与vi的区别,及常用操作,有查找关键词,向上向下翻页,快速跳到一行首末尾,设置取消行号,撤销编辑
- SVN显示提交时间,提交人等信息
- iFunk:用户全方位享乐体验的升级
- 学习C++要做笔记1(引用)
- 阿里云加密服务使用教程
- 微信开发笔记(1)——配置微信服务器、验证信息安全性
- Toolbar快速实现,让你一分钟上手