zoukankan      html  css  js  c++  java
  • 第三季-第10课-时间编程

    第10课-时间编程

     

    10.1 时间类型

    Codinated Universal Time(UTC):世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich Mean Time,GMT)。

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

    10.2 函数学习

    这里面的函数学习方法是和之前的学习方法一致的,即三步学习法:函数名;函数原型;函数使用。

    1. 获取日历时间

    (1) 函数名

    time

    (2) 函数原形

    time_t time(time_t *t);

    (3) 函数功能

    返回日历时间

    (4) 所属头文件

    #include<time.h>

    (5) 返回值

    成功:日历时间

    失败:-1

    (6) 参数说明

    t:不为空的情况下保存返回值。

    (7) 范例程序

    #include<time.h>

    void main()

    {                

             time_t ctime;

             ctime = time(NULL);

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

    }

    2. 获取格林威治时间

    (1) 函数名

    gmtime

    (2) 函数原形

    struct tm *gmtime(const time_t *timep);

    (3) 函数功能

    将参数timep所指定的日历时间抓华为标准时间。

    (4) 所属头文件

    #include<time.h>

    (5) 返回值

    strict tm

    {

    int tm_sec;             //seconds

    int tm_min;            //minutes

    int tm_hour;           //hours

    int tm_mday;                        //day of the month

    int tm_mon;            //month

    int tm_year;            //year

    int tm_wday;           //day of the week

    int tm_yday;           //day in the year

    int tm_isdst;           //daylight saving time

    };

    成功:世界标准时间,以struct tm形式存储的

    失败:NULL

    (6) 参数说明

    timep:带转化的世界时间

    (7) 范例程序

    #include<time.h>

    void main()

    {                

             time_t ctime;

             struct tm *tm;

             ctime = time(NULL);

             //将日历时间转化为标准时间

             tm = gmtime(&ctime);

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

            //注意对于结构体内的成员的标识方法。

    }

    3. 获取本地时间

    (1) 函数名

    localtime()

    (2) 函数原形

    struct tm *localtime(const time_t *timep);

    (3) 函数功能

    将参数timep所指向的日历时间转化为本地时间。

    (4) 所属头文件

    #include<time.h>

    (5) 返回值

    strict tm

    {

    int tm_sec;             //seconds

    int tm_min;            //minutes

    int tm_hour;           //hours

    int tm_mday;                       //day of the month

    int tm_mon;            //month

    int tm_year;            //year

    int tm_wday;           //day of the week

    int tm_yday;           //day in the year

    int tm_isdst;           //daylight saving time

    };

    成功:返回以结构体tm存储的本地时间

    失败:NULL

    (6) 参数说明

    timep:待转化的日历时间

    (7) 范例程序

    #include<time.h>

    void main()

    {                

             time_t ctime;

             struct tm *tm;

             ctime = time(NULL);

             //将日历时间转化为本地时间

             tm = localtime(&ctime);

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

    }

    注:为了验证我们的程序是正确的,我们可以通过date命令来查看时间,会发现和我们上面程序打印出来的时间是一样的。

    4. 字符串(格式化)方式显示时间

    (1) 函数名

    asctime

    (2) 函数原形

    char *asctime(const struct tm *tm);

    (3) 函数功能

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

    (4) 所属头文件

    #include<time.h>

    (5) 返回值

    字符串方式显示的时间

    (6) 参数说明

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

    (7) 范例程序

    #include<time.h>

    void main()

    {                

             time_t ctime;

             struct tm *tm;

             char *stime;

             ctime = time(NULL);

             //将日历时间转化为本地时间

             tm = gmtime(&ctime);

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

             stime = asctime(tm);

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

    }

    5. 获取高精度时间

    (1) 函数名

    gettimeofday();

    (2) 函数原形

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

    (3) 函数功能

    获取高精度的时间。

    (4) 所属头文件

    #include<sys/time.h>

    (4) 返回值

    成功:0;

    失败:-1

    (6) 参数说明

    struct timeval

    {

    time_t       tv_sec;     //seconds

    Suseconds_t  tv_usec;    //microseconds

    }

    strutct timezone

    {

    int  tz_minuteswest;      //minutes west of Greewich

    int  tz_dsttime;          //type of DST correction

    }

    tv:保存从1970年1月1日0:0:0到现在经历的秒数和微秒数;

    tzone:通常为NULL

    (7) 范例程序

    下面的程序用来检测func()函数运行的时间,这里我们用了头文件#include<stdio.h>,因为NULL符号的使用需要这个头文件。

    #include<sys/time.h>

    #include<stdio.h>

    void func()

    {

             int i,j;

             int ret;

             for(i=0;i<1000;i++)

                      for(j=0;j<1000;j++)

                               ret = j;

    }

    void main()

    {

             int i,j;

             struct timeval tv1;

             struct timeval tv2;

             gettimeofday(&tv1,NULL);

             func();

             gettimeofday(&tv1,NULL);

        i = tv2.tv_sec-tv1.tv_sec;

             j = tv2.tv_usec-tv1.tv_usec;

             printf("sec is %d,usec is%d ",i,j);

    }

    10.3 总结

     

  • 相关阅读:
    理解dajngo ORM查询中select_related的作用
    Django 模型层 Meta 选项详解
    token和session的区别
    Python 爬虫 urllib、urllib2、urllib3用法及区别
    linux里面访问一个链接的方法
    scrapy-redis实现全站分布式数据爬取
    linux shell 操作 mysql命令(不进入mysql操作界面)
    后台+下载(wget)+多个下载url
    维基下载页面说明(指南)
    pytorch --Rnn语言模型(LSTM,BiLSTM) -- 《Recurrent neural network based language model》
  • 原文地址:https://www.cnblogs.com/free-1122/p/11345373.html
Copyright © 2011-2022 走看看