Monday, March 22, 2010

git svn usage notes

I've been using git-svn recently, and like it quite a bit. The killer feature for me is that I can make commits while off-line; I like being able to commit changes in small chunks. Here are some notes on how I most frequently use it.
  1. Start. Make local copy of trunk, branches, tags and revision history.
            $ cd ~/src/git
            $ git svn clone svn://myprj
            $ cd myproj/trunk
    
  2. Push your local changes to SVN repository.
            $ git svn dcommit
    
  3. Revert your local changes to last version you committed.
            $ pwd
            /home/mark/src/git/ao/trunk/wwwrpt
            $ git show HEAD:trunk/wwwrpt/Makefile
            $ git show HEAD:trunk/wwwrpt/Makefile > Makefile
    
  4. Revert to the version before your last commit.
            $ git show \
            > HEAD~1:branches/2.2_WORKFLOW/engine/session.h \
            >> engine/session.h
    
  5. Fix commit message on last commit. (Must do before dcommit.)
            $ git commit --ammend
            [spawns editor with prior comment: edit and save]
    

Tuesday, February 2, 2010

Seconds since epoch from Python datetime

My naive approach, given a naive Python date:
$ cat > t.py
from datetime import datetime
dt = datetime(2010,2,2,13,52,39)
dt.strftime("%s")
^D
$ python t.py
1265136759
But that's not right:
$ TZ=GMT date -r 1265136759
Tue Feb  2 18:52:39 GMT 2010
$
To get the correct seconds, you need to first set timezone to GMT.
$ cat > t.py
import os
from datetime import datetime

oldtz = ''
if os.environ.has_key('TZ'):
    oldtz = os.environ['TZ']
os.environ['TZ'] = 'GMT'
dt = datetime(2010,2,2,13,52,39)
print dt.strftime("%s")
if oldtz:
    os.environ['TZ'] = oldtz
else:
    del os.environ['TZ']
$ python t.py
1265118759
$ TZ=GMT date -r 1265118759
Tue Feb  2 13:52:39 GMT 2010
$
The difference in these two second values is 5 hours, which is my local time zone offset from GMT. So although the Python date has no timezone information, when Python's strftime() converts it to seconds since epoch, it uses the local timezone instead of GMT. I consistently find dates and times to be tricky when programming.