bash - 将秒转换为时、分、秒

标签 bash shell

如何将秒转换为小时、分钟和秒?

show_time() {
  ?????
}

show_time 36 # 00:00:36
show_time 1036 # 00:17:26
show_time 91925 # 25:32:05

最佳答案

使用日期,转换为UTC:

$ date -d@36 -u +%H:%M:%S
00:00:36
$ date -d@1036 -u +%H:%M:%S
00:17:16
$ date -d@12345 -u +%H:%M:%S
03:25:45

限制是小时数将在 23 点循环,但这对于您需要单行的大多数用例来说并不重要。

在 macOS 上,运行 brew install coreutils 并将 date 替换为 gdate

关于bash - 将秒转换为时、分、秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12199631/

相关文章:

linux - 在 linux 脚本中访问在 docker 容器中运行的 mongo shell

linux - 在shell中如何处理/usr/sbin和/usr/local/sbin?

c++ - Bash 可以从 C++ 程序接收参数或返回值吗?

bash - 当 grep 找到匹配项时执行命令

regex - Grep 正则表达式 : How to find multiple area codes in phone number?

bash - UNIX 'join' 命令在意外标记 `(' 附近引发语法错误

linux - 删除除每个前缀的最近 n 个文件之外的所有文件(基于文件日期)?

bash -/bin/sh^M : bad interpreter: No such file or directory

bash - 如何在 bash 脚本中正确循环?

Linux 自动启动