Linux中表示“时间”的结构体和相关函数
分类: C/C++
- /* Returned by `time'. */
- typedef __time_t time_t;
- /* A time value that is accurate to the nearest
- microsecond but also has a range of years. */
- struct timeval
- {
- __time_t tv_sec; /* Seconds. */
- __suseconds_t tv_usec; /* Microseconds. */
- };
- struct timespec
- {
- __time_t tv_sec; /* Seconds. */
- long int tv_nsec; /* Nanoseconds. */
- };
- struct tm
- {
- int tm_sec; /* Seconds. [0-60] (1 leap second) */
- int tm_min; /* Minutes. [0-59] */
- int tm_hour; /* Hours. [0-23] */
- int tm_mday; /* Day. [1-31] */
- int tm_mon; /* Month. [0-11] */
- int tm_year; /* Year - 1900. */
- int tm_wday; /* Day of week. [0-6] */
- int tm_yday; /* Days in year.[0-365] */
- int tm_isdst; /* DST. [-1/0/1]*/
- #ifdef __USE_BSD
- long int tm_gmtoff; /* Seconds east of UTC. */
- __const char *tm_zone; /* Timezone abbreviation. */
- #else
- long int __tm_gmtoff; /* Seconds east of UTC. */
- __const char *__tm_zone; /* Timezone abbreviation. */
- #endif
- };
- #include <time.h>
- #include <stdio.h>
- int main()
- {
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- printf("%.24s %ld Nanoseconds\n", ctime(&ts.tv_sec), ts.tv_nsec);
- return 0;
- }
- #include <time.h>
- char *asctime(const struct tm *tm);
- char *asctime_r(const struct tm *tm, char *buf);
- char *ctime(const time_t *timep);
- char *ctime_r(const time_t *timep, char *buf);
- sizt_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
- struct timeval tv;
- struct timespec ts;
- time_t sec_tv = tv.tv_sec;
- time_t sec_ts = ts.ts_sec;
- struct tm *gmtime(const time_t *timep);
- struct tm *gmtime_r(const time_t *timep, struct tm *result);
- 或者:
- struct tm *localtime(const time_t *timep);
- struct tm *localtime_r(const time_t *timep, struct tm *result);
- #include <time.h>
- #include <sys/time.h>
- #include <stdio.h>
- int main()
- {
- struct timeval tv;
- char strTime[32];
- gettimeofday(&tv, NULL);
- struct tm *ptm = gmtime(&tv.tv_sec); //将秒转换成struct tm的形式
- strftime(strTime, 32, "%F %T", ptm);
- printf("%s ", strTime); //输出精确到秒
- printf("%ld Micorseconds\n", (long)tv.tv_usec); //输出微秒
- return 0;
- }
Linux中表示“时间”的结构体和相关函数的更多相关文章
- Linux中进程控制块PCB-------task_struct结构体结构
Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...
- linux块设备驱动---相关结构体(转)
上回最后面介绍了相关数据结构,下面再详细介绍 块设备对象结构 block_device 内核用结构block_device实例代表一个块设备对象,如:整个硬盘或特定分区.如果该结构代表一个分区,则其成 ...
- Linux C 程序 预处理,结构体(13)
C语言预处理,结构体 C语言预处理命令1.宏定义 1.无参数宏 #define 标识符 字符串 #代表本行是编译预处理命名 习惯上,宏定义大写 代替一个字符串,介绍重复书写某个字符串的工作量 有意义的 ...
- 浅析 Linux 中的时间编程和实现原理一—— Linux 应用层的时间编程【转】
本文转载自:http://www.cnblogs.com/qingchen1984/p/7007631.html 本篇文章主要介绍了"浅析 Linux 中的时间编程和实现原理一—— Linu ...
- Objective-C中经常使用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect
Objective-C中经常使用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect 1 NSRange NSRange 的原型为 typedef struct _N ...
- FFMPEG中最要害的结构体之间的关系
FFMPEG中最关键的结构体之间的关系 http://www.myexception.cn/program/1404591.html FFMPEG中结构体很多.最关键的结构体可以分成以下几类: a) ...
- Linux进程管理之task_struct结构体
进程是处于执行期的程序以及它所管理的资源(如打开的文件.挂起的信号.进程状态.地址空间等等)的总称.注意,程序并不是进程,实际上两个或多个进程不仅有可能执行同一程序,而且还有可能共享地址空间等资源. ...
- Linux串口通信之termios结构体说明
termios结构体中,该结构体一般包括如下的成员:tcflag_t c_iflag; tcflag_t c_oflag; tcflag_t c_cflag; tcfla ...
- Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)【转】
Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息.它定义在include/linux/sched.h文件中. 谈到task_str ...
随机推荐
- [VIM] 格式化代码
快速使用vim格式化代码 在vim的编辑模式i下直接ESC退出道命令模式之后直接敲入如下命令: gg=G 将全部代码格式化 nG=mG 将第n行到第m行的代码格式化 注:如果ESC ...
- myeclipse 2014新建maven web 项目步骤
首先在myeclipse中配置maven的安装路径: 在pom.xml中加上: 1 2 3 4 5 6 7 8 9 10 <plugins> <plugin&g ...
- 深入理解计算机系统家庭作业汇总 20135301&&20135328
深入理解计算机系统家庭作业 深入理解计算机系统第二章家庭作业 题目2.64 题目要求 判断二进制数偶数位是否有任意一位位为1,有的话返回1,否则返回0 解题过程 int any_even_one(un ...
- Ubuntu下eclipse安装svn插件
好记性不如烂笔头,碰见一个不大不小的问题,记录下. 系统:Ubuntu 12.04 Eclipse:eclipse-jee-kepler-R-linux-gtk.tar.gz subclipse:1. ...
- [软件测试]Linux环境中简单清爽的Google Test (GTest)测试环境搭建(初级使用)
本文将介绍单元测试工具google test(GTEST)在linux操作系统中测试环境的搭建方法.本文属于google test使用的基础教程.在linux中使用google test之前,需要对如 ...
- 云计算之路-阿里云上:2014年6月12日14:40出现CPU 100%
这是今天遇到的第2次故障,主站1台云服务器在14:40:33-14:41:09期间出现CPU突然跑满的状况,而CPU跑满之前,请求量并没有明显增长. 向阿里云提交之后,说晚上会对这台云服务器的虚拟机进 ...
- 云计算之路-阿里云上:2014年6月11日17点遇到的CPU 100%状况
今天下午17:00-17:05之间,在请求量没有明显变化的情况下,SLB中的1台云服务器的CPU突然串到100%(当时SLB中一共有3台云服务器),见下图: 造成的直接后果是请求执行时间变得超长,最长 ...
- 前端开发之Chrome插件
Postman - REST Client Postman是Ajax开发的神器,对于Restful开发方式特别有帮助,可以用来模拟各种请求来测试API的正确性,比如用来模拟Ajax请求.它还支持认证, ...
- KMP和扩展KMP
文章网上太多这里提一下代码细节: KMP: scanf("%s\n",s); scanf("%s\n",t); int ls=strlen(s),lt=strl ...
- [AaronYang]C#人爱学不学[2]
1. 记事本写C#,脱离vs 新建记事本,名字为 helloworld.cs using System; namespace Hello{ public class HelloWorldSay{ st ...