time函数

  • time

#include<time.h>

time_t time(time_t *t);

typdef long int time_t;

time() returns the time as the number of secs since 1970-01-01 00:00:00 +0000(UTC)

假如参数t不是NULL,返回值(秒数)(日历时间)也存储在t中。

失败返回-1.

  • ctime

#include <time.h>

char *ctime(const time_t *time);

将秒数转化为具体日期(ASCII),长度固定26字节。

the time is 1449579811
the time ascii is Tue Dec  8 21:03:31 2015

等价:asctime(localtime(t));

  • broken-down time

Broken-down time is stored in the structure tm whichi is deiined in time.h as follows:

struct tm {
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};

tm_year指的是自1900年以来的年数。

  • asctime
char *asctime(const struct tm *tm);

将分离时间装换为字符串时间。

  • localtime
struct tm *localtime(const time_t *timep);

将秒数装换为分离时间。

    • gmtime
struct tm *gmtime(const time_t *timep);

  • mktime
time_t mktime(struct tm *tm);

将分离时间转换为秒数。

    • difftime
double difftime(time_t time1, time_t time0);

The difftime() function returns the number of seconds elapsed between time time1 and time time0, represented as a double. Each of the times is specified in calendar time, which means its value is a measurement (in seconds) relative to the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

  • clock

clock_t clock(void);

DESCRIPTION

   The  clock()  function returns an approximation of processor time used by the program.

RETURN VALUE

The value returned is the CPU time used so far as a  clock_t;  to  get the  number of seconds used, divide by CLOCKS_PER_SEC.  
If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t) -1.
  • gettimeofday

#include <sys/time.h>


struct timeval{

    long tv_sec;

    long tv_usec;

};

int gettimeofday(struct timeval *tv, struct timezone *tz);

gettimeofday将时间保存在tv中,tz一般使用NULL代替,不建议设置tz。返回日历时间即绝对时间(非本地时间)。


通过不同时间段运行函数可测量时间。

  • clock_gettime/clock_settime
#include <time.h>
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);

The functions clock_gettime() and clock_settime() retrieve and set the time of the specified clock clk_id.

The clk_id argument is the identifier of the particular clock on which to act. A clock may be system-wide and hence visible for all processes, or per-process if it measures time only within a single process.m
CLOCK_REALTIME: real time,受adjtime和NTP影响和系统管理员调时的影响。
CLOCK_REALTIME_COARSE:快但少精度的CLOCK_REALTIME。
CLOCK_MONOTONIC:单调时钟,不受管理员调时影响,但受NTP和adjtime等调时影响。
CLOCK_MONOTONIC_RAW:和CLOCK_MONOTONIC相似,但基于硬件时间,不受NTP和aditime等任何调时影响。准确的启动时间应倚赖于此。
CLOCK_PROCESS_CPUTIME_ID:Pre-process CPU-time clock,测量进程内所有线程花费时间。
CLOCK_THREAD_CPUTIME_ID:Thread-specific CPU-time clock。

  • setitimer&getitimer

#include <sys/time.h>


int getitimer(int which, struct itimerval *value);

int setitimer(int which, struct itimerval *newval, struct itimerval *oldval);

struct itimerval {

    struct timeval it_interval;

    struct timeval it_value;

};

itimerval 结构中的 it_value 是减少的时间,当这个值为 0 的时候就发出相应的信号了. 然后设置为 it_interval 值。

当it_interval为0时,timer为单触发时钟,singal-shot timer。

Linux 操作系统为每一个进程提供了 3 个内部间隔计时器.

ITIMER_REAL:减少实际时间.到时的时候发出 SIGALRM 信号.

ITIMER_VIRTUAL:减少有效时间(进程执行的时间).产生 SIGVTALRM 信号.

ITIMER_PROF:减少进程的有效时间和系统时间(为进程调度用的时间).这个经常和上面一 个使用用来计算系统内核时间和用户时间.产生 SIGPROF 信号.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <signal.h> struct itimerval it; void sighandler(int sig)
{
printf("catch sig:[%d]...\n", sig); it.it_value.tv_sec = it.it_interval.tv_sec;
it.it_value.tv_usec = it.it_interval.tv_usec;
} int main(int argc, char *argv[])
{
struct sigaction sa;
memset(&sa, , sizeof(struct sigaction));
sa.sa_handler=sighandler;
sa.sa_flags=;
sigaction(SIGALRM, &sa, NULL); //signal(SIGALRM, sighandler);
//signal(SIGVTALRM, sighandler); it.it_interval.tv_sec = ;
it.it_interval.tv_usec = ;
it.it_value.tv_sec = it.it_interval.tv_sec;
it.it_value.tv_usec = it.it_interval.tv_usec;
setitimer(ITIMER_REAL, &it, NULL);
//setitimer(ITIMER_VIRTUAL, &it, NULL); while();
return ;
}

