I'm using the GNU date command to parse arbitrary natural language dates - for example to detect if a certain epoch time stamp is from last week, I can do:if [ $timestamp -lt $(date +%s -d last sunday) ]; then ...Now when I'm trying to port my script to FreeBSD, how do I achieve the same functionality? man date didn't show any promise, unless I missed something obvious.
Parsing arbitrary natural language dates under BSD
freebsd;date
There are a number of commands on FreeBSD that use the same API as GNU date to input natural language dates from the user. I've just found one that can be tricked into converting that date into Unix epoch time:/usr/sbin/fifolog_reader -B 'last sunday' /dev/null 2>&1 |  sed 's/^From[[:blank:]]*\([0-9]*\).*/\1/p'(note that at least on FreeBSD 9.1-RELEASE-p2 where I tried that on, it only seems to work reliably if you're in a UTC timezone and the date specifications it recognises are not necessarily the same as those recognised by GNU date).Note that some shells have that capability builtin.ksh93:if (( timestamp < $(printf '%(%s)T' 'last sunday') )); thenzsh:autoload calendar_scandatecalendar_scandate 'last sun'if (( timestamp < REPLY)); then...Or you could use perl and the Date::Manip if installed:last_sun=$(perl -MDate::Manip -e 'print UnixDate(last sunday,%s)')if [ $timestamp -lt $last_sun ]; then...Or:if perl -MDate::Manip -e 'exit 1 unless $ARGV[0] < UnixDate(last sunday,%s)    ' $timestamp; then....If the aim is to check file timestamps, then note that FreeBSD find supports:find file -prune -newermt 'last sunday'In this very case, if you want the time of the beginning of this week (weeks starting on Sunday), you can do:week_start=$(($(date '+%s - 86400*%w - 3600*%H - 60*%M - %S')))That should work on both GNU and FreeBSD (or any system where %s is supported).In timezones with summer time, that will be off by an hour around the switch from/to summer time.