This library contains helper classes to produce correctly formed NSLocale, NSDateFormatter and NSCalendar objects
Objective-C Objective-C++ HTML Ruby C Shell
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ESLocale-DynamicFramework
ESLocale-DynamicFrameworkTests
ESLocale-Framework
ESLocale.xcodeproj
ESLocale
ESLocaleTests-ghunit
ESLocaleTests
TestData
article
doc
.gitignore
.travis.yml
AUTHORS
BuildAppledoc.sh
ESLocale.podspec
ESLocaleFramework.podspec
ESLocaleTests copy-Info.plist
License-AFL.txt
License-BSD.txt
README.md
libXCode5gcovPatch.a

README.md

Build Status Carthage Support Pod Version Pod Platform

This library contains helper classes to produce correctly formed NSLocale, NSDateFormatter and NSCalendar objects. Such objects are required to parse data that comes from web services as current locale may not match the remote service locale. Also it contains custom SQLite functions to perform localizef calendar computations in queries.

Source Code License          : BSD
Supplement materials license : AFL

Documentation

The reference of the API exposed by this library can be found at github pages of this repository.

Distribution

The library supports all ways of distribution :

  • cocoapods
  • static library for iOS
  • static framework for iOS
  • Carthage package manager (since ESLocale v1.2.1)

Parsing Dates From Network

Typically in order to parse a date string one uses something like the code below :

NSDateFormatter* ansi = [ NSDateFormatter new ];
ansi.dateFormat = @"yyyy-MM-dd"; // ANSI date formatter

NSDate* date = [ ansi dateForString: dateStringFromNetwork ];

This approach works only if the locale on the device is the same one as at the back end. However, the locale on the device is subject to change by the user. This means that locale on both the server and the parser must be set to en_US_POSIX.

Proper configuration of NSDateFormatter is not as easy as it seems and requires some attention from the programmer.

NSLocale* posixLocale = [ [ NSLocale alloc ] initWithLocaleIdentifier: @"en_US_POSIX" ];

NSCalendar* gregorianCalendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier: NSGregorianCalendar ];

NSTimeZone* gmtTimezone = [ [ NSTimeZone alloc ] initWithName: @"GMT" ];
[ gregorianCalendar setTimeZone: gmtTimezone  ];
[ gregorianCalendar setLocale: posixLocale ];


NSDateFormatter* ansi = [ NSDateFormatter new ];
ansi.calendar = gregorianCalendar;
ansi.locale = posixLocale;
ansi.timeZone = gmtTimeZone;

// Only now we are ready to go
ansi.dateFormat = @"yyyy-MM-dd"; // ANSI date formatter
NSDate* date = [ ansi dateForString: dateStringFromNetwork ];

With ESLocale library it takes only one line.

NSDateFormatter* ansi = [ ESLocaleFactory ansiDateFormatter ];

// Uses en_US_POSIX locale and ANSI date format
NSDate* date = [ ansi dateForString: dateStringFromNetwork ];

A More General Case

The ESLocaleFactory class automates creation of a date formatter for any localization requirements :

NSCalendar* russianCalendar = [ ESLocaleFactory gregorianCalendarWithLocaleId: @"ru_RU" ];

NSDateFormatter* russianFormatter = [ NSDateFormatter new ];

[ ESLocaleFactory setCalendar: russianCalendar 
             forDateFormatter: russianFormatter ];

russianFormatter.dateFormat = @"dd MM yyyy";
// You are ready to use it

Bitdeli Badge