I have an upcoming deadline on Nov. 1st, 2011, and I want to print the days until the deadline every time I open Terminal on my MacBook Pro. I've added this to my .profile
:
python -c "from datetime import date; print '%s days until deadline.' % (date(2011,11,1) - date.today()).days"
And this prints 146 days until deadline.
whenever I create a new Terminal window.
This s开发者_开发百科olves my need, but I'm wondering what the one-liner looks like in other languages, or if there are ways to use bash or standard posix command line tools to do the same task.
echo $(expr '(' $(date -d 2011/11/1 +%s) - $(date +%s) + 86399 ')' / 86400) " days until deadline"
Using awk you can do:
echo "2011 11 01" | awk '{dt=mktime($0 " 00 00 00")-systime(); print int(dt/86400) " days";}'
OUTPUT (as of 8th June 2011)
145 days
Update (to get time difference rounded use below code)
awk '{dt=mktime($0 " 00 00 00")-systime(); d=dt/86400+0.5; printf("%d\n",d);}'
精彩评论