C\C++时间处理函数[获取系统时间,求时间差,格式化输出等]
2011-09-01 17:52
986 查看
///////////////////////////////////////////////////////// // // test_time.c // // Copyright (C) 2011 huangkangquan@scut2008 // // Description: this file include the time.h, practice // the time type and function. // 1.There types of time: // clock_t;time_t;struct tm; // // 2.Time process function: // clock_t clock(void); // double difftime(time_t time1,time_t time2); // time_t mktime(struct tm * timeptr); // time_t time(time_t *timer); // // 3.Time translation function: // char * asction(const struct tm * timeptr); // char * ctime(const time_t * timer); // struct tm * gmtime(const time_t * timer); // struct tm * localtime(const time_t * timer); // size_t strftime(char * restrict s,size_t maxsize, // const char * restrict format, // const struct tm * restrict timeptr); // // Sorry about my poor English,just for practicing. // ///////////////////////////////////////////////////////// #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h>/*for Sleep*/ time_t cur_time; time_t cur_time2; clock_t cur_clock; /*how much clicks since the program begin*/ struct tm * tm_time; void get_current_time()/*get current time by three ways*/ { cur_time = time(NULL); /* the first method*/ time(&cur_time2); /*the second method*/ cur_clock = clock(); /*the third method*/ } void display_what_can_get() { printf("cur_time = %g\n",(double)cur_time); printf("cur_time2 = %g\n",(double)cur_time2); printf("cur_clock = %g\n",(double)cur_clock); printf("CLOCKS_PER_SEC = %g\n",(double)CLOCKS_PER_SEC); Sleep(2000); time_t now_time = time(NULL); printf("Running time: %g sec.\n",difftime(now_time,cur_time)); clock_t now_clock = clock(); printf("Running time: %g sec.\n",(now_clock - cur_clock)/(double)CLOCKS_PER_SEC); printf("Running clocks: %g times.\n",(double)(now_clock - cur_clock)); printf("CLOCKS_PER_SEC: %g\n",(double)CLOCKS_PER_SEC); } void display_tm(struct tm * tm_time) { printf("tm_year:\t%d\n",tm_time->tm_year); printf("tm_mon :\t%d\n",tm_time->tm_mon); printf("tm_mday:\t%d\n",tm_time->tm_mday); printf("tm_hour:\t%d\n",tm_time->tm_hour); printf("tm_min :\t%d\n",tm_time->tm_min); printf("tm_sec :\t%d\n",tm_time->tm_sec); } void translate_type() { // from time_t to struct tm //initial_tm(tm_time); time_t my_time = time(NULL); printf("local time:\n"); tm_time = localtime(&my_time);/*get the local time*/ display_tm(tm_time); printf("GMT time\n"); tm_time = gmtime(&cur_time); display_tm(tm_time); // from struct tm to time_t time_t time = mktime(tm_time); // print the string printf("%s\n",ctime(&my_time)); printf("%s\n",asctime(localtime(&my_time))); printf("%s\n",asctime(gmtime(&my_time))); // strftime() just like the other formate I/O function,it's complicated,because has a lot of format term. } int main() { get_current_time(); display_what_can_get(); translate_type(); return 0; }
运行结果:
clock_t是指该程序开始启动后的时钟滴答,从上面的程序测试可看出 CLOCKS_PER_SEC 为 1000.
time_t 是一种日历时间,事实上clock_t与time_t是与实现有关的类型,可能是作为整数也可能时浮点数存储,C标准只是将其规定为“算术运算类型”,
我将它强制转为浮点输出来观察了。
struct tm 把时间分解为时分秒等,包括tm_sec,tm_min,tm_hour,tm_mday,tm_mon,tm_year,tm_wday,tm_yday,tm_isdst.
需要注意的一点是tm_mon是当前日历显示-1,比如当前是9月,此时tm_mon会是8.
localtime()产生的是本地时间,gmtime产生的是GMT(格林尼治)时间。
ctime(time_t * t) 等价于 asctime(localtime(time_t * t));
strftime()与其他格式化输入输出差不多,但转换符号是不同的。
获取时间差,可以用 difftime(time_t1,time_t2);见程序。
相关文章推荐
- C或C++调用Windows系统函数 实现延时 或 获取当前时间的处理
- C++获取系统时间和格林制(GMT)时间函数,并按一定格式输出
- [C++]win32输出当前系统时间函数,可用以程序计时
- JAVA中获取当前系统时间,日期并格式化输出
- c++/c获取系统时间,并格式化输出
- C/C++获取系统时间的函数汇集
- 时间处理 c++ 获取当前系统时间 1. 时间戳形式 2. char *形式
- JS获取系统当前时间并格式化输出小函数
- 时间处理 c++ 获取当前系统时间 1. 时间戳形式 2. char *形式[转]
- QT 获取当前系统时间并格式化输出
- java中获取当前系统时间,日期并格式化输出
- 获取系统当前时间,完成格式化,得到一个字符串。例如”201608130743”(指年月日时分)。再随机取3个小于10的整数,分别插入到 字符串的年、月、日后面,完成输出。
- java中获取当前系统时间,日期并格式化输出
- Delphi 获取系统时间后格式化输出
- C/C++下获取系统时间的函数
- C++中获取当前时间并格式化输出
- C++和VC中获取系统时间函数
- C++:win32输出当前系统时间函数
- java中获取当前系统时间,日期并格式化输出
- Java获取当前的系统时间并格式化输出