文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>时间相关编程

时间相关编程

时间:2010-09-19  来源:谷底望月

timeval

timeval结构可以用来保存时间信息。

在文件<sys/time.h>中定义,结构如下:

struct timeval {
    time_t tv_sec; /* seconds */
    suseconds_t tv_usec; /* microseconds */
};

其中tv_usec为微秒(10-6秒) 。

 

其主要用法如下:

struct timeval e;
gettimeofday(&e, NULL);//获得当前时间
settimeofday(&e, NULL);//设置当前时间

time_t

time_t保存时间信息,其实际为一个长整型。是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。

常用方法

1.基本用法

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main(void)
{
  time_t t;
    t = time(NULL);
  printf("The number of seconds since January 1, 1970 is %ld",t);
  return 0;
}

2.time函数也常用于随机数的生成,用日历时间作为种子。

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

int main(void) 
{ 
  int i; 
  srand((unsigned) time(NULL)); 
  printf("ten random numbers from 0 to 99\n\n"); 
  for(i=0;i<10;i++) 
  { 
    printf("%d\n",rand()%100); 
  } 
  return 0; 
} 

3.用time()函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间。

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

int main(void) 
{ 
  time_t timer;//time_t就是long int 类型 
  struct tm *tblock; 
  timer = time(NULL);//这一句也可以改成time(&timer); 
  tblock = localtime(&timer); 
  printf("Local time is: %s\n",asctime(tblock)); 
  return 0; 
}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载