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 ...
随机推荐
- Python类库下载
https://sourceforge.net/projects/pywin32/files/pywin32/ WMI库的安装 下载 http://timgolden.me.uk/python/wmi ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- 与Python Falling In Love_Python跨台阶(环境搭建)
Python--环境搭建 首先需要下载python安装包,官网下载地址:https://www.python.org/downloads/ 下载完直接点击安装... 安装完后就可以配置环境变量咯^_^ ...
- 常见面试题之ListView的复用及如何优化
经常有人问我,作为刚毕业的要去面试,关于安卓开发的问题,技术面试官会经常问哪些问题呢?我想来想去不能一股脑的全写出来,我准备把这些问题单独拿出来写,并详细的分析一下,这样对于初学者是最有帮助的.这次的 ...
- ejs
这个博客比较专业些http://sunnyhl.iteye.com/blog/1985539 ejs速度不是最快的,推荐最多大概是因为其简单的语法结构.主要通过<% %><%=%&g ...
- js中基本操作
1.操作标签值 <!DOCTYPE html> <html> <meta charset="utf-8"> <meta http-equi ...
- 编写高质量代码改善C#程序的157个建议[动态数组、循环遍历、对象集合初始化]
前言 软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...
- GDI+中发生一般性错误的解决办法(转帖)
今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常.于是开始“ ...
- SDAutolayout图片大小根据数量变化
只需要在自定义的PhotoContainerView中做一下判断就可以了 ) { [self setupAutoWidthFlowItems:[temp copy] withPerRowItemsCo ...
- HashMap的一些用法
1.HashMap的遍历 //这个是通过 迭代器iterator 来实现 HashMap的遍历 Iterator iterator=hashMap.keySet().iterator(); while ...