Newsgroups: gmane.linux.debian.user Date: Sat, 13 May 2006 14:04:01 -0400
> Is there any nice tool to operate dates at the console? > thanks!
It's perhaps a bit too heavy, but I always use Perl's DateManip for that: http://search.cpan.org/~sbeck/DateManip-5.44/Manip.pod
I had to write a script a while back that computed "one week before today"; DateManip's way of writing that is quite literate and quite easy to code:
LASTINTERVAL=`perl -e "use Date::Manip; print UnixDate(DateCalc(\"now\",\"-1 week\"), \"%d/%b/%Y\")"`
In general, I should note that "perl -e" is super-awesome.
The alternative, using just the command-line 'date' tool, is too cumbersome. Perl's actually the easiest here, I've found.
Stephen R. Laniel
> I am working on a script that needs to compute days. It needs to know > how many days have gone since some given date. for example, if I run it > today, it will need to know how many days have passed since, say, Feb, > 02, 2006. And add the corresponding number to a variable.
Since you're doing this in a script, it presumably doesn't need to be done in a single line. I'd start with
date -d "Feb 2, 2006" +"%s"
to get the number of seconds since the epoch for midnight on the date specified. Then I'd do
date +"%s"
to get the same for the current time. Subtract those, and you get the number of seconds since Feb 2, 2006. There are 86400 seconds in a day. Divide, throw out the remainder, and you've got the number of days since Feb 2, 2006.
If you *must* have a one-liner:
expr \( `date +"%s"` - `date -d "Feb 2, 2006" +"%s"` \) / 86400
Michael A. Marsh
On 5/13/06, Stephen R Laniel wrote:
> That sort of work is precisely why I do it in Perl. Because > then you start getting into messiness with leap years, > timezones, etc., etc., etc. There's a reason that time > libraries are hard to write. :-) Perl's done all the work > for you; be lazy.
What do leap years have to do with it? Leap *seconds*, maybe, but I'm willing to be off on the count of days by a few seconds. If timezones are a real problem, you can always specify the timezone after the year. date has done all the work for you.
Michael A. Marsh