How to get epoch time in shell script (for ksh)?

不想你离开。 提交于 2020-01-17 08:13:07

问题


How to get epoch time in shell script (for ksh)? I am interested in getting epoch time for the start of day (so e.g. now is July 28th, 2011 ~ 14:25:00 EST, I need time at midnight).


回答1:


If you have GNU date,

epoch=$( date -d 00:00 +%s )

Otherwise, if you have tclsh,

epoch=$( echo 'puts [clock scan 00:00]' | tclsh )

Otherwise,

epoch=$( perl -MTime::Local -le 'print timelocal(0,0,0,(localtime)[3..8])' )



回答2:


ksh's printf '%(fmt)T' supports time calculating. For example:

$ printf '%T\n' now
Mon Mar 18 15:11:46 CST 2013
$ printf '%T\n' '2 days ago'
Sat Mar 16 15:11:55 CST 2013
$ printf '%T\n' 'midnight today'
Mon Mar 18 00:00:00 CST 2013
$


来源:https://stackoverflow.com/questions/6863827/how-to-get-epoch-time-in-shell-script-for-ksh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!