Extracting Specific Date-time Components in Postgres

When programming, sometimes it's useful to extract various time components from a Date-time field in SQL queries.  For example, you might want to filter the date field by, hour, or year (or both).  Fortunately, Postgres has a easy way to pull this data out within the query itself:

SELECT EXTRACT(hour FROM a_datetime_field) FROM a_table;

If the field was the value '4/5/2009 13:09:00' then that above query would return "13" in the select.

In a stranger, practical example, I was moving a database from one server to another and for some unknown reason, all the dates in a table were fudged so that instead of being the year 2009, it was 142009, and the seconds were also stuffed up - the result being that any queries I ran against the table threw ADO.NET exceptions because the dates couldn't be parsed properly.  I needed to run a query like this one to set things right again:

UPDATE atable
  SET datetimeadded = cast( '2009-' || EXTRACT(month FROM datetimeadded) || '-' ||
    EXTRACT(day FROM datetimeadded) || ' ' || EXTRACT(hour FROM datetimeadded) || ':' ||
    EXTRACT(minute FROM datetimeadded) AS timestamp),
  datetimeupdated =  cast('2009-' || EXTRACT(month FROM datetimeupdated) || '-' ||
    EXTRACT(day FROM datetimeupdated) || ' ' || EXTRACT(hour FROM datetimeupdated) || ':' ||
    EXTRACT(minute FROM datetimeupdated) AS timestamp);

The casts are needed to get the update to cooperate, but basically this recreates the date time piece-by-piece based on the original date-time value.

Better way to get the Scheme, Host and Port from Uri Object in .Net

Recently I was working on a project where I needed to strip the scheme, host and port (if it wasn't 80) from a Uri object. Obviously I could have used string concatenation to get the value I needed, but it just seemed so ghastly and inelegant. In frustration and annoyance that it wasn't part of the standard Uri object definition, I asked a friend who gave me this little gem:

Uri uri = new Uri("http://localhost:6767/h/ello.php");
string url = new Uri(uri.AbsoluteUri).GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);

The string, 'url', will (in this example) hold the value "http://localhost:6767". This works in .Net 2.0 and up, and with .Net 3.0, you could even create a static method extension to make it even easier.

Sometimes, Microsoft Gets it Right (The .NET Framework 4)

Parallel Programming are two words which are not nearly used enough by programmers today. I think this is partially due to the fact that most developers are answerable to management types who simply "want to get the job done". It's also highly susceptible to deadlocks, race conditions and other problems, which are somewhat more avoidable in traditional single-threaded-apartment model applications. The problem is, that we've reached a precipice in CPU architecture where CPUs are scaling out instead of up. In other words, instead of simply working harder and faster, they're working smarter - executing many simultaneous operations.

The only problem with this is that applications need to be programmed to capitalize on this architecture - and making Multi-threaded applications easier is obviously on Microsoft's mind with the announcements of features in the upcoming The .NET Framework 4.0.

One of the main the main features I am looking forward to is the Parallel class to easily thread simple loops. This Parallel class represents a significant advancement in simplistically managing loops.  The .Net 4.0 team assures us that "for many common scenarios, it just works, resulting in terrific speedups".  A similar technique can be used to write parallel loops over iteration spaces of non-integral objects.

Parallel.For(0, N, i=>
  {
    DoWork(i);
  });

There are also overhauls to the ThreadPool class (which was in dire need of serious attention) and the inclusion of "Tasks" - simple generic types which assist developers in creating native IAsyncResult objects: this means that Task can be used as the core of a Begin/End implementation.  They've also really thought about these improvments, with easy and clear ways to cancel parallel operations, as well as a number of great ways to handle Exceptions within parallel blocks.

There are of course other advantages to the 4.0 Framework, but it's the big emphasis on easing MTA (Multi-Threaded Apartment) model application development that's got me excited.

Enjoy!

Upgrade Subversion Client on Mac OSX

As a long-time Windows user and programmer, I cannot state enough just how great Mac OSX is as a development environment.  It comes with so many tools already installed as standard ready to go (or at the very least on the OSX install disk).

Like I said, mac osx comes with a subversion client out of the box. At least Leopard does. If you do not believe me, try the following command and watch the output:

