Friday, February 18, 2011

Minimalistic to-do

Last month while browsing on net, I found blog post on how to create minimalist to-do list in bash shell. Code was simple, 3 lines in .bash_profile. Since I day spend considerable amount of time in iTerm2 (alternative to Terminal app on Mac OS X) and I hardly close iTerm2, I thought to give it a try and see how it works out for me. After using for around a month, I'm finding it easier to manage my to-do list. And with help of Dropbox on Mac and PlainText app on iPhone, I'm able to sync list between my Mac and iPhone. Unfortunately I lost the link to blog post, so pasting code here.

Add following in .bash_profile
# Todo start
# Store in PlainText iPhone app folder in Dropbox for sync
export TODO=~/Dropbox/PlainText/todo.txt
function todo() { if [ $# == "0" ]; then cat $TODO; else echo "* $@" >> $TODO; fi }
function todone() { sed -i "" -e "/$*/d" $TODO; }

Reload profile
$ source .bash_profile

Add task to to-do list
$ todo write test case of bug #345
$ todo refactor worker class
$ todo check 280slides.com

Display to-do list
$ todo
* write test case of bug #345
* refactor worker class
* check 280slides.com

Remove task from to-do
$ todone refactor

To remove to-do item, only part of description is required. This might sometime get messy if list is long, but hey, isn't goal is to keep getting things done in to-do list and try to keep it as small as possible?

I also used GeekTool to display to-do list on my desktop on second monitor.

Tuesday, February 08, 2011

Thursday, February 03, 2011

Calculate number of days between two dates

Computing number of days between two dates is simple mathematics. This is how it is done.

public int differenceInDays(Calendar start, Calendar end) {  
    long ms = Math.abs(start.getTimeInMillis() - end.getTimeInMillis());
 
    return (int) (ms / (1000 * 3600 * 24));
}
But does this work in all cases? Not exactly. Take a look at this code:

public Calendar newDateInstance(int year, int month, int day) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.DAY_OF_MONTH, day);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    return cal;
}

Calendar start = newDateInstance(2011, 3, 13);
Calendar end = newDateInstance(2011, 3, 14);
System.out.println(differenceInDays(start, end));

This code prints 0. Why? Because on 14th March morning Daylight Saving starts and an hour is lost. One solution I use is to add 1 hour to end date to avoid loosing a day. Here's how above code is rewritten.

private void resetTime(Calendar cal) {
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
}

public int differenceInDays(Calendar start, Calendar end) {  
    resetTime(start);
    resetTime(end);
 
    // This is to avoid loosing a day when daylight saving starts(14th March)
    end.set(Calendar.HOUR_OF_DAY, 1);
 
    long ms = Math.abs(start.getTimeInMillis() - end.getTimeInMillis());

    return (int) (ms / (1000 * 3600 * 24));
}