2.时间函数(gettimeofday)

本文详细介绍了gettimeofday函数,它是UNIX系统中用于获取当前时间的API,返回自1970年1月1日以来的秒数和微秒数。通过C语言示例展示了如何使用这个函数获取并打印时间信息。
摘要由CSDN通过智能技术生成

时间函数(gettimeofday)

时间函数gettimeofday

gettimeofday 函数是一个用于获取当前时间的UNIX系统调用,它返回自1970年1月1日以来的秒数和微秒数。它的原型如下:

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

参数

tv:一个指向 struct timeval 结构的指针,用于存储当前时间。
tz:一个指向 struct timezone 结构的指针,用于存储时区信息。在现代UNIX系统中,通常将该参数设置为 NULL,因为不再使用时区信息。

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

gettimeofday 函数返回当前时间的秒数和微秒数,分别存储在 tv_sec 和 tv_usec 字段中。

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

int main()
{
	struct timeval tv;
	gettimeofday(&tv, NULL);

	printf("sec = %ld\tusec = %ld\n",tv.tv_sec, tv.tv_usec);

	return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值