C语言获取Linux系统精确时间
2024-10-19 03:35:42
gettimeofday()函数的使用方法
1.函数原型
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
2.说明
gettimeofday()会把目前的时间用tv 结构体返回,当地时区的信息则放到tz所指的结构中
3.结构体
struct timeval{
long tv_sec;/*秒*/
long tv_usec;/*微妙*/
};
struct timezone{
int tz_minuteswest; /*和greenwich 时间差了多少分钟*/
int tz_dsttime; /*DST的校正*/
}
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#define SIZE_OF_DATETIME 20
int sysUsecTime(char *pTime)
{
struct timeval tv;
struct timezone tz;
struct tm *p;
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
sprintf(pTime,"%04d%02d%02d%02d%02d%02d%06ld",1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
return 0;
}
int main ()
{
char strusecTime[SIZE_OF_DATETIME+1];
sysUsecTime(strusecTime);
printf("%s\n",strusecTime);
}
最新文章
- Android笔记——permission权限大全
- fuelphp 问题1
- 什么是智能扫描开单打印进销存POS?安卓工业手持PDA设备上的POS销售开单,现场打印打票,用扫描枪太方便了
- 证书与keytool
- 工作中Linux常用命令
- HDU 3537 (博弈 翻硬币) Daizhenyang&#39;s Coin
- GreenDao官方文档翻译(下)
- Ubuntu下安装配置zsh和oh my zsh
- Ping pong
- [改善Java代码]使用forName动态加载类文件
- 【概率】poj 2096:Collecting Bugs
- 简单改造 starling 中的 AssetManager 让其更适合 批次加载纹理
- linux修改时区,时间格式
- iOS开源项目集合一
- nginx利用limit模块设置IP并发防CC攻击
- 推荐几个我一直在使用chrome小工具(上)
- static方法与非static方法是否可以互相调用
- POJ 3348 Cows [凸包 面积]
- java 中 printf()语句的理解
- [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II