2008-12-14

Formatting phone numbers to standard

[edit: fixed Blogger messing up a regex and added a link to the E.123 amendment]

I just got my dev G1 phone on Friday which means I have been fiddling with my Gmail contacts to make them show up nicely in Contacts on Android. On top of always wanting accurate contact information I am also a stickler for formatting stuff to international standards (I blame my experience of writing time.strptime() and how painful it was to have to support so many different formats).

It turns out that there is a standard called E.123 from the ITU (wikipedia) which specifes how phone numbers, email addresses, and URLs should be written down. For international numbers it's what I am used to where spaces are used to separate everything out: +22 607 123 4567. For national numbers it is also what I would expect where parentheses are used to separate out the area code, although using a space to separate the first three digits from the last four is different: (607) 123 4567.

But what if you want to write a national number in an international format? Well, the standard is poorly formatted and missing key details to disambiguate the examples. While I would expect (607) 123 4567 to be formatted as +1 607 123 4567, the standard also shows it as +1607 123 4567 (notice the lack of space after the first 1). But based on international numbers I bet +1 607 123 4567 is the correct format.

But leave it up to the Internet to complicate things. RFC 3966 specified how the 'tel' URI works in listing a phone number in a link. The RFC complicates matters as it does NOT allow for spaces to be used in a phone number. The RFC admits that E.123 says to use spaces, but the amount of escaping required for those spaces was deemed too much of a pain. That led to the RFC allowing the use of '-', '.', and parentheses for digit separation, but not spaces.

But since this is for my address book and not some URI, I will just use spaces. I will probably also adopt the + to be correct and for the off chance I actually end up in some foreign country with a foreign telephone to use that knows how to properly handle the +.

And just two random points to share with people about phone numbers. One is how to dial internationally using an international phone number. All you need to do is substitute the + in the number for what you must dial to make an international call. From North America that happens to be 011. That means to call +22 607 123 4567 you would dial 011 22 607 123 4567. This is slightly annoying as the use of 1 for all countries under the North American Numbering Plan (NANP) means that if you were going to call out to NANP from within NANP you would dial 011 1 607 123 4567. Luckily you don't have to do that. =)

Second is how to specify emergency contacts in your address book. In the UK they started a trend of defining something called ICE (In Case of Emergency) in people's address books. Basically you put an entry in your address book entitled 'ICE1', 'ICE2', etc. and emergency personnel supposedly know to look for that when they find your cell phone. E.123 standardized this in Amendment 1 using a different notation: '0(P?<number>[1-9])(?P<relation>\w+)' (yes, that's a regex). The group 'number' is just some ordering and 'relation' is something descriptive like a name or how the person is related to you. This has the perk over ICE that these should be sorted to the top of your address book.