库time.h初探

来源:互联网 发布:如何保证数据的准确性 编辑:程序博客网 时间:2024/06/07 23:34

转载自http://blog.csdn.net/chenyiming_1990/article/details/8682552
time.h 属于C语言库

总结可以是两个类型:
time_t:表示距离 UTC 时间 1970-01-01 00:00:00 的秒数。也叫做日历时,类型是 long (typedef __int64 time_h)
clock_t: 只用于程序计时,貌似其他的没它什么事。类型是long, typedef long clock_t
struct tm:通常用于存储(人类习惯的年月日时分秒)本地时间。
UTC:cordinated universal time 世界统一时间,世界标准时间,日内瓦“1979.12.03”?,中国大陆时间与UTC时差为+8.UTC+8=本地时间。

struct tm  {      int tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */      int tm_min; /* Minutes: 0-59 */      int tm_hour;    /* Hours since midnight: 0-23 */      int tm_mday;    /* Day of the month: 1-31 */      int tm_mon; /* Months *since* january: 0-11 */      int tm_year;    /* Years since 1900 */      int tm_wday;    /* Days since Sunday (0-6) */      int tm_yday;    /* Days since Jan. 1: 0-365 */      int tm_isdst;   /* +1 Daylight Savings Time, 0 No DST,* -1 don't know */  };  

常用函数:
clock: 获取程序开始执行后占用的处理器时间,返回值clock_t。
time:获取当前系统时间(UTC时间)的time_t值。
ctime:将time_t值转换为表示本地时间的字符串。
gmttime:将time_t值转换为表示GMT时间的字符串。
localtime:将time_t转换为表示本地时间的strunct tm结构。
mktime:将表示本地时间的struct tm转换为time_t。
asctime:将struct tm转换为字符串形式。
difftime:得到两个日历时之间的差值。
strftime:自定义把结构体tm的日期与时间信息转换为制定的格式。

#include <iostream>using namespace std;int main(){    time_t timer;  //typedef __int64 time_t 类型等同于long    timer = time(NULL);    cout << timer << endl;    cout << timer / 60 / 60 / 24 / 365 << endl;    cout << "-------------------------" << endl;    time_t timerr = 1502708561 + 24 * 3600 * 365;    cout << timerr << endl;    cout << timerr / 60 / 60 / 24 / 365 << endl;    system("pause");}
150270882247-------------------------153424456148

转换时间格式成日常习惯模式:

#include <time.h>#include <iostream>using namespace std;int main(){    time_t timer;  //typedef __int64 time_t 类型等同于long    timer = time(NULL);    cout << timer << endl;    struct tm *sp_time;    sp_time = localtime(&timer);    cout << "local time is :" << asctime(sp_time);    system("pause");}

输出为:

150270917647local time is :Mon Aug 14 19:12:56 2017

自定义结构体struct tm

#include <time.h>#include <string>#include <stdio.h>using namespace std;int main(){    struct tm st;    char str[80];    st.tm_sec = 39;    st.tm_min = 42;    st.tm_hour = 20;    st.tm_mon = 11;    st.tm_mday = 28;    st.tm_year =117; // 相对于1900,过了多少年    st.tm_wday = 6;  //相对一周,过了几天    st.tm_yday = 0;  //相对一年头,过了多少天,    st.tm_isdst = 0; //一般为0    strcpy(str, asctime(&st));    printf("local time is :%s" ,str);    system("pause");}

输出为:

local time is :Sat Dec 28 20:42:39 2017

获得日历时间

#include <time.h>#include <string>#include <stdio.h>using namespace std;int main(){    time_t t;    //    t = time(NULL);    //  time(&t); //两句等效    printf("Today date and time is : %s", ctime(&t));    system("pause");    return 0;}

结果为:

Today date and time is : Mon Aug 14 19:32:09 2017

计算时间差

#include <time.h>#include <stdlib.h>#include <stdio.h>#include <windows.h>using namespace std;int main(){    time_t first, second;    system("cls"); //调用系统命令,清楚屏幕上文字    time(&first);    Sleep(2000);    second = time(NULL);    printf("The difference time is : %f", difftime(second,first));    system("pause");    return 0;}

结果为:

Today date and time is : Mon Aug 14 19:32:09 2017

计算系统运行时间

#include <time.h>#include <stdlib.h>#include <stdio.h>#include <windows.h>using namespace std;int main(){    Sleep(2895);    printf("%lf\n", (double)clock()/CLOCKS_PER_SEC);//clock_t clock()时间单位是ms    //define CLOCKS_PER_SEC ((clock_t)1000)    system("pause");    return 0;}

结果为:

3.200000

挂钟时间,为长数,时间时间
clock()就像ros使用“use_sim_time”

原创粉丝点击