Best Way to Get File Modified Time in Seconds

Since it seems like there might not be a "correct" solution I figured I'd post my current one for comparison:

if stat -c %Y . >/dev/null 2>&1; then
    get_modified_time() { stat -c %Y "$1" 2>/dev/null; }
elif stat -f %m . >/dev/null 2>&1; then
    get_modified_time() { stat -f %m "$1" 2>/dev/null; }
elif date -r . +%s >/dev/null 2>&1; then
    get_modified_time() { date -r "$1" +%s 2>/dev/null; }
else
    echo 'get_modified_time() is unsupported' >&2
    get_modified_time() { printf '%s' 0; }
fi

[edit] I'm updating this to reflect the more up to date version of the code I use, basically it tests the two main stat methods and a somewhat common date method in any attempt to get the modified time for the current working directory, and if one of the methods succeeds it creates a function encapsulating it for use later in the script.

This method differs from the previous one I posted since it always does some processing, even if get_modified_time is never called, but it's more efficiently overall if you do need to call it most of the time. It also lets you catch an unsupported platform earlier on.

If you prefer the function that only tests functions when it is called, then here's the other form:

get_modified_time() {
    modified_time=$(stat -c %Y "$1" 2> /dev/null)
    if [ "$?" -ne 0 ]; then
        modified_time=$(stat -f %m "$1" 2> /dev/null)
        if [ "$?" -ne 0 ]; then
            modified_time=$(date -r "$1" +%s 2> /dev/null)
            [ "$?" -ne 0 ] && modified_time=0
        fi
    fi
    echo "$modified_time"
}