在程序中,经常需要输出系统的当前时间、计算程序的执行时间、使用计时器等。 一、时间的类型 1.格林威治标准时间 coordinated universal time(UTC)是世界标准时间,即常说的格林威治标准时间(greenwich meantime,GMT). 2.日历时间 日历时间(calendar time)是用'一个标准时间点(如1970年1月1日0点)到此时经过的秒数'来表示的时间. 二、时间函数的API 时间函数的API均属于系统调用函数.。 1.获取日历时间 #include <time.h> time_t time(time_t *tloc) 函数功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数. 参数:通常设置为NULL (time_t在time.h中定义:typedef long int time_t) 例: #include <time.h> int main(int argc,char *argv[]) { int seconds=0; seconds = time(NULL); printf('seconds=%d\n',seconds); } 执行结果: [root@localhost Time]# ./time seconds=1294908511 通常用户得到日历时间的秒数没有实际的意义,但可以为时间转化做一些铺垫性质的工作.为了更好的利用时间,用户需要将这些秒数转化为更容易接受的时间表示方式,这些表示时间的方式有格林威治时间、本地时间等. 2.将日历时间转换为格林威治标准时间 struct tm *gmtime(const time_t *timep) 函数功能:将日历时间转化为格林威治标准时间,并保存在tm结构 参数:日历时间的返回值 3.将日历时间转化为本地时间 struct tm* localtime(const time_t *timep) |
|
来自: 昵称18037940 > 《待分类》