2-10 时间编程

(本节笔记的相关实验代码在这里

1.  基本概念——时间类型

    UTC:Coordinated Universal Time(世界标准时间)

    GMT:Greenwich Mean Time(格林威治标准时间)

    Calendar Time:日历时间:从1970年1月1日0时0分0秒到现在所经历的秒数总和。


2.  函数学习

    2.1  获取日历时间

        函数名:

                time

        函数原型 (man 2 time)

                time_t time (time_t *t);

        函数功能:

                返回日历时间

        所属头文件:

                <time.h>

        返回值:

                成功:返回日历时间    失败:返回-1

        参数说明:

                t:如果t不为NULL,则把返回值保存到*t所指向的地址中。

        范例代码:

#include <stdio.h>

#include <stdio.h>

int main(int argc, char ** argv)

{

    time_t ctime;

    ctime = time(NULL);

    ctime = time(NULL);

    printf("ctime is %d\n",ctime);

    return 0;

}


    2.2  获取格林威治标准时间

        函数名:

                gmtime

        函数原型:

                struct tm *gmtime(const time_t *timep);

        函数功能:

                把timep所指向的日历时间转化为UTC,也就是世界标准时间

        所属头文件:

                <time.h>

        返回值:

                成功返回一个结构体,以struct tm形式存储,该结构体的形式为:

                        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;

                        };

                失败:返回NULL

        参数说明:

                t:指向待转化日历时间的指针

        范例代码:

#include <stdio.h>

#include <time.h>

int main(int argc, char ** argv)

{

    time_t ctime;

    struct tm *tm;

    ctime = time(NULL);

    tm = gmtime(& ctime);

    printf("now is hour %d, min is %d\n", tm->tm_hour, tm->tm_min);

    return 0;

}


    2.3  获取本地时间

        函数名:

                localtime

        函数原型:

                struct tm *localtime(const time_t *timep);

        函数功能:

                把timep指向的日历时间转化为本地时间

        所属头文件:

                <time.h>

        返回值:

                成功:返回以struct tm格式的本地时间    失败:返回NULL

        参数说明:

                timep:指向要转化的日历时间的指针

        范例代码:

                参考gmtime.c的范例代码


    2.4  格式化显示时间

        函数名:

                asctime

        函数原型:

                char *asctime(const struct tm *tm);

        函数功能:

                把tm格式的时间转化为字符串

        所属头文件:

                <time.h>

        返回值:

                成功:返回显示时间的字符串    失败:返回NULL

        参数说明:

                tm:要转化的tm格式的时间

        范例代码:

..........

char *stime;

stime = asctime(localtime);

printf("time is %s\n",stime);


    2.5  获取高精度时间

        函数名:

                gettimeofday

        函数原型:

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

       函数功能:

                获取高精度时间

        所属头文件:

                <sys/time.h>

        返回值:

                成功:返回0    失败返回:-1

        参数说明:

                tv:struct timeval{

                        time_t tv_sec                        /*  second  */

                        suseconds_t tv_usec        /*  microseconds  */

                        };                                            /*  从1970年1月1日0时0分0秒到现在的秒数 + 微秒  */

                tz::struct timezone{

                        int tz_minuteswest;

                        int tz_dsttime;

                        };

        范例代码:(touch gettime.c)

#include <stdio.h>

#include <sys/time.h>

int main(int argc, char ** argv)

{

    struct timeval tv;

    gettimeofday(&tv, NULL);

    printf("sec is %d, usec is %d\n", tv.tv_sec, tv.tv_usec);

    return 0;

}        


/*  该函数一般的用法是,通过获取两次不同的时间相减,可获取某个函数或者某个功能的精确运行时间  */

..........

gettimeofday(&tv1, NULL);

fun();

gettimeofday(&tv2, NULL);

s_sub = tv2.sec - tv1.sec;

us_sub = tv2.usec - tv1.usec;


    2.6  获取格式时间

        函数名:

                ctime

        函数原型:

                char *ctime(const time_t *timep);

        函数功能:

                获取本地时间后,把本地时间转换成asctime格式,也就是效果相等于:asctime(localtime(timeval))

        所属头文件:

                <time.h>

        返回值:

                成功:返回asc形式的字符串    失败:返回NULL

        参数说明:

                timep:存放日历格式的地址

        范例代码一:

#include <stdio.h>

#include <time.h>

int main(int argc, char ** argv)

{

    time_t the_time;

    char *buf = NULL;

    the_time = time(NULL);

    buf = ctime(&the_time);

    printf("%s\n", buf;

    return 0;

}

        范例代码二:

#include <stdio.h>

#include <time.h>

int main(int argc, char ** argv)

{

    char buf[256];

    struct tm *gm_time;

    strftime(buf, 256,"%c", gm_time);

    printf("%s\n", buf);

    return 0;

}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值