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.

15 comments:

Anonymous said...

@ is not actually undocumented. It is indeed not in the date manpage, but it is described in the coreutils info manual.

Mysterious Man said...

Do you explain me the same using Perl?
:D

Anonymous said...

...please where can I buy a unicorn?

Anonymous said...

[url=http://www.blingforfun.com]hip hop jewelry[/url],[url=http://www.blingforfun.com/pendants/cat_9.html]hip hop pendants[/url],hip hop watches,[url=http://blingforfun.com/belts/cat_18.html]bling bling[/url] ,hip hop,[url=http://blingforfun.com/chains/cat_7.html]hip hop chains[/url],hip hop bling,[url=http://blingforfun.com/chains/cat_7.html]iced out chains[/url],[url=http://www.blingforfun.com/chains/cat_7.html]wholesale chains[/url]
hip hop jewelry
wholesale hip hop watches
hip hop rings

jim said...

http://www.smartbargainsus.com/main/index.php

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=1

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=22

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=4

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=7

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=8

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=9

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=15

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=16

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=17

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=19

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=20

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=saleItems

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=2

http://www.smartbargainsus.com/main/index.php?act=viewCat&catId=21

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=18

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=20

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=16

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=24

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=78

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=63

http://www.smartbargainsus.com/main/index.php?act=viewProd&productId=65

Anonymous said...

saw this super hot picture... check it out

[url=http://babenudity.com/view/jailbait-tits-boobs/]Jailbait[/url]

Steve said...

Hello .. firstly I would like to send greetings to all readers. After this, I recognize the content so interesting about this article. For me personally I liked all the information. I would like to know of cases like this more often. In my personal experience I might mention a book called Generic Viagra in this book that I mentioned have very interesting topics, and also you have much to do with the main theme of this article.

sex stories women taboo said...

To do what. Everyone wins.
free interracial gay sex stories
family taboo stories
taboo blowjob stories
old gay sex stories
sci fi bondage stories
To do what. Everyone wins.

erotic stories taboo said...

Thinking back upon all my misery since then, I realize it was still,largely my fault. I had no choice, and once it got under way, Isorta lost it.
hot forced sex stories
granny incest stories
xnxx free stories
free rape incest text stories
story stories tales young sex
Thinking back upon all my misery since then, I realize it was still,largely my fault. I had no choice, and once it got under way, Isorta lost it.

Anonymous said...

Your blog is obviously definitely the most efficient . As a whole impact of the page is actually impressive .
locksmith Aventura FL
Locksmith Nashville
Daly City locksmith
Locksmith RedwoodCity CA
Locksmith Newark CA
Locksmith Santa Clara CA
locksmith plano tx
locksmith hialeah
locksmith miami fl
pembroke pines fl locksmith
Aventura FL locksmith
miami beach locksmiths
Locksmith Mesquite tx
miami fl locksmith
locksmith fort worth tx
locksmith aventura
locksmiths fort worth
locksmith hialeah
locksmith pembroke pines fl
irvine locksmith
miami locksmiths
pembroke pines fl locksmith
miami locksmiths
locksmith pembroke pines

Viagra online said...

Thank you for sharing your blog. Very interesing topics.

Dorothy said...

Quite helpful material, thank you for this post.

Dentist Seo said...

After this, I recognize the content so interesting about this article. For me personally I liked all the information.
Locksmith SEO

Anonymous said...


What things should i do in las vegas?

So, you are planning to travel to las vegas and wondering,"What things should i do in las vegas"?The options are really limitless when planning things to do in las vegas.There is a little of something for everyone that visits las vegas.Las vegas is not just a place for gambling at casinos, if you are not into that there are plenty of other options of attractions and things to do on the vegas strip.When planning a trip and planning things to do in las vegas, here are some things to consider.
If you have never been to the las vegas strip before, when considering things to do in las vegas, make sure you visit the las vegas hotels on the strip.The strip has dozens of fun themed hotels that have a lot to offer.One of the most famous hotels in las vegas is the bellagio.The bellagio offers a free water show in front of the hotel.You have probably seen pictures of it or on movies.The water fountain show is incredible and plays every day of the week, so make sure not to miss it!The great thing about las vegas hotels is that they can transport you to a different place or time.Anywhere from italy to egypt and back to medieval times.The hotels are so much fun!
If you are a thrill seeker, you might want to get an adrenaline rush with riding roller coasters!When planning things to do in las vegas, i always make sure i plan a roller coaster ride, they are my favorite attractions!Personally, my favorite roller [URL=http://www.nikeshoxs2013.com]Cheap Nike Shox nz[/URL] coaster in vegas is the manhattan express.The manhattan express is part of the new york new york hotel.The roller coaster wraps itself around the outside of hotel, taking you to great heights and giving you a huge rush!
A trip to las vegas would not be complete without seeing a show.Make sure to read the reviews [URL=http://www.nikeshoxsale99.com]Cheap Nike Shox R4[/URL] before you choose a show.There are plenty of types of shows to choose from when planning things to do in las vegas.Some of the shows include:Broadway musicals, acrobatics(Such as cirque du soleil), adult shows, family shows, and many more.If you are on a budget, you can find booths that will sell tickets for 1/2 off the same day of the show.


Anonymous said...

[url=http://www.vip1michaelkorsoutlet.org]Michael Kors Outlet[/url] - Worse than a wound is often the removal of a bandage

[url=http://www.mislouboutinsaleuk.co.uk]Christian Louboutin Outlet[/url]cheap skate shoes The continuous movement of the feet of skaters this super-abrasive surface wears her shoes in no time

[url=http://www.getfreerunaustralia.org]Nike Australia[/url]Even though your little girl is young enough to start walking, you can let her wear trendy and fashionable shoes for baby girls particularly to safeguard her delicate feet from infection and other foreign materials while taking her outside

[url=http://www.vipnikenewzealand.info]Cheap Nike Shoes[/url] If I don't trust you; I am going to stay far away from you

[url=http://www.upnikepascherfr.info]Nike Pas Cher[/url] ( AND DIDN"T EVEN LOOK FOR A COUPON!!! ) so i turned around and took them back! I told Dh when I got home