Monthly Archives: June 2011

Ruby and Rails – Time, Date, DateTime, TZInfo

From a discussion (Ruby – The Non-awesome parts) on the RORO email list – the differences between Time, Date, DateTime and TZInfo:

>> Time vs Date vs DateTime vs TZInfo (vs ActiveSupport::TimeZone). Which
>> do I use and when?

Time is a timestamp and can represent a moment in time no matter where
it occurred in the world, agnostic of timezone. Think UNIX timestamps.
Date is a thing on a calendar that might mean a different moment in
time to different people depending on their timezone. Think of Date
for things like recording your birthday. DateTime subclasses Date and
adds hour, minute, second and timezone information to a Date so it
becomes a lot like Time, but that makes me unsure when to use DateTime
and when to use Time, to be honest.

Time has timezone capabilities built in. TZInfo has a different set of
timezone capabilities and operates with Time objects, but seems to
ignore their built-in timezone settings. I’m still not sure why.

OSX, iPhone, iTunes – finer grained syncing

iTunes’ syncing of movies and podcasts is a big lame – there’s not enough fine grained control. For example, you can’t say “sync only this category of movies”, or “sync 30 minutes of podcast X, and 4 episodes of podcast Y, and only remove episodes of podcast Z after they’ve been listened to twice“. And at the moment I don’t have the time to migrate to Android and/or write my own podcast sync tools on Linux, so I’m stuck with iTunes/OSX. (I used to have some scripts for doing this for an ancient device, but device is dead and scripts are suffering from bit-rot).

One work around I’ve found is to categorise as much media as possible as either “Music” or “Music Videos”. You can then work with playlists and “smart” (sic) playlists to get a bit more control.

ssh – fix long login times – disable Kerberos

If ssh logins are taking a long time (but are ultimately successful), it may be because Kerberos (gssapi) is being attempted as an authentication method, and the destination is incorrectly configured (often seems to be CentOS or RHEL):

% ssh -v foo
...
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
                                                     ^^^^^^^^^^^^^^^

Quick fix: disable Kerberos on the client:

% cat ~/.ssh/config
...
GSSAPIAuthentication no

Of course the problem could also be DNS or other network issues. Thanks to Waiting for SSH login prompt.

git hooks – prepare-commit-msg

Update: foo commented that this was a “bad idea” (bordering on wrong?) and suggested I use options like git log –stat instead. See the comments below. Thanks foo!

When I’m working with git, I like being able to see a list of files that have changed (new, modified, deleted, etc) in the commit message. So when I do a git log, rather than getting this:

commit c1bcfdf4ebd9b0ae952b89f484558ee9eef720b3
Author: Sonia Hamilton
Date:   Fri Jun 17 21:15:53 2011 +1000

    dotjilot code changes

I get a nice message like this:

commit c1bcfdf4ebd9b0ae952b89f484558ee9eef720b3
Author: Sonia Hamilton
Date:   Fri Jun 17 21:15:53 2011 +1000

    dotjilot code changes
        modified:   setuplinux
        modified:   funcs

I used to manually edit the commit message on each commit, but git has a system of hooks that allows you to automate this sort of thing (look in the repo/.git/hooks directory for some examples). Here’s a script to automatically uncomment all changes in the commit message:

% cat prepare-commit-msg
#!/bin/sh
# use case statement for future flexibility - see prepare-commit-msg.sample
case "$2,$3" in
  *)
    /usr/bin/perl -i.bak -ne 's/^#// if /modified:|new file:|deleted:|renamed:/; print' "$1" ;;
esac

And here’s a quick script for setting this up across all your local repos. Remember – hooks aren’t synced between repos, so you need to do this on each machine you work on.

#!/bin/bash
# vim: ai ts=4 sts=4 et sw=4 ft=sh

gitdirs="foo bar baz"
for d in $gitdirs ; do
    if [ -d ${HOME}/${d} ] ; then
        cp prepare-commit-msg ${HOME}/${d}/.git/hooks
    fi
done

There’s only one disadvantage to doing this. Usually, you can just exit without saving the commit message and git will cancel the commit. But because the script modifies the message, there’s no nice way of telling git you want to back out (unless someone can tell me how…). I get around this by saving the message and doing a git reset –soft HEAD^ at the command line.