将以秒为单位的时间跨度转换为shell中的格式化时间 [英] Convert a time span in seconds to formatted time in shell

查看:230
本文介绍了将以秒为单位的时间跨度转换为shell中的格式化时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个$ i变量,它在shell脚本中是秒,并且我试图将其转换为24小时HH:MM:SS.在外壳中可以吗?

I have a variable of $i which is seconds in a shell script, and I am trying to convert it to 24 HOUR HH:MM:SS. Is this possible in shell?

推荐答案

这是一种有趣的hacky方法,可以精确地执行您要查找的内容=)

Here's a fun hacky way to do exactly what you are looking for =)

date -u -d @${i} +"%T"

说明:

  • date实用程序允许您从字符串中以秒为单位指定从1970-01-01 00:00:00 UTC开始的时间,并以您指定的任何格式输出时间.
  • -u选项用于显示UTC时间,因此不考虑时区偏移量(因为从1970年开始的时间是UTC)
  • 以下部分是特定于GNU date的(Linux):
    • -d部分告诉date接受来自字符串的时间信息,而不是使用now
    • @${i}部分是如何告诉date $i以秒为单位
    • The date utility allows you to specify a time, from string, in seconds since 1970-01-01 00:00:00 UTC, and output it in whatever format you specify.
    • The -u option is to display UTC time, so it doesn't factor in timezone offsets (since start time from 1970 is in UTC)
    • The following parts are GNU date-specific (Linux):
      • The -d part tells date to accept the time information from string instead of using now
      • The @${i} part is how you tell date that $i is in seconds

      这篇关于将以秒为单位的时间跨度转换为shell中的格式化时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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