Thursday, December 20, 2012

Formatting dates in iOS

Hi. I think this memoir will sound pretty basic for a lot of people out there, but the spirit of my blog is this, store experiences, solutions, details that I wouldn't remember, because I won't deal with them too often, because brain space is too precious to be wasted, or because I am too dumb to remember the stuff I am messing with. Anyway, this is a piece of code I am using to format dates in an iOS project of mine:


- (NSString*)formatDateTime
{
    NSDate *now = [[NSDate alloc] init];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd;MM;yyyy;HH;mm;SS"];
    NSString *dateTime = [dateFormatter stringFromDate:now];
    return dateTime;
}

There are some tricky details here, about the format string. A valid and desired output for me would be something like 20;12;2012;20;04;22. But this code can actually deliver something like 20;12;2012;21;49;78. We surely don't want to show anything like 49 minutes and 78 seconds. 

Let's see this, piece by piece. If you write @"DD;MM;yyyy;HH;mm;SS" you will obtain an output as 355;12;2012;20;04;22, because the capital D specifies day of the year (In this example, December 20th is the 355th day of the year 2012). So you should specify lower case d to obtain the day of the month (20 in my example). Capital M or MM for months will deliver the numerical value of the month of the year (1 for M, or 01 for MM, to 12). MMM will output the abbreviated form for the name of the month (for instance, "Dec") and MMMM will output the complete name of the month ("December"). 

A capital H specifies that you want to show the hour of the day in the range from 0 to 23. A lower case h will produce the hour in the range from 1 to 12. Lower case m or mm will produce the value of the minutes part of the hour, respectively paddled or not. Finally, a capital S will produce the fractional value of the seconds part of the hour, which means a decimal value that will make no sense for your user in most cases. A lower case s will deliver the expected value from 1 to 59.

Doubling d, m (for months), h, m (for minutes) and s, as we have already mentioned, will paddle the zero (01  to 09 instead of 1 to 9), which is usually the desired aesthetics. So my final formatting string becomes @"dd;MM;yyyy;HH;mm;ss".

I haven't spoke about the year part, but you should use yyyy instead of YYYY, to show the year with four digits (or yy, to have it with two digits). There is a technicality here. The capital Y actually specifies that you are using ISO 8601 year-week calendar, which in general will deliver the Gregorian calendar expected value, but exceptions are allowed. This is something that most of the time will not deserve your concern, but is an usual advice adopting the lower case format as a default.

Finally, these rules are not iOS or Mac specific. Actually, they come from Unicode Technical Standard #35, which means that, as a Unicode standard, they are pretty much ubiquitous. 

Um abraço!





Saturday, December 10, 2011

Offline Maps in Android Applications - Part 2

We will continue this sequence of articles about offline maps in Android, showing how we can create a disconnected set of maps, which we will show using Osmdroid API. We will start by download and install Mobile Atlas Creator, a free map visualization and atlas creation tool, which works with many different map sources like Google Maps, Yahoo Maps or Open Street Maps.

Mobile Atlas Creator interface looks like this.



















Lets define a new atlas! At the left panel, use the "Map source" list box to choose an online data source. In this example I am using Google Maps. The next step is moving the map on the right panel to select the part of the world you desire to show on your application. You can use the mouse to select a rectangular area on the map. The coordinates of this area will appear at the "Selection coordinates" on the top of the left panel (you can also, of course, input the coordinates directly on the N, W, E and S text boxes). After this, click on the check boxes named from 0 to 19 on the "Zoom Levels" option, to select the zoom levels you want to use. I recommend you try from 0 to 15 to follow this tutorial. Type some tittle (for instance, "MyMaps") for your atlas at the "Name" text box, and click the "Add selection" button. The "Atlas Content" box will be filled with the "MyMaps" atlas features you have just defined.

In order to really create your atlas, select "OSMAND tile storage" format on the "Atlas settings" section and click the "Create atlas" button. A dialog will appear, showing the progress of the download of the images and creation of the maps that will compose your atlas.


The resulting atlas will be stored at the "atlases" sub-folder of the Mobile Atlas Creator installation directory, into a new folder named with some identifier like "Unnamed atlas_2011-12-06_232235". Inside this folder you will find another folder named after your selected map source, in this case "Google Maps". Take a look inside this folder. There is a collection of numbered folders. These numbers indicate the zoom levels you have defined, and inside each zoom level folder you will find another collection of numbered folders containing the real tile images, with numbers as names and the extension .png.tile, which is the extension that Osmdroid API will recognize.

You can now set the content of the resulting atlas folder into a ZIP file, lets say "MyMaps.zip" (remember, this content will be a folder named after your online maps source). Our next post will show how to implement our offline map application using this file as its data source. See you soon!

Thursday, December 8, 2011

Offline Maps in Android Applications - Part 1

The canonical way to present maps in Android applications is using com.google.android.maps.MapView class, which has Google Maps has its unique data source. This implies that you are restrained to a single map source, and that your application  must have an available data connection and internet access privileges in order to show maps. Offline maps are not possible (at least while I write these lines).

Open Street Map provides a solid alternative to Google Maps. It was conceived following the model of Wikipedia: Content is provided by the users community. A lot of effort has been made by people around the world to provide maps which follow OSM specifications, and more content is continuously being added or updated. The success of Open Street Map has promoted the creation of several open source software projects aiming to develop alternatives to the original mapping on Android, based on Google online services (initiatives as Osmand, an open source application which offers navigation and routing functionality).

In this small series of articles we will talk about implementing maps visualization using Osmdroid API, a set of classes which substitutes (with further features) Android's standard MapView and its support classes, making possible to use a variety of map sources (including Google Maps), connected or disconnected. In our next article we will show how to generate a ZIP file containing a set of raster (PNG) maps, which we will use as a disconnected data source to our application. See you soon!