首页 > 文章列表 > time.ctime()在python中的使用

time.ctime()在python中的使用

Python time.ctime()
430 2022-08-07

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

1、说明

该函数把时间秒数转为字符串。

2、语法

time.ctime([ sec ])

3、参数

sec -- 要转换为字符串时间的秒数。

4、返回值

该函数没有任何返回值。

5、实例

//连续每隔1秒加印系统的当前时间
//使用sleep来进行不精确的延时
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t timetemp;     //定义一个时间结构体变量
while(1)
{
time(&timetemp);    //获取时间参数
printf("%s", ctime(&timetemp)); //打印当前时间
sleep(1);
}
 
return 0;
}

在对于时间准确度的把握上,为了使操作的更加细化,很多人习惯把时间精确到秒。但在实际程序操作中,虽然秒数方便我们的查阅,但是计算机并不能直接的识别,所以还需要借助time.ctime()函数转换成字符串的形式。