How to set date with Epoch format

How to set date with Epoch format

I want to set date from a seconds since epoch value, for instance I want to set date with input value 1452053571.

I read through date -help but not found anything.

Is there any parameters to do it?

答案1

With GNU date, you can use the same date string format for both -d and -s options.

To convert from seconds since epoch to human readable format:

date -d '@2147483647'

To set it:

date -s '@2147483647'

With *BSD date:

# Convert seconds since epoch
$ date -r 2147483647 
Tue Jan 19 03:14:07 UTC 2038

# Set date by seconds since epoch
$ date "$(date -r 2147483647 +'%y%m%d%H%M.%S')"
Tue Jan 19 03:14:07 UTC 2038

相关内容