Warning: The following post has the same effect as 19 cups of coffee before bedtime. If you are in doubt you will be able to handle it, I suggest you don’t read any further.

Every once in a while, you are presented with a product or a design or even an idea that you immediately think is da bomb even though you can’t really explain why. It may not be all that brilliant, but there is something lovable about it you can’t ignore or easily forget. It stays with you in your dreams until you go purchase the product or implement in your own. It’s what showbiz calls the x-factor.

What I have for you today, dear reader is nothing less than that. A method you need for all your projects, but have never realized it before. Trust me when I say that you might as well implement it everywhere right now or you are going to lose a lot of sleep until you do. Hold on to your seat and get ready to be amazed.

Let’s start by seeing what it is you do wrong today when you code your software and then see how to make it much better with a little brilliance.

The problem

When writing an application for either web or desktop, we often display dates and time to the visitor or user. It could be the date and time of an invoice or the time of arrival somewhere. What we have done in the past is simply writing out the time in a pretty boring way like so:

11:48:09 PM
or
23:48:09

You might be wondering what is wrong with the given examples and I understand that, because that is the way time has always been formatted in most parts of the world. In other parts the delimiter might be a dot or comma, but otherwise pretty much the same.

Now ask yourself if it is important for your users to know the precise time down to the minute and do they care about the seconds? The answer in most cases is a laud and clear NO. Nobody cares. We’ll ignore the cases where users do care because that would give us a laud and clear NO in 100% of the cases.

The solution

We just established that 100% of your users do not care about precise stated time. Instead let’s give them something that makes sense. Enter the brilliant method you didn’t know you needed.

The method is called ConvertToRelativeTime and it takes a DateTime as parameter and returns a string. If we stick to the above examples of the time 11:48:09 PM and feed it as a parameter to the brilliant method, then we get this output:

A quarter to midnight

Other outputs could be:

Half past eleven pm
About three am
A quarter past eight am 

I’ll give you a few seconds to wipe off the hot coffee you just spilled in your lap…

In a busy world where time is everything and money is made by the second, we need to relax when we can. This is one such way. A sort of an anti-stress approach to time display.

The idea about relative time is not mine. I got it from 37signals who got it from someone else. I fell in love with it the moment I saw it, and for the past week I’ve been dreaming and losing sleep because of it – just like you will. You don’t need to worry though, because I have written the code for you to grab and implement.

The code

Ok, it's actually two methods - a public and a private helper to the public. 

public static string ConvertToRelativeTime(DateTime date)

{

  string relative = string.Empty;

  int hour = date.Hour;

  string meridian = date.Hour > 12 ? "pm" : "am";

 

  if (date.Minute < 8)

  {

    relative = "about";

  }

  else if (date.Minute > 52)

  {

    relative = "about";

    hour++;

  }

  else if (date.Minute < 22)

  {

    relative = "a quarter past";

  }

  else if (date.Minute < 38)

  {

    relative = "half past";

  }

  else if (date.Minute <= 52)

  {

    relative = "a quarter to";

    if (hour == 23)

    {

      hour = 0;

      meridian = string.Empty;

    }

    else

    {

      hour++;

    }

  }

 

  if (hour == 0)

  {

    meridian = string.Empty;

  }

 

  string name = GetHourText(hour);

  return relative + " " + name + " " +  meridian;

}

 

private static string GetHourText(int hour)

{

  if (hour > 12)

    hour = hour - 12;

 

  switch (hour)

  {

    case 1: return "one";

    case 2: return "two";

    case 3: return "three";

    case 4: return "four";

    case 5: return "five";

    case 6: return "six";

    case 7: return "seven";

    case 8: return "eight";

    case 9: return "nine";

    case 10: return "ten";

    case 11: return "eleven";

 

    default:

      if (hour == 0)

        return "midnight";

      else

        return "twelve";

  }

}

If you tweak it a bit you can display time in even more relative terms. What about these examples:

Two and a half hour to lunch
About an hour past bedtime
Work day ends in about 3 hours

You have a Facebook profile with a lot of connected friends. You share your interests and personal information with those friends. You also have a LinkedIn profile with just as many connected contacts with whom you share your job related information. All your pictures are in the hands of Flickr and your Twitter account tells your friends what you are up to on an hourly basis. Your mobile phone’s phonebook is handled by ZYB and all your personal videos are located at YouTube. All your favourite links are available from del.icio.us and your appointments are controlled by Google Calendar.

Does this sound familiar? If it does then here are some questions for you.

Think about these questions

  • Why maintain your profile information on multiple sites?
  • Who owns all this information?
  • Are you in charge of your online identity?

If you’re like me, you have a hard time answering those questions. Most people probably don’t think of this, but you are living the online connected life and should have an opinion about this. Not because of privacy issues, but because you know there must be an easy answer to the questions. You just can’t find them and it might bother you. So where do you go from here?

The semantic web

Ok, so you’ve heard about the semantic web but what does it mean in relation to all this? This is the real question and it will become the answer to all the previous questions.

