Linux高级编程——时间编程
宗旨:技术的学习是有限的,分享的精神是无限的。
1 时间类型
(1) 世界标准世界(格林威治时间)
(2) 日历时间(1970年1月1日0时)——到此时经历的秒数
2 时间获取
#include<time.h>
time_ttime(time_t *tloc)——获取日历时间
/*typedef longtime_t*/
3 时间转化
struct tm*gmtime(const time_t *timep)
——将日历时间转化为格林威治标准时间,并保存至TM结构中
struct tm*localtime(const time_t *timep)
——将日历时间转化为本地时间,并保存至TM结构中
4 时间保存
struct tm
{int tm_sec;//秒值int tm_min;//分钟值int tm_hour;//小时值int tm_mday;//本月第几日int tm_mon;//本年第几月int tm_year;//tm_year+1990=哪一年int tm_wday;//本周第几日int tm_yday;//本年第几日int tm_isdst;//日光节约时间
};
5 时间显示
char *asctime(conststruct tm* tm)
——将tm格式的时间转化为字符串
char*ctime(const time_t *timep)
——将日历时间转化为字符串
*Linux下显示系统时间实例:
#include <time.h>
#include <stdio.h>int main(void)
{struct tm *ptr;time_t lt;/*获取日历时间*/lt=time(NULL);/*转化为格林威治时间*/ptr=gmtime(lt);/*以格林威治时间的字符串方式打印*/printf(asctime(ptr));/*以本地时间的字符串方式打印*/printf(ctime(lt));return 0;
}