更改 UNIX 时间以按日和小时模式排列数据
我有一个包含 9 列的输入文件,其中第 6 列和第 7 列分别表示开始和结束 UNIX 时间。 (时间线的总体跨度是1290895200到1291154399,3天)我正在寻找一个perl脚本,它可以接受unix时间来指定一天中的小时,将开始日期计算为第1天并增加相应的时间。是否可以通过检查每行上的时间戳并生成具有以下格式的输出文件来获得(列中的其余值保持不变,仅将 unix 时间戳转换为小时和天):
.Hour Day Col3 Col4 Col5 Col6 ..... 0 1 ....... 1 1 ....... upto 23 3 ....
I have an input file with 9 columns where 6th and 7th columns are for start and end unix time. (The overall span of the timeline is 1290895200 to 1291154399, 3 days) I am looking for a perl script which can take in the unix time to specify the hour of the day, counting the start date as Day 1 and increasing accordingle. Could this be obtained by checking the timestamps on each row and generate output file with the format below (with rest of the values in columns remaining unchanged only unix time stamp converted to Hour and Day):
.
Hour Day Col3 Col4 Col5 Col6 ..... 0 1 ....... 1 1 ....... upto 23 3 ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设列中没有空格,Perl '
-a
' (perldoc perlrun
)开关将会很有用。当与-n
或-p
组合时,它会自动拆分数组@F
中的输入。给定开始时间戳(Unix 时间),很容易将第 6 列或第 7 列(或两者)替换为与开始时间戳相关的小时/天。问题是“您想要在行的开头使用哪个”?
使用此
awk
程序生成一些数据:上面的 Perl 脚本生成了以下输出。第 6 列包含给定时间戳范围内的时间,每 3200 秒递增一次,并随机扰动最多 60 秒。第 7 列包含第 6 列中的值和结束时间之间范围内的随机时间。输出以两个小时/天值作为前缀,一个用于第 6 列,一个用于第 7 列。根据您的喜好修改格式。
Assuming no spaces within columns, the Perl '
-a
' (perldoc perlrun
) switch will be useful. It splits the input in the array@F
automatically when combined with-n
or-p
.Given the start timestamp (a Unix time), it is easy enough to replace either column 6 or 7 (or both) with an hour/day relateve to the start timestamp. The issue is 'which did you want at the beginning of the line'?
Using this
awk
program to generate some data:The Perl script above generated the output that follows. Column 6 contains a time in your given timestamp range, incrementing every 3200 seconds and randomly perturbed by up to 60 more seconds. Column 7 contains a random time in the range between the value in column 6 and the end time. The output is prefixed with two hour/day values, one for column 6, one for column 7. Tinker with the formatting to your heart's content.
localtime
函数将 unix 时间戳细分为数据列表,其中包含当年的小时和天数等。您可以使用它来非常轻松地构建您的格式。
The
localtime
function gives you a breakdown of a unix timestamp into a list of data containing, among others, the hour and the number of the day within the current year.You can use that to build your format pretty easily.
如果
start_date
为1290895200
并且date
为1291154399
则offset
为259199
,天
为2
,小时
为23
if
start_date
is1290895200
anddate
is1291154399
thenoffset
is259199
,day
is2
andhour
is23
看一下
DateTime
模块,它很可能会有所帮助你。Have a look at
DateTime
module which will most probably be of help to you.