[Tutor] Equivalent to perl -e
David Rock
david at graniteweb.com
Mon Oct 16 17:49:04 CEST 2006
* Chris Lasher <chris.lasher at gmail.com> [2006-10-15 22:07]:
> Haha! I'll relay that message! Thanks Kent and Glenn!
>
Here is one I actually use in real life. I needed something to figure
out what the previous year, month, etc for rolling up old log files.
The best thing I could think of for date calculation was datetime.
This is embedded inside a shell script.
python -c '
import time
import datetime
dtup_now = time.localtime()
y,m,d = dtup_now[:3]
d_today = datetime.datetime(y,m,d)
d_delta = datetime.timedelta(d_today.day)
last_month = d_today - d_delta
d_delta = datetime.timedelta(last_month.day)
two_month = last_month - datetime.timedelta(last_month.day)
d_delta = datetime.timedelta(two_month.day)
del_month = two_month - datetime.timedelta(two_month.day)
print "%d %02d %d%02d" % (last_month.year, last_month.month, del_month.year, del_month.month)'
What you will notice is it gets complicated in a hurry if you try to do
loops or anything fancy because of formatting constraints. Not that it
can't be done, but it would hurt to try. :-)
--
David Rock
david at graniteweb.com
More information about the Tutor
mailing list