Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
1  
This might help give you some ideas: unix.com/tips-tutorials/… –  JoshRagem Jan 10 '13 at 22:47
    
Thanks, Josh! I'll take a look. –  FloppyDisk Jan 11 '13 at 13:52

1 Answer 1

up vote 3 down vote accepted

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;
share|improve this answer
    
Just what I needed. Thanks, Michael! –  FloppyDisk Jan 11 '13 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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