C++基础--第十七章时间

文章详细介绍了C语言中与时间相关的数据类型,如clock_t、time_t、size_t和tm结构,以及常用的时间函数,包括time()、ctime()、clock()、localtime()、gmtime()、mktime()、difftime()和strftime()。这些函数用于获取系统时间、转换时间格式、计算时间差等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件:#include<ctime>

有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。其中类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数

结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:

struct tm {

    int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
    
    int tm_min;   // 分,范围从 0 到 59

    int tm_hour;  // 小时,范围从 0 到 23

    int tm_mday;  // 一月中的第几天,范围从 1 到 31

    int tm_mon;   // 月,范围从 0 到 11

    int tm_year;  // 自 1900 年起的年数

    int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起

    int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起

    int tm_isdst; // 夏令时

};

主要函数

time_t time(time_t *time);

time_t是一个整数类型,通常是一个长整型。它表示 1970 年 1 月 1 日以来经过的秒数

作用:获取当前的系统时间(以秒为单位)并将其存储在 arg 所指向的对象中,同时返回当前时间

例:

#include <stdio.h>

#include <time.h>



int main ()

{

time_t seconds;



seconds = time(NULL);

printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);



return(0);

}

char *ctime(const time_t *time);

作用:将 time 所指向的时间值(以秒为单位)转换为一个表示日期和时间的字符串,并返回该字符串。

返回的字符串格式如下: Www Mmm dd hh:mm:ss yyyy 其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份。

注意:该ctime函数是根据参数time_t类型来转换出对应的时间的,如果给了一个空的time_t类型,则返回Thu Jan 01 08:00:00 1970

例:

int main ()

{

time_t curtime;

curtime=time(NULL);


printf("当前时间 = %s", ctime(&curtime));


return(0);

}

clock_t clock(void);

作用:返回程序运行时间的近似值,以时钟周期为单位。为了获取 CPU 所使用的秒数,需要除以 CLOCKS_PER_SEC

例:

#include <time.h>

#include <stdio.h>



int main()

{

clock_t start_t, end_t;

double total_t;

int i;



start_t = clock();

printf("程序启动,start_t = %ld\n", start_t);

   

printf("开始一个大循环,start_t = %ld\n", start_t);

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

{

}

end_t = clock();

printf("大循环结束,end_t = %ld\n", end_t);

  

total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;

printf("CPU 占用的总时间:%f\n", total_t  );

printf("程序退出...\n");



return(0);

}

tm *localtime(const time_t *time);

作用:将 time 所指向的时间值(以秒为单位)转换为表示本地时间的 tm 结构的指针。

注意:该localtime函数是根据参数time_t类型来转换成tm结构的

例:

#include <stdio.h>

#include <time.h>



int main ()

{

time_t rawtime;

struct tm *info;

char buffer[80];



rawtime=time(NULL);



info = localtime( &rawtime );

printf("当前的本地时间和日期:%s", asctime(info));



return(0);

}

tm *gmtime(const time_t *time);

作用:将 timer 所指向的时间值转换为一个 tm 结构,表示协调世界时 (UTC) 的时间

例:

#include <stdio.h>

#include <time.h>



#define BST (+1)

#define CCT (+8)



int main ()

{



time_t rawtime;

struct tm *info;



rawtime=time(NULL);

/* 获取 GMT 时间 */

info = gmtime(&rawtime);

  

printf("当前的世界时钟:\n");

printf("伦敦:%2d:%02d\n", (info->tm_hour+BST)%24, info->tm_min);

printf("中国:%2d:%02d\n", (info->tm_hour+CCT)%24, info->tm_min);



return(0);

}

gmtime和localtime的区别和联系:

联系:都用于将 time_t 类型的时间值转换为 tm 结构

区别:

        gmtime 函数将时间值转换为协调世界时 (UTC) 的时间,不考虑时区差异。返回的 tm 结构中的时间值是相对于 UTC 的时间。

        localtime 函数将时间值转换为本地时区的时间。返回的 tm 结构中的时间值是相对于本地时区的时间。

例:

#include <iostream>

#include <ctime>



int main() {

    time_t now = time(nullptr);

   

    tm* gmTime = gmtime(&now);

    tm* localTime = localtime(&now);

   

    std::cout << "UTC 时间为: "

              << gmTime->tm_hour << ":" << gmTime->tm_min << ":" << gmTime->tm_sec

              << std::endl;

   

    std::cout << "本地时间为: "

              << localTime->tm_hour << ":" << localTime->tm_min << ":" << localTime->tm_sec

              << std::endl;

   

    return 0;

}

char * asctime ( const struct tm * time );

作用:将 time 所指向的 tm 结构表示的时间转换为一个表示日期和时间的字符串,并返回该字符串的指针。

例:

#include <iostream>

#include <ctime>



int main() {

    time_t now = time(nullptr);

   

    tm* localTime = localtime(&now);

   

    char* timeString = asctime(localTime);

   

    std::cout << "当前本地时间为: " << timeString;

   

    return 0;

}

time_t mktime(struct tm *time);

作用:把tm结构转换为time_t类型。发生错误时返回-1。

例:

#include <stdio.h>

#include <time.h>



int main ()

{

time_t ret;

struct tm info;

char buffer[80];



info.tm_year = 2023 - 1900;

info.tm_mon = 5 - 1;

info.tm_mday = 13;

info.tm_hour = 0;

info.tm_min = 0;

info.tm_sec = 1;

info.tm_isdst = -1;



ret = mktime(&info);

if( ret == -1 ) {

printf("Error: unable to make time using mktime\n");

}

else {

printf("当前时间 = %s", ctime(&ret));

}



return(0);

}

double difftime ( time_t time2, time_t time1 );

作用:该函数返回 time1 和 time2 之间相差的秒数。

例:

#include <stdio.h>

#include <time.h>

#ifdef _WIN32

#include <Windows.h>

#else

#include <unistd.h>

#endif



int main ()

{

time_t start_t, end_t;

double diff_t;



printf("程序启动...\n");

time(&start_t);//新的使用方式



printf("休眠 5 秒...\n");

sleep(5);



time(&end_t);



diff_t = difftime(end_t, start_t);

printf("执行时间 = %f\n", diff_t);

printf("程序退出...\n");



return(0);

}

size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr);

参数:

        str:指向一个字符数组的指针,用于存储格式化后的时间字符串。

        count:指定输出缓冲区的大小,以防止缓冲区溢出。

        format:一个格式化字符串,用于指定输出的时间格式。

        timeptr:指向一个 tm 结构的指针,表示要格式化的时间。

作用: 将 tm 结构表示的时间按照指定的格式化字符串进行格式化,并将结果存储在 str 指向的字符数组中。

格式

例:

#include <stdio.h>

#include <time.h>



int main ()

{

time_t rawtime;

struct tm *info;

char buffer[80];



time( &rawtime );



info = localtime( &rawtime );



strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);

printf("格式化的日期 & 时间 : |%s|\n", buffer );



return(0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值