Wednesday, August 29, 2007

Shell date and time tricks

One of the things I hate the most when programming is dealing with time and date functions and all the special cases which exists. Even more, I have the feeling I'm walking a path many times walked before, so it becomes one of the most unpleasant tasks for me.

Fortunately, it's true, so many others walked before that path so here are some quick tricks for shell programming using the wonderful date UN*X program:
  1. Converting epoch:
    1. From epoch to anything else: date -d @$epoch_value +FORMAT (where FORMAT is of course as described on date(1), and '@' makes the actual undocummented trick).
    2. From anything else to epoch: date +%s
  2. Calculating times
    1. One day forward: date -d "1 day"
    2. One day backwards: date -d "1 day ago"
    3. Just imagine "1 month", "3 months ago" and the like. Not only google is so friendly with human language ;-)
  3. More format conversion: '-d' option accepts several other formats as input, even with calculations:
    1. date -d "1977-08-19 30 years", yeah! my 30th birthday was on sunday. Thanks date, and it was (as epoch): date -d "1977-08-19 30 years" +%s... 1187474400 :-D
    2. Funny ls:

      ls -l | while read perms links user group size d t name
      do
      echo $perms $links $user $group $size $( date -d "$d $t 1 day" ) $name
      done
      You can, of course, change the way date is shown. That's because 'YYYY-MM-DD hh:mm' is also a valid input format for date as it is 'YYYY/MM/DD'.
So '@', '1 day' and 'date' saved me the day.

Update: This features and more are indeed described in the coreutils info manual. Thanks mp for pointing it out.

Thursday, August 9, 2007

Be water (and light) my friend (part II)

Last night we were at Parquesur shopping center at Leganés, the place we are going to live in a year and a half. We had the chance to see the lights, water and music show is held there almost every summer night, so I was prepared, I had the camera and tripod at hand in prevision we would have the opportunity to take some pictures. Here are the best shots.