1

My google-fu has failed me so I'm hoping someone here can point me in the right direction. I'm in the middle of doing some data validation efforts and I suspect the system is recording some invalid dates in the unix epoch format (seconds since the epoch). Is there an easy way in bash, given data that looks like 1357853083 (seconds since the unix epoch) to ascertain if it's a valid unix timestamp or not? Ideally I'm looking for timestamps that cannot be converted to a human readable date as well as timestamps that might render to a future date that's greater than the current date.

I've compiled this monstrosity which converts things into a human readable date, but I can't figure out how to make it find invalid dates.

$ grep "delimiter" * | grep -v "delimiter2" | awk -F"," '{print $2}' | awk -F":" '{print $2}' | awk -F"*" '{print $1}' | awk '{printf("%s", strftime("%Y.%m.%d ",$1)); printf("%s", strftime("%H:%M:%S \n",$1))}' | sort -V
3

So it looks like you want to do two tests.

  1. Check for a valid epoch time number which would be from -2147483648 to 2147483647 or +-9.22337203685478E18 depending on whether your system uses 32 or 64 bits for time.
  2. You want to check for a number that is less than today's date.

So combining the two tests you are looking for numbers in the range -2147483648 to today's date in epoch time. The below code should do what you want, although I would raise the lower limit to a reasonable epoch time date such as the date when the computer was installed or your OS compiled. I arbitrarily set the lower limit to 0 seconds or 1/1/1970.

#$i stores the value being tested
#returns failed if it fails validation, otherwise it returns passed
if [ $i -le 0 -o $i -gt `date +%s` ]; then echo "failed"; else echo "passed"; fi;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.