如何使用OS X BSD`date`命令将日期字符串转换为纪元时间戳? [英] How to convert date string to epoch timestamp with the OS X BSD `date` command?

查看:139
本文介绍了如何使用OS X BSD`date`命令将日期字符串转换为纪元时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的日期字符串Jul 27 23:59:59 2016 GMT,我需要使用OS X BSD date命令将其转换为等效的纪元时间戳.

I have a date string in the following format Jul 27 23:59:59 2016 GMT and I need to convert it to the equivalent epoch timestamp with the OS X BSD date command.

GNU date有一个不错的-d/--date=STRING自变量:

GNU date has a nice -d/--date=STRING argument:

$ date -d "Jul 27 23:59:59 2016 GMT" +'%s'
1469663999

不幸的是,OSX上的BSD date命令没有这样的选项.

The BSD date command on OSX sadly has no such option.

date -j -f "<FORMAT>" "Jul 27 23:59:59 2016 GMT" +'%s'似乎是解决方法,但是我找不到写入格式字符串. Apple的手册页指出:

date -j -f "<FORMAT>" "Jul 27 23:59:59 2016 GMT" +'%s' seems to be the way to go, but I can't find the write format string. Apple's man page states:

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"

can be used to parse the output from date and express it in Epoch time.

但这似乎不正确:

$ date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Failed conversion of ``Wed 13 Jul 2016 11:17:49 BST'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
        [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

如何使用OS X BSD date命令将这种格式的日期字符串转换为纪元时间戳?

How can I convert a date string in this format to an epoch timestamp with the OS X BSD date command?

我似乎无法从date中获得一个版本,但是如果有意义的话,我使用的是OS X 10.11.5(El Capitan).

I can't seem to get a version out of date, but I'm on OS X 10.11.5 (El Capitan) if that's significant.

推荐答案

您是这个意思吗?

date -j -f "%a %b %d %T %Z %Y" "Wed Jul 13 11:30:27 BST 2016" +"%s"
1468405827

我通过告诉date以与您使用的格式相同的格式输出结果:

I worked that out by telling date to output in the same format as you were using:

date -j +"%a %b %d %T %Z %Y"

这篇关于如何使用OS X BSD`date`命令将日期字符串转换为纪元时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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