svn, version 1.4.4 (r25188)
compiled May 31 2008, 03:45:57

Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository access (RA) modules are available:

* ra_dav : Module for accessing a repository via WebDAV (DeltaV) protocol.
- handles 'http' scheme
- handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
- handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
- handles 'file' scheme

However, as you can see, the default Subversion client (type: svn --version in a terminal window) is quite old.  The biggest problem to me is that I use Versions and that stops NetBeans IDE and the command-line client no longer work.  But no fear! It turns out it is very easy to update it.

  1. Head over to http://svnbinaries.open.collab.net/servlets/ProjectDocumentList?folderID=164&expandFolder=164&folderID=0 and grab the correct version of subversion for you, download and install it.  So long as the new version is higher than the old, you can just install and it will copy over the old version.
  2. Make sure that the new binaries are on the path before the original subversion libraries.  To do this, issue the following command in a terminal: export PATH=/opt/subversion/bin:$PATH

If you type: svn --version into a terminal window again you will now see the version you installed.

svn, version 1.5.4 (r33841)
   compiled Oct 27 2008, 11:19:10

Copyright (C) 2000-2008 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository access (RA) modules are available:

* ra_neon : Module for accessing a repository via WebDAV protocol using Neon.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - with Cyrus SASL authentication
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme
* ra_serf : Module for accessing a repository via WebDAV protocol using serf.
  - handles 'http' scheme
  - handles 'https' scheme

Hope this post helps you use the new and improved subversion.

Why I Bought a MacBook

For my whole life, I've basically been a Microsoft Fanboi.  This isn't because I actually believed Microsoft was better, but more so that I was a big gamer; and well, games sucked on everything that wasn't a PC or a console.  My options were limited.

So after my impressionable teen years and I started my IT career, it was really only logical to continue down that road, since in my opinion at the time, Window's worked fine.  Linux and BSD operating systems were still notoriously difficult to use, and had swarms of issues and Apple machines we're for people who didn't want a REAL computer.  And since I still liked gaming – Windows was my de-facto standard operating system and I never thought that would ever change – until a couple of months ago when Vista finally broke what was left of my patience and spirit.

Unable to tolerate the Vista rhetoric any longer, and having been frustrated with the “omg, your computer's sex is on fire” beauty of the new Intel-based MacBooks I made the switch.  A long time ASP3, .Net developer by trade, doing the unthinkable – joined the army of  people who like to  wear berets, and watching Steve Jobs' latest Apple keynote speech.

And I have never been happier, nor have I looked back.

Looking back on it, I actually think that the problem started the moment that WinFS was dropped from the (then longhorn) feature set.  This was followed by more dropped features until there was nothing left to look forward to.  Vista ended up being all bling and no substance.  It is the Amy Winehouse of operating systems.  But still, despite this, despite the endless trouble with driver accessibility, despite the pitiful security solution called UAC, despite the endless and constant flow of problems; I persevered – hopeful in the knowledge that if I gave Windows some love, it would love me back.  This has always been the case with previous versions of Windows, why should this be any different?  But try as I might, as patient as I was, there just ain't no love coming from Vista.  It hates you from the moment you install it, until the day that you die.  And no matter how many service packs or patches I installed, it never got any easier.

It's like the spoiled middle kid from a dysfunctional family.  What's worse, is that I suspect Microsoft knows it!

And before all of my previous comrades start cheering for Windows 7, like its the second coming, can I just point out that Windows 7 is effectively a working, stable version of Vista.  Not much more.  And what's worse, your going to be charged for the pleasure of upgrading to what Vista should have been in the first place.

Having had several conversations with various Windows user's since the purchase of my Mac, has been an interesting experience.  I've come to realise, that Windows users have come to expect mediocrity from operating systems.  And when I try to explain some of the awesomeness of OSX I am met with either apathy or a serious lack of comprehension.

