C语言中有对时间的操作函数吗?

折腾_苏州 2005-08-01 03:05:41
例如:
得到当前系统时间的函数,返回值是什么类型?
有时间类型吗?
如何进行时间的运算,如计算一段时间的大小,返回值是什么类型?
那位高手能解释清楚?
谢谢.
...全文
259 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
WingForce 2005-08-01
  • 打赏
  • 举报
回复
看到标题时候吃了一惊
以为C语言居然可以控制时间....
领先相对论若干年啊
qingyuan18 2005-08-01
  • 打赏
  • 举报
回复
主要的时间函数:mktime(tm *)返回相对于1970 1 1 的一个long型(time_t)
time(time_t *)构造当前时间,返回当前时间相对与1970 1 1的long型(time_t)
localtime(time_t *)根据time_t的long值返回tm结构体

有这几个函数足够操作时间了
折腾_苏州 2005-08-01
  • 打赏
  • 举报
回复
成功解决!!
谢谢!!
zaisheng09 2005-08-01
  • 打赏
  • 举报
回复
up~
PMsg 2005-08-01
  • 打赏
  • 举报
回复
Calculates the wall-clock time used by the calling process.

clock_t clock( void );
Return Value
The elapsed wall-clock time since the start of the process (elapsed time in seconds times CLOCKS_PER_SEC). If the amount of elapsed time is unavailable, the function returns –1, cast as a clock_t.

Remarks
The clock function tells how much time the calling process has used. A timer tick is approximately equal to 1/CLOCKS_PER_SEC second. In versions of Microsoft C before 6.0, the CLOCKS_PER_SEC constant was called CLK_TCK.

Requirements
Routine Required header Compatibility
clock <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example
// crt_clock.c
/* This example prompts for how long
* the program is to run and then continuously
* displays the elapsed time for that period.
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

int main( void )
{
long i = 6000000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Sample Output
Delay for three seconds
Done!
Time to do 6000000 empty loops is 0.1 seconds
jixingzhong 2005-08-01
  • 打赏
  • 举报
回复
计算时间差 :


/*calculate time*/
#include "time.h"
#include "stdio.h"
#include "conio.h"
main()
{
time_t start,end;
int i;
start=time(NULL);
for(i=0;i<30000;i++)
printf("\1\1\1\1\1\1\1\1\1\1\n");
end=time(NULL);
printf("\1: The different is %6.3f\n",difftime(end,start));
getch();
}
jixingzhong 2005-08-01
  • 打赏
  • 举报
回复
时间日期函数,
函数库为time.h、dos.h

在时间日期函数里,主要用到的结构有以下几个:
总时间日期贮存结构tm
┌──────────────────────┐
│struct tm │
│{ │
│ 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*/ │
│ int tm_yday; /*自1月1日起的天数,0-365*/ │
│ int tm_isdst; /*是否采用夏时制,采用为正数*/│
│} │
└──────────────────────┘
日期贮存结构date
┌───────────────┐
│struct date │
│{ │
│ int da_year; /*自1900的年数*/│
│ char da_day; /*天数*/ │
│ char da_mon; /*月数 1=Jan*/ │
│} │
└───────────────┘
时间贮存结构time
┌────────────────┐
│struct time │
│{ │
│ unsigned char ti_min; /*分钟*/│
│ unsigned char ti_hour; /*小时*/│
│ unsigned char ti_hund; │
│ unsigned char ti_sec; /*秒*/ │
│ │
└────────────────┘

char *ctime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
字符串:Mon Nov 21 11:31:54 1983\n\0

char *asctime(struct tm *tm)本函数把指定的tm结构类的时间转换成下列格式的字符串:
Mon Nov 21 11:31:54 1983\n\0

double difftime(time_t time2,time_t time1)计算结构time2和time1之间的时间差距(以秒为单位)

struct tm *gmtime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)转换成格林威治时间,并以tm结构形式返回

struct tm *localtime(long *clock)本函数把clock所指的时间(如函数time返回的时间)转换成当地标准时间,并以tm结构形式返回

void tzset()本函数提供了对UNIX操作系统的兼容性

long dostounix(struct date *dateptr,struct time *timeptr)本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回
自格林威治时间1970年1月1日凌晨起到现在的秒数

void unixtodos(long utime,struct date *dateptr,struct time *timeptr)本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成DOS格式并保存于用户所指的结构dateptr和timeptr中

void getdate(struct date *dateblk)本函数将计算机内的日期写入结构dateblk中以供用户使用

void setdate(struct date *dateblk)本函数将计算机内的日期改成由结构dateblk所指定的日期

void gettime(struct time *timep)本函数将计算机内的时间写入结构timep中,以供用户使用

void settime(struct time *timep)本函数将计算机内的时间改为由结构timep所指的时间

long time(long *tloc)本函数给出自格林威治时间1970年1月1日凌晨至现在所经过的秒数,并将该值存于tloc所指的单元中.

int stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)写入计算机中.
折腾_苏州 2005-08-01
  • 打赏
  • 举报
回复
在线等
1. C 语言中的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++中指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 13. 位域 58 14. C语言函数二维数组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函数大全 68 17. C语言宏定义技巧 89 18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函数与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函数使用大全 158 33. 二叉树的数据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI C中struct内存布局 173 36. 冒泡和选择排序实现 180 37. 函数指针数组与返回数组指针的函数 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与数组学习笔记 222 45. 数组不是指针 224 46. 标准C中字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函数的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高级C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机数的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员数据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