time()函数:

NAME
time - get time in seconds

SYNOPSIS
#include <time.h>

time_t time(time_t *tloc);

/* typedef long     time_t; */

DESCRIPTION
time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.

返回从公元1970-01-01的UTC时间 从00:00:00 到现在所经历的描述,如果tloc非空,返回值也会存储到tloc指向的内存

RETURN VALUE
On success, the value of time in seconds since the Epoch is returned. On error, ((time_t) -1) is returned, and errno is set appropriately.

执行成功,返回秒数

执行失败,返回-1

localtime()函数

作用:

localtime是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间

原型:

struct tm *localtime(const time_t *timer)

  • const time_t *time:这是指向表示日历时间的 time_t 值的指针,由time()函数返回

返回值:

struct tm定义如下:

struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月份,范围从 0 到 11 */
int tm_year; /* 自 1900 起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};

例子:

#include <stdio.h>
#include <time.h> int main ()
{
time_t rawtime;
struct tm *info;
char buffer[]; time( &rawtime ); info = localtime( &rawtime );
printf("当前的本地时间和日期:%s", asctime(info)); return();
}

asctime()函数

原型:

char* asctime (const struct tm * timeptr)

作用:

把timeptr指向的tm结构体中储存的时间转换为字符串,返回的字符串格式为:Www Mmm dd hh:mm:ss yyyy。其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。

参数:

  • const struct tm * timept:由localtime()函数返回

返回值:

时间字符串

例子:

/* asctime example */
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, time, localtime, asctime */ int main ()
{
time_t rawtime;
struct tm * timeinfo; time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) ); return ;
}
函数范例的输出:
The current date/time is: Wed Feb 13 15:46:46 2018

gettimeofday()函数

原型:

#include <sys/time.h>
int gettimeofday(struct timeval*tv, struct timezone *tz);

作用:

获得当前精确时间(1970年1月1日到现在的时间),或者为执行计时,可以使用gettimeofday()函数。

参数:

  • struct timeval*tv

    struct timeval{
    long int tv_sec; // 秒数
    long int tv_usec; // 微秒数
    }
  • struct timezone *tz
    struct timezone{
    int tz_minuteswest;  /*格林威治时间往西方的时差*/
    int tz_dsttime;/*DST 时间的修正方式*/
    }

返回值:

执行成功,返回0

执行失败,返回-1

例子:

它获得的时间精确到微秒(1e-6 s)量级。在一段代码前后分别使用gettimeofday可以计算代码执行时间:

struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
foo();
gettimeofday(&tv_end, NULL);

Linux常用时间函数的更多相关文章

  1. linux中时间函数

    linux下常用时间类型有四种: time_t . struct   tm. struct  timeval .    struct   timespec 1.time_t   时间函数 time_t ...

  2. Linux常用系统函数

    Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...

  3. PLSQL常用时间函数

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. Lua常用时间函数

    常用时间函数 print(os.time()) --当前系统时间值 print(os.date( print(os.date("*t"), os.time()) --当前系统时间表 ...

  5. mysql时间类型总结及常用时间函数

    日期时间和类型 常用日期和时间类型 字节 year                1       表示年份                   值范围:(1901----2155) date     ...

  6. mysql常用时间函数与类型转换

    一.用到的函数有: 1.时间格式化函数  DATE_FORMAT(date,format) 2.时间加减函数DATE_ADD(date,INTERVAL expr unit)DATE_SUB(date ...

  7. MySQL常用时间函数

    官方文档:Date and Time Functions Name Description ADDDATE() Add time values (intervals) to a date value ...

  8. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  9. Linux时间时区详解与常用时间函数

    时间与时区 整个地球分为二十四时区,每个时区都有自己的本地时间. Ø  UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计 ...

随机推荐

  1. Laya中地图拼接的缝隙问题

    拼图的空隙. Egret也有拼图的空隙.比如制作飞机游戏,背景拼接轮换着下移,有明显的缝隙.用TextureMerger可以解决. 看了下Laya.可以设置repeat. 编辑模式,图片上右键,设置默 ...

  2. springboot 整合rabbitMQ

    RabbitMQ RabbitMQ是一个被广泛使用的开源消息队列.它是轻量级且易于部署的,它能支持多种消息协议.RabbitMQ可以部署在分布式和联合配置中,以满足高规模.高可用性的需求. 在pom. ...

  3. 实现RTSP摄像机硬盘录像机NVR网站网页微信H5直播方案EasyNVR部署问题之:ERR_CONTENT_LENGTH_MISMATCH

    背景分析 接触到EasyNVR产品的开发者都知道,EasyNVR是一套功能齐全.简洁易用的流媒体解决方案,可作为能力曾前端接入摄像头,后端接入业务系统使用,也可以作为应用层,直接修改为属于企业用户自己 ...

  4. (转)自动微分(Automatic Differentiation)简介——tensorflow核心原理

    现代深度学习系统中(比如MXNet, TensorFlow等)都用到了一种技术——自动微分.在此之前,机器学习社区中很少发挥这个利器,一般都是用Backpropagation进行梯度求解,然后进行SG ...

  5. DataTable.NET的event執行順序

    var dataTable = $('#table') .on( 'processing.dt', function ( e, settings, processing ) { $('#loading ...

  6. javaScript的高级函数

    1.map() map()方法返回一个新数组,新数组是原始数组调用函数之后处理后的值. map()方法按照原始数组元素顺序依次处理元素. map不会对空数组进行检测. map不会改变原始数组 . 参数 ...

  7. Qt信号-槽原理剖析--(1)信号槽简介

    唯有创造才是快乐.只有创造的生灵才是生灵.--罗曼·罗兰 信号槽是观察者模式的一种实现,特性如下: A.一个信号就是一个能够被观察的事件,或者至少是事件已经发生的一种通知: B.一个槽就是一个观察者, ...

  8. LeetCode 856. 括号的分数(Score of Parentheses)

    856. 括号的分数 856. Score of Parentheses 题目描述 给定一个平衡括号字符串 S,按下述规则计算该字符串的分数: () 得 1 分. AB 得 A + B 分,其中 A ...

  9. C++的派生类构造函数是否要带上基类构造函数

    //public:Student(int s_age):People(s_age) //C++的派生类构造函数后面是否带上基类构造函数,取决于基类构造函数是否需要传入参数,如果要参数,就一定带上:不需 ...

  10. 08-Maps

    Maps map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值.Maps类似于python中的字典 Maps定义 maps的key值必须是可hash(就是不可 ...