如何将 ctime 更改为正常的字符串表示? [英] How to change ctime to normal string representation?

查看:56
本文介绍了如何将 ctime 更改为正常的字符串表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 File::stat 我们可以得到给定文件的 ctime.我的问题是如何将 ctime(即自纪元以来以秒为单位的 inode 更改时间)更改为正常的时间表示,例如2009-08-26 17:28:28".是否有任何内置或模块可以解决此任务?

Using File::stat we can get the ctime of a given file. My question is how to change the ctime, which means the inode change time in seconds since the epoch, to a normal time representation like "2009-08-26 17:28:28". Is there any build-in or module can solve this task?

推荐答案

最标准的方式是使用 POSIX 模块及其 strftime 函数.

The most standard way is to use POSIX module and it's strftime function.

use POSIX qw( strftime );
use File::stat;

my $stat_epoch = stat( 'some_file.name' )->ctime;
print strftime('%Y-%m-%d %H:%M:%S', localtime( $stat_epoch ) );

所有这些标记,如 %Y、%m 等,都是在标准中定义的,并且在 C、系统日期"命令(至少在 Unix 上)等中工作相同.

All these markers like %Y, %m and so on, are defined in standard, and work the same in C, system "date" command (at least on Unix) and so on.

这篇关于如何将 ctime 更改为正常的字符串表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