Justin Lin
Justin Lin

Reputation: 761

bash: query timestamp of UTC date on BSD

What I intent to get is

$ xxx 2019-10-11 <= insert your command
1570752000

The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd

Upvotes: 0

Views: 328

Answers (2)

Greg A. Woods
Greg A. Woods

Reputation: 2792

On NetBSD the following will work:

TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'

Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).

On MacOS you might try something similar to what Oguz suggested:

TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'

Upvotes: 0

oguz ismail
oguz ismail

Reputation: 50795

This should work:

date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'

-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.

Upvotes: 1

Related Questions