It didn't take me long at all to fall in love with the MacBook.  And this is just as much a tribute to Apple elegance (when they choose to use it) as it is about Vista sucking harder than a hoover.  But the single, final feature which sealed the deal was the magical MacBook, sleep and wake-up/resume functionality.  It sleeps and wakes up on he close/open of the lid like any other notebook.  But the speed at which is goes from fully awake → fully asleep → fully awake is simply staggering.  And, unlike Windows, I mean fully asleep (minimal battery usage) to a fully awake (and fully usable) state.  I have the 2.0GHz version of the 2009 aluminum MacBook, and the whole process is only a few seconds!  Simply amazing.

And much of the things which have truly impressed me are not even immediately obvious.  The best way I have found to summarize the difference is this; “All of the things that you [Windows users] have learned to live with, just gone!”  Windows in Finder (the equivalent to Explorer) open instantly, viewing folder file properties are virtually instant, search is staggeringly fast (this is called spotlight) and often, close to instant.

Even gaming isn't an issue (although I seldom get time for much of this these days) – I have a 30gb side partition for Boot Camp which runs Windows naively with correct drivers when I HAVE to use Windows with as much grunt as I can give it.  VMWare Fusion actually lets you run the Boot Camp installation as a virtual machine if you only need basic Windows functionality (such as testing browser computability).  This is sometimes to even needed, however, as many of the more popular games are sometimes available for OSX anyway.

I was expecting a lot more pain with the transition and its difficult to adequately paint the correct picture, but the simple fact is, MacBooks are amazing.

Enough said.

Plans of the Taylor House (from "Home Improvement")

I have recently been watching re-runs of the 90's sitcom "Home Improvement" and I think the design of the Taylor's house is just perfect.  After some googling, I stumbled across this site, which among other things had a plan of the ground floor and (modified) basement of Tim the Tool-man's house.  I grabbed the downloads and posted them here (in case that site ever goes down).

combined_plan

I created here (hosted as a mirror to the original here and here).

Project Euler Problem 48 Solution

The series, 1^(1) + 2^(2) + 3^(3) + ... + 10^(10) = 10405071317.

Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + ... + 1000^(1000).

My solution in Ruby:

sum = 0
for i in 1..1000 do
  sum += i**i
end
str = sum.to_s
 
puts str[str.length - 10,str.length]

UPDATE

s = 0
(1..1000).inject { |s, x| s + x ** x } % (10 ** 10)
str = s.to_s
puts str[str.length - 10,str.length]

Howto Backup PostgreSQL Databases Server With pg_dump command

Recently I had do to a lots of PostgreSQL database administration as I needed to move several databases onto  a production server.  PostgreSQL is one of the most robust, open source database servers available, and for my money, faster and generally better than MySQL. Like MySQL database server, it provides utilities for creating a backup.

Backup database using pg_dump command. pg_dump is a utility for backing up a PostgreSQL database. It dumps only one database at a time.

$ pg_dump table | gzip -c > table.dump.tar.gz

Another option is use to pg_dumpall command. As a name suggest it dumps (backs up) each database, and preserves cluster-wide data such as users and groups. You can use it as follows:

$ pg_dumpall | gzip -c > all.dump.tar.gz


Project Euler Problem 28 Solution

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21   22   23   24   25
20    7    8    9   10
19    6    1    2   11
18    5    4    3   12
17   16   15   14   13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

My solution in Ruby:

corners = [1]
num = 1
add = 2
500.times do
  4.times do
    num += add
    corners << num
  end
  add += 2
end
sum = 0
corners.each { |c| sum += c }
puts sum

Project Euler Problem 25 Solution

The Fibonacci sequence is defined by the recurrence relation:

F_(n) = F_(n−1) + F_(n−2), where F_(1) = 1 and F_(2) = 1.

Hence the first 12 terms will be:

F_(1) = 1
F_(2) = 1
F_(3) = 2
F_(4) = 3
F_(5) = 5
F_(6) = 8
F_(7) = 13
F_(8) = 21
F_(9) = 34
F_(10) = 55
F_(11) = 89
F_(12) = 144

The 12th term, F_(12), is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

My solution in Ruby:

t1, t2, term = 1, 2, 3
loop do
  temp = t1 + t2
  t1 = t2
  t2 = temp
  term += 1
  temp_s = temp.to_s
  break if temp_s.length &gt;= 1000
end
puts term
Enhanced by Zemanta