Toast
This is Narrato’s new home. We moved into our new space at Wayra today!
Will the rise of wearable computing lead to an increase in life-logging and self-quantifying?
Currently, most of the popular wearable computers on sale are focused on health, fitness, and life-logging.
The Nike FuelBand, Fitbits, and even the Memoto camera are all made to help us collect more data about ourselves and use it to improve our lives in one way or another.
The Pebble Watch is an exception that shows signs of this changing. It’s a watch that’s mainly about the convenience of having important information on your wrist instead of in your pocket.
“Wearable computing devices are projected to explode in popularity over the next year”
ABI research forecasts that 485 million wearable computing devices will be shipping yearly, by 2018. If that was to happen, would interest in life-logging and self-quantifying increase as much too?
Parsing python datetime isoformat using NSDateFormatter REDUX
So since my last post about parsing python datetime.isoformat using NSDateFormatter I’ve discovered a lot more about the intricacies of doing it successfully, and heres what I’ve gleaned:
First: When there is no microseconds in the datetime isoformat() wont print it
Second: if the microseconds aren’t included in the output, the previous code I posted wont parse it (it’ll return a nil date).
The fixed code:
NSString * inputDate = @"2012-07-19T08:31:23Z";
NSDate * parsedDate = nil;
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
parsedDate = [dateFormatter dateFromString:inputDate];
if (parsedDate==nil) {
NSDateFormatter *iso8601Formatter = [NSDateFormatter new];
[iso8601Formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
iso8601Formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
iso8601Formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
parsedDate = [iso8601Formatter dateFromString:inputDate];
}
This code will “nicely” fail over and use a non-microsecond parser.
BUT WAIT THERES MORE
If you don’t actually need the microsecond accuracy then theres a “better” way to do this, from the python side. Anywhere you are outputting a datetime do the following:
output_ts = str(dt.replace(microsecond = 0).isoformat())+"Z"
(where dt is our datetime object).
This has the advantage of making sure that all datetimes are “iso8601 compatible”. To decode it use the following code (taken from the code above)
NSDateFormatter *iso8601Formatter = [NSDateFormatter new];
[iso8601Formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
iso8601Formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
iso8601Formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
parsedDate = [iso8601Formatter dateFromString:inputDate];
** its bad-form to allocate a NSDateFormatter each time, it has a fairly hefty setup overhead, you should allocate it in your instance init.
And in summation
If you need to include microseconds in your output, you would be better off outputting it as a float in the format seconds.microseconds and parsing it using [NSDate dateWithTimeIntervalSince1970:] which supports floating point NSTimeInterval input.
To output a float from a datetime (dt) in Python:
import mktime
sec_ms = mktime(dt.timetuple()) + dt.microsecond/1000000.0
Blindly using sample code you find on the internet without reading up on how the technology works is not a good way to develop applications (a lot of people are guilty of this)
Q:Hello Tony, I am using your Reachability code from GitHub and i have a question. I noticed that sometimes my classes doesnt get deallocated when i call [self startNotifier];. Do i have to ever call [self stopNotifier];? also, do i have to declare the Reachability variable inside the method that is calling it or can i declare it as an ivar? Thanks, Newton
You shouldn’t have to subclass Reachability, you can simply create it as an iVar in (for example) your App Delegate.
If you use the block based callbacks the Reachability object that is calling the block is passed in as a parameter, if you use the NSNotification system it is passed as the ‘object’ of the Notification.
When you call startNotifier, the Reachability object retains itself (this is by design) so it can dispatch Reachability change notifications, calling stopNotifier will cause the object to dealloc, unless you have a strong reference to it.
Check the sample code for more information on how to correctly use Reachability.
How to parse python datetime isoformat using NSDateFormatter
I looked around for a while and didn’t find anything that worked properly and included the millisecond param so here it is in all its glory:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSS"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDate * parsedDate = [dateFormatter dateFromString:@"2012-07-19T08:31:23.492123"];
NSLog(@"parsedDate: %@, %f", parsedDate, [parsedDate timeIntervalSince1970]);
Edit: to add this to RestKit use:
// Set it Globally
[RKObjectMapping addDefaultDateFormatter:dateFormatter];
[RKObjectMapping setPreferredDateFormatter:dateFormatter];
If we can do this with Lemons then we can make Cave Johnson’s dream a reality!