参考:

linux应用time和timezone

time函数的更多相关文章

  1. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  2. 探究javascript对象和数组的异同,及函数变量缓存技巧

    javascript中最经典也最受非议的一句话就是:javascript中一切皆是对象.这篇重点要提到的,就是任何jser都不陌生的Object和Array. 有段时间曾经很诧异,到底两种数据类型用来 ...

  3. JavaScript权威指南 - 函数

    函数本身就是一段JavaScript代码,定义一次但可能被调用任意次.如果函数挂载在一个对象上,作为对象的一个属性,通常这种函数被称作对象的方法.用于初始化一个新创建的对象的函数被称作构造函数. 相对 ...

  4. C++对C的函数拓展

    一,内联函数 1.内联函数的概念 C++中的const常量可以用来代替宏常数的定义,例如:用const int a = 10来替换# define a 10.那么C++中是否有什么解决方案来替代宏代码 ...

  5. 菜鸟Python学习笔记第一天:关于一些函数库的使用

    2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ...

  6. javascript中的this与函数讲解

    前言 javascript中没有块级作用域(es6以前),javascript中作用域分为函数作用域和全局作用域.并且,大家可以认为全局作用域其实就是Window函数的函数作用域,我们编写的js代码, ...

  7. 复杂的 Hash 函数组合有意义吗?

    很久以前看到一篇文章,讲某个大网站储存用户口令时,会经过十分复杂的处理.怎么个复杂记不得了,大概就是先 Hash,结果加上一些特殊字符再 Hash,结果再加上些字符.再倒序.再怎么怎么的.再 Hash ...

  8. JS核心系列:浅谈函数的作用域

    一.作用域(scope) 所谓作用域就是:变量在声明它们的函数体以及这个函数体嵌套的任意函数体内都是有定义的. function scope(){ var foo = "global&quo ...

  9. C++中的时间函数

    C++获取时间函数众多,何时该用什么函数,拿到的是什么时间?该怎么用?很多人都会混淆. 本文是本人经历了几款游戏客户端和服务器开发后,对游戏中时间获取的一点总结. 最早学习游戏客户端时,为了获取最精确 ...

  10. Python高手之路【四】python函数装饰器

    def outer(func): def inner(): print('hello') print('hello') print('hello') r = func() print('end') p ...

随机推荐

  1. 让Netty入门变得简单

    让Netty入门变得简单 https://mp.weixin.qq.com/s/MBnbLmCmFJo0QK9WNwXrXQ 如果先启动nettyClient就不会有nettyServer输出了: p ...

  2. WinForm 窗口缩放动画效果

    using System; using System.Collections.Generic; using System.Text; using System.Threading; using Sys ...

  3. Linux shell 提取文件名和目录名

    ${}用于字符串的读取,提取和替换功能,可以使用${} 提取字符串 1.提取文件名 [root@localhost log]# var=/dir1/dir2/file.txt [root@localh ...

  4. wifidog 移植到MIPS平台

    使用的是一款Broadcom的芯片,现在上面运行wifidog实现认证上网的功能.由于不是openwrt平台,所以就没了make menuconfig 勾选就能自动编译到版本中的.所以想使用交叉编译的 ...

  5. xmpp 服务器配置 open fire for windows 及 spark 测试

    xmpp 服务器配置 open fire for windows 此文章为 XMPP windows服务器配置,使用的是 open fire 3.9.1.exe 1: 下载 open fire ope ...

  6. vue常用属性解释。

    props:详看 示例-网格组件. props 可以是数组或对象,用于接收来自父组件的数据.props 可以是简单的数组,或者使用对象作为替代,对象允许配置高级选项,如类型检测.自定义校验和设置默认值 ...

  7. 反射方式,获取出集合ArrayList类的class文件对象

    /* * 定义集合类,泛型String * 要求向集合中添加Integer类型 * * 反射方式,获取出集合ArrayList类的class文件对象 * 通过class文件对象,调用add方法 * * ...

  8. 【转】MVC4验证用户登录特性实现方法

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  9. Entity Framework Code First关系映射约定【l转发】

    本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...

  10. Java泛型中extends和super的理解

    作者:zhang siege链接:https://www.zhihu.com/question/20400700/answer/91106397来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...