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 |
(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.
if (( timestamp < $(printf '%(%s)T' 'last sunday') )); then
calendar_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...
if perl -MDate::Manip -e 'exit 1 unless $ARGV < UnixDate("last sunday","%s")
' "$timestamp"; then....
If the aim is to check file timestamps, then note that FreeBSD
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.