Linux文件编程,时间编程——时间编程

15 篇文章 0 订阅

1、时间类型

Coordinated Universal Time (UTC):世界标准时间Greenwich Mean TimeGMT格林威治标准时间。

Calendar Time:日历时间,是用“从一个标准时间点(如:1970110)”到此时经过的秒数来表示时间。


2、时间获取

#include <time.h>


time_t time(time_t *tloc)

功能:获取日历时间,即从1970110点到当前时间,所经历的秒数。

/*typedef  long  time_t*/


3、时间转化

struct tm *gmtime(const time_t *timep)

功能:将日历时间转化为UTC,并且保存至TM结构。

struct tm *localtime(const time_t *timep)

功能:将日历时间转化为本地时间,并且保存至TM结构。


tm结构:

struct  tm

{

    inttm_sec; //秒值

    inttm_min; //分钟值

    inttm_hour; //小时值

    inttm_mday; //本月第几日

    inttm_mon; //本年第几月

    inttm_year; //tm_year +1900 =哪一年

    inttm_wday; //本周第几日

    inttm_yday; //本年第几日

    inttm_isdst; //日光节约时间

};

例子:

/*
 * ================================================================
 *
 *       Filename:  time1.c
 *
 *    Description:  获取时间
 *
 *        Version:  1.0
 *        Created:  2013年10月30日 17时51分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ydonghao (), ydonghao2@gmail.com
 *   Organization:  Personal
 *
 * ================================================================
 */
#include    <time.h>
#include    <stdio.h>

int main(void)
{
    struct tm *local;
    time_t t;
    t = time(NULL);
    local = localtime(&t);
    printf("Local hour is:%d\n",local->tm_hour);
    local = gmtime(&t);
    printf("UTC hour is: %d\n", local->tm_hour);
    return 0;
}

char*asctime(const struct tmtm)

功能:将tm格式的时间转化为字符串,如:

SatJul 30 08 :43:03 2005


char*ctime(const time_t *timep)

功能:将日历时间转化为本地时间的字符串形式。


例子:

/*
 * =================================================================
 *
 *       Filename:  time2.c
 *
 *    Description:  时间显示
 *
 *        Version:  1.0
 *        Created:  2013年10月30日 18时05分54秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ydonghao (), ydonghao2@gmail.com
 *   Organization:  Personal
 *
 * =====================================================================
 */

#include    <time.h>
#include    <stdio.h>

int main(void)
{
    struct tm*ptr;
    time_t lt;
    lt = time(NULL);
    ptr = gmtime(<);
    printf(asctime(ptr));
    printf(ctime(<));
    return 0;
}

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

功能:获取从今日凌晨到现在的时间差,常用于计算事件耗时;


struct  timeval

{

    inttv_sec; //秒数

    inttv_usec; //微秒数

}


例子:

time3.c

/*
 * =================================================================
 *
 *       Filename:  time3.c
 *
 *    Description:  对函数执行时间计数
 *
 *        Version:  1.0
 *        Created:  2013年10月30日 18时15分33秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ydonghao (), ydonghao2@gmail.com
 *   Organization:  Personal
 *
  ==================================================================
 */

#include    <sys/time.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <math.h>

void function(void)
{
    unsigned int i,j;
    double y;
    for( i = 0; i < 1000 ; i++ )
    {
	for( j = 0; j < 1000 ; j++ )
	{
	    y ++ ;
	}
	
    }
    
}

int main(void)
{
    struct timeval tpStart, tpEnd;
    float timeuse;
    gettimeofday(&tpStart, NULL);
    function();
    gettimeofday(&tpEnd, NULL);

    timeuse = 1000000 * (tpEnd.tv_sec - tpStart.tv_sec) +
	tpEnd.tv_usec - tpStart.tv_usec;
    timeuse /= 1000000;

    printf("Used Time: %f\n", timeuse);

    return 0;
}


unsigned    int     sleep(unsigned int seconds);

功能:使程序睡眠seconds


void    usleep(unsigned long usec)

功能:使程序睡眠usec微秒。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值