关于时间

来源:互联网 发布:php 判断是微信浏览器 编辑:程序博客网 时间:2024/05/22 13:38

<span style="background-color: rgb(255, 255, 255); font-family: Arial; font-size: 14px; line-height: 26px;">转载:http://blog.csdn.net/shandianling/article/details/7707925</span>
<span style="background-color: rgb(255, 255, 255); font-family: Arial; font-size: 14px; line-height: 26px;">1)求 时间差(精确到微秒) </span><span style="background-color: rgb(255, 255, 255); font-family: Arial; font-size: 14px; line-height: 26px;">int gettimeofday (struct timeval *__restrict __tv,__timezone_ptr_t __tz);</span>

<pre name="code" class="cpp">#include <stdio.h>#include <sys/time.h>intmain(void){  int i=0;  struct timeval tv,tv2;  gettimeofday(&tv, NULL);  for (i = 0; i < 100000; i++)    {      printf("%d\t",i+1);      if((i+1)%20 == 0) puts("\n");    }  gettimeofday(&tv2, NULL);  printf("\nsecond : %f",  (tv2.tv_usec - tv.tv_usec)/1000000.0 + (tv2.tv_sec-tv.tv_sec) );  return 0;}

2)求当前时间(精确到秒) time(0) 及时间的转换。
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"></span><pre name="code" class="cpp">#include <time.h>#include <stdio.h>#include <string.h>intmain(){  time_t now = time(0);  const char * cnow = ctime(&now);  struct tm *ptmNow = localtime(&now);  char ctmnow[128] =    { 0 };  printf("秒数:%d\n",now);  printf("原始时间:%s",cnow);  sprintf(ctmnow, "%4d-%02d-%02d %02d:%02d:%02d", 1900 + ptmNow->tm_year,     1+ptmNow->tm_mon,ptmNow->tm_mday,ptmNow->tm_hour,ptmNow->tm_min,ptmNow->tm_sec);  printf("手动转换为日期: %s\n",ctmnow);  strftime(ctmnow,sizeof(ctmnow),"自动转换的日期: %Y-%m-%d %H:%M:%S",ptmNow); //格式化时间//strftime(ctmnow,sizeof(ctmnow),"自动转换的日期: %F %T",ptmNow); //或格式化时间  printf("%s\n",ctmnow);  return 0;}
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"></span>
3)时间函数相互关系
<span style="font-family: Arial; font-size: 14px; line-height: 26px;"><img src="file:///c:/documents and settings/administrator/application data/360se6/User Data/temp/1341373996_2710.jpg" alt="" /><img src="http://img.blog.csdn.net/20150813164231066?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span>
0 0
原创粉丝点击