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 ...
随机推荐
- Chrome 插件: 起动本地应用 (Native messaging)
Chrome 插件: 起动本地应用 (Native messaging) www.MyException.Cn 网友分享于:2014-08-01 浏览:3次 Chrome 插件: 启动本地应用 ...
- WebSocket使用教程 2
WebSocket使用教程 - 带完整实例 收藏 james_laughing 发表于 2年前 阅读 46438 收藏 23 点赞 5 评论 4 摘要: WebSocket使用教程 - 带完整实例 什 ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- 系统级I/O 第八周11.9~11.15
第十章 系统级I/O cp1 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include ...
- TPLINK GPL code 简要分析
从TPLINK官网下载了GPL code,下载后文件名是wr841nv9_en_gpl.tar.gz, 但是无论是linux还是windows下解压都提示压缩包有问题,不过还是可以解压出完整的目录的. ...
- DLL中调用约定和名称修饰(一)
DLL中调用约定和名称修饰(一) 调用约定(Calling Convention)是指在程序设计语言中为了实现函数调用而建立的一种协议.这种协议规定了该语言的函数中的参数传送方式.参数是否可变和由谁来 ...
- Android Support Annotations :安卓注解快速上手
我们都知道,安卓资源文件都是int类型的ID来保存其引用,通过注解类型,可以让我们在写代码的时候,及时发现参数类型的错误,避免潜在的BUG,如下: 我们通过@LayoutRes指定了参数必须要是R.l ...
- 迅雷首席架构师刘智聪:微信小程序的架构与系统设计的几点观感
笔者注:本文来自于迅雷首席工程师刘智聪的个人分享,他毕业于南昌大学化学系,加入迅雷后设计开发了多款迅雷核心产品,凭借“大规模网络流媒体服务关键支撑技术”项目获得2015年国家科学技术进步奖二等奖,同时 ...
- [CH#58解题报告]
题目:http://206.contesthunter.org/contest/CH%20Round%20%2358%20-%20OrzCC%E6%9D%AFnoip%E6%A8%A1%E6%8B%9 ...
- overlay-scrollbar-js
function doAccept() { var div = $('#shadowbox .legal-gate-cont') var con = $('#shadowbox .html') var ...