Related Entries

India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Adding namespace to XML
Opera RSS to OPML

« CD-R with Mandrake 9 and Toshiba 1905-S301
» Really beginning to like SQLite

Useless Python: deltadate

I finally wrote 5 lines to solve a major annoyance.

Ok, this might be a really silly script. But it has proven to be incredibly useful to me. What does it do?

It takes in two parameters, viz., delta and format. delta defaults to zero and format defaults to usual date default on my system.

And it outputs the date that is delta times different from current time.

Here is a sample session output.


%deltadate
Tue Feb 18 16:10:49 +0000 2003
%deltadate +1
Wed Feb 19 16:10:52 +0000 2003
%deltadate -1
Mon Feb 17 16:10:55 +0000 2003
%deltadate -2 ”%Y%m%d”
20030216
%deltadate +7 ”%Y-%b-%d”
2003-Feb-25

I think it can be used in rotating logs. If you are interested, you can get the script from CVS.

  1. This is a nice little script, but I don't understand what you use it for. Can you give an example?

    Posted by: Tom on February 19, 2003 11:28 AM
  2. I use it mainly for Log-file rotation. For example, I've files prepared in various machines that have log information, data dumps etc (these are mostly from packaged software - so I can't change it). Every day, I need to go and get yesterday's files. With this script, I can easily find out the file name of those files.

    In some instances, I need to get files from 3 days before (business rules). This helps. The files are usually transferred via custom transfer applications, so it makes it lot more easy to generate a file name and then get it.

    Posted by: Babu on February 19, 2003 12:38 PM
  3. Well, atleast on Linux, GNU's implementation of date command does the job this script does. You can say date --date='yesterday' to get yesterday's date. There are lot more options too. Check 'info date' to understand more options

    Posted by: Babu on April 19, 2003 05:57 PM
  4. I like it, just changed it to a function:

    def getDateStr(delta=0,format="%Y%m%d"):
    '''author : S Babu vsbabu_at_vsbabu_dot_org'''
    return time.strftime(format, time.localtime(time.time()+60*60*24*delta))

    I am using it to check the log files (20040308_load.log,20040307_load.log) from this morning and last night for errors.

    Posted by: bingo_tailspin on March 8, 2004 10:02 PM
  5. I am looking for something that will go thourgh a dirctory and del anything over six months old. Could this do something like that?

    Posted by: skipjack on September 19, 2004 03:07 PM
//-->