At the moment we have billions of web pages floating around as islands in the cloud. All the information on those web pages is isolated and doesn’t give any meaning to computers. You have to be a human to understand the meaning of even the simplest web page. If it contains an address or information about an upcoming event you have to be a human to understand that. The simple web page might also contain information about your interests and a review of movie you just saw. If you have a blog then you probably also have a blogroll where you list the websites of your friends. Still, you have to be a human to understand that.

That’s where the semantic web kicks in. It knows about all this information and makes it searchable in a way that Google can only dream about. All you need to do is to adhere to some standards when you write information like addresses, calendar events, interests, friends etc. Standards like hCard, hCalendar, APML, SIOC, XFN and FOAF. These standards are machine readable and can be adopted by any website without changing the design and layout. I suggest you take a look at each of the standards if you haven’t heard of them before. Trust me, it’s damn easy to implement.

Now, imaging that you have a single web page that contains all this information in a machine readable way. This will become your central location of your online identity. Because it is written in a machine readable way, all the social networking sites you use can consume this information automagically.

The standards for the semantic web are ready for you to use. Three things are still missing for us to achieve our goal of a centralized online life and they are the key to success.

The final three steps

Step one: In order to have a centralized online life from where all information is spread and maintained across sites and social networks, the first thing we need is a web page suitable for this. This is where things get tricky. It has to be a page that you and only you own and control. A personal website is the natural choice, but then you need to know how to mark up your information according to the various standards and that is not something that the average Facebook user is capable of creating.

There could also be services that allow you to maintain this centralized profile for you. Facebook could be one such service, but then you still wouldn’t really know who owns your data – you or Facebook. No, as I see it, only a personal website puts you in total control. Hang on, we’ll get there.

Let’s take it to the second step and assume you have a personal website that holds your personal information only you control.

Step two: Social networking sites have to be capable of consuming the information from your personal website – your secure centralized information location. This will only happen if enough people have a centralized location and, by that end, the demand is great enough for the sites to implement support for it. This will not happen just by a few people implement the standards on their website. We need something more that will drive a much bigger user base.

We need all the personal CMS and blog platform vendors to step up to the plate and provide this functionality out-of-the-box. Average Joe should not need to know about the inner workings of these standards, he just need to know that when he enters his personal information it will work in a cross-site manor. This is what he should think after he entered his information on his central location and visits Facebook to create an account:

I’ve already given the Internet my information once, why should I do it again? Here is my website URL, which should be enough for you to gather what you need in order to create my profile.

Step three: All this should be achieved by only having one set of credentials no matter where you sign up or sign in. OpenID takes care of that in a beautiful way. You sign in to your central location using your OpenID credentials and stay logged in whenever you visit LinkedIn, Twitter etc. The scenario now becomes very smooth and transparent.

The perfect scenario

Sign in to your personal website using OpenID and fill in your profile, interests, contact information, list your friends and calendar events. Some of this you synchronize with Outlook, Messenger etc. Now you have an only identity that you control and own.

You have heard about Flickr so you visit the site and sign up with the same OpenID credentials. Then you start uploading your photos. Flickr automagically knows about your friends, so you can tag all of your pictures with the friends you already entered and they will be notified by e-mail even though they don’t have a Flickr account.

Now you move on to Facebook to sign up. Facebook automatically connect you to the friends you have entered on your personal website and they import the photos from Flickr as well. It also import your interests, favourite movies etc. and adds it to your profile.

Then you find a girlfriends/boyfriend and update your status from single to in a relationship on your personal website. That status then automagically updates in your Facebook account without you need to do anything. You also signs up to ZYB and from there you change your phone number. That information is then pushed back to your personal website and updated – probably with you having to approve it.

The circle is now complete. Data flows to and from your central location. You are in total control all the time from a centralized location you own. Information is shared cross multiple websites and is always updated.

Start the revolution

To start implementing the above scenario, we as developers need to be pro-active. This is not something that will happen because Tim O’Reilly, Steve Ballmer or anyone else says so. Actually they already have but it stays in limbo.

It begins by you, the visionary developer, starts to implement one or more of the standards on websites you build. It starts by you convincing your boss to implement it in your line of CMS products. It starts by you influencing decisions makers at your job.

It starts by you. And me. All developers.

The standards are ready to be used. Open source components and libraries have already been developed to let you consume these different standards on various platforms like ASP.NET, PHP and Ruby. Do a quick web search and you'll see. In other words, both ends of the equations already exist – the client and the server technologies and standards.

An easy way to start is to implement microformats into existing websites in the form of contacts, calendar events, interests and friends are tagged up and ready to be consumed by social networks. Next you can think about implementing FOAF which provide more details to your friend list. APML will more detailed describe your interests and SIOC can describe how you and your contacts interact with each other on the web. I would go by doing it in that order.

This is just thin air coming from me if I didn’t believe so much in it. That’s why the next version of BlogEngine.NET will start incorporating this vision. Remember, we developers are the ones it boils down to about creating the semantic web. We are the ones that have to take responsibility in driving the web forward like no one else can.