time.h

来源:互联网 发布:介绍软件的ppt 编辑:程序博客网 时间:2024/05/18 00:33

1、时间获取

                                                 ------------转为本地时间(localtime)

获取日历时间(time)------|

                ------------转为UTC时间(gmtime)



#include <stdio.h>#include <time.h>int main(void){struct tm *local;time_t it;//获取日历时间it = time(NULL);//转为本地时间local = localtime(&it);printf("the Local hour is:%d\n",local->tm_hour);//转为UTC时间local = gmtime(&it);printf("the UTC hour is:%d\n",local->tm_hour);return 0;}


运行结果




2、时间显示

时间获取后只是保存在一个结构体里面,并不能打印出来

为了方便将结构体里的数据转为一组字符串

                                                


=======UTC时间(gmtime)--------------转为字符串(asctime)

日历时间(time)=======|

||

||

||

 转为字符串(ctime)


#include <stdio.h>#include <time.h>int main(void){struct tm *local;time_t it;//获取日历时间it = time(NULL);//将日历时间转为UTC时间local = gmtime(&it);//将UTC时间转为字符串printf(asctime(local));//直接将日历时间转为字符串printf(ctime(&it));return 0;}

运行结果


0 0