如何生成 Unix 时间戳?
相关问题是"Datetime To Unix timestamp",但这个问题更笼统。
我需要 Unix 时间戳来解决我的最后一个问题。 a> 我的兴趣是 Python、Ruby 和 Haskell,但也欢迎其他方法。
生成 Unix 时间戳的最简单方法是什么?
Related question is "Datetime To Unix timestamp", but this question is more general.
I need Unix timestamps to solve my last question. My interests are Python, Ruby and Haskell, but other approaches are welcome.
What is the easiest way to generate Unix timestamps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
nawk:
nawk:
在 Linux 或 MacOS 中,您可以使用:
其中
+%s
,自 1970-01-01 00:00:00 UTC 以来的秒数。 (GNU Coreutils 8.24 日期手册)示例输出现在为 1454000043。
In Linux or MacOS you can use:
where
+%s
, seconds since 1970-01-01 00:00:00 UTC. (GNU Coreutils 8.24 Date manual)Example output now 1454000043.
在红宝石中:
in Ruby:
在 Perl 中:
In Perl:
其中(GNU Coreutils 8.24 日期手册)
+%s
,自 1970-01-01 00:00:00 UTC 以来的秒数+%N
,纳秒 (000000000..999999999)自纪元以来示例输出现在
1454000043.704350695
。我注意到
date
的BSD手册没有包含关于+%s
标志的精确解释。where (GNU Coreutils 8.24 Date manual)
+%s
, seconds since 1970-01-01 00:00:00 UTC+%N
, nanoseconds (000000000..999999999) since epochExample output now
1454000043.704350695
.I noticed that BSD manual of
date
did not include precise explanation about the flag+%s
.在 python 中添加以下行来获取时间戳:
In python add the following lines to get a time stamp:
在 Bash 5 中,有一个新变量:
或者如果您想要更高的精度(以微秒为单位):
In Bash 5 there's a new variable:
Or if you want higher precision (in microseconds):
unix 的“date”命令的用途非常广泛。
获取
date
的输出,该输出将采用 -f 定义的格式,然后以 +%s, 秒的形式打印出来(-j 表示不要尝试设置日期)自纪元以来。The unix 'date' command is surprisingly versatile.
Takes the output of
date
, which will be in the format defined by -f, and then prints it out (-j says don't attempt to set the date) in the form +%s, seconds since epoch.首先,Unix“纪元”或零时间是 1970-01-01 00:00:00Z(意思是 Zulu 或 GMT 或 UTC 时区的 1970 年 1 月 1 日午夜)。 Unix 时间戳是自该时间以来的秒数 - 不考虑闰秒。
在 Perl 中生成当前时间相当容易:
生成与给定日期/时间值相对应的时间则不太容易。 从逻辑上讲,您使用 POSIX 中的
strptime()
函数。 但是,Perl POSIX::strptime 模块(与 POSIX 模块分开)具有签名:POSIX 模块中的函数
mktime
具有签名:因此,如果您知道数据的格式,你可以写一个变体:
First of all, the Unix 'epoch' or zero-time is 1970-01-01 00:00:00Z (meaning midnight of 1st January 1970 in the Zulu or GMT or UTC time zone). A Unix time stamp is the number of seconds since that time - not accounting for leap seconds.
Generating the current time in Perl is rather easy:
Generating the time corresponding to a given date/time value is rather less easy. Logically, you use the
strptime()
function from POSIX. However, the Perl POSIX::strptime module (which is separate from the POSIX module) has the signature:The function
mktime
in the POSIX module has the signature:So, if you know the format of your data, you could write a variant on:
在 Haskell 中
转到
C
in Swift
in Haskell
in Go
in C
in Swift
为了完整起见,PHP:
在 BASH 中:
For completeness, PHP:
In BASH:
如果我想使用 date 命令打印 utc 日期时间,我需要将 -u 参数与 date 命令一起使用。
示例
输出
If I want to print utc date time using date command I need to using -u argument with date command.
Example
Output
在 Haskell 中...
要将其作为 POSIXTime 类型返回:
作为整数:
In Haskell...
To get it back as a POSIXTime type:
As an integer:
让我们尝试 JavaScript:
...或者更好的是静态方法:
在这两种情况下,我除以 1000 来从秒变为毫秒,并使用 Math.floor 来仅表示已经过去的整秒(与. 四舍五入,可能四舍五入为尚未过去的一整秒)。
Let's try JavaScript:
...or even nicer, the static approach:
In both cases I divide by
1000
to go from seconds to millis and I useMath.floor
to only represent whole seconds that have passed (vs. rounding, which might round up to a whole second that hasn't passed yet).对于类 Unix 环境,以下内容将起作用。
https://tech.io/snippet/a3dWEQY
For Unix-like environment the following will work.
https://tech.io/snippet/a3dWEQY
使用 NodeJS,只需打开终端并输入:
node -e "console.log(new Date().getTime())"
或node -e "console.log( Date.now())"
With NodeJS, just open a terminal and type:
node -e "console.log(new Date().getTime())"
ornode -e "console.log(Date.now())"
在铁锈中:
In Rust:
如果您需要来自 shell 脚本(Bourne 系列:sh、ksh、bash、zsh 等)的 Unix 时间戳,这应该适用于任何 Unix 机器,与其他建议(perl、haskell、ruby、python、GNU date)不同),它基于 POSIX 标准命令和功能。
If you need a Unix timestamp from a shell script (Bourne family: sh, ksh, bash, zsh, ...), this should work on any Unix machine as unlike the other suggestions (perl, haskell, ruby, python, GNU date), it is based on a POSIX standard command and feature.