NSDateFormatter problems between versions 10.4 and 10.5 - objective-c

I'm creating an app that needs to run on 10.4 and above. Having a date string that is formatted properly is crucial. When I run my program in 10.5 debug everything is good; I get a date which is exactly how I want it:
Jul 13, 2010 16
the 16 being the hour without the minutes, which I specifically need. Then when I run it in 10.4 I get this:
07/13/10
My code to produce this date looks like this:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH"];
NSDate *newNow = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:newNow];
After reading some relevant documentation (link below), it seems like 10.4 has a unique way to to format that is different then 10.3, 10.5 and 10.6. To quote the link:
"The format string uses the format patterns from the Unicode standard (this reference is to version tr35-6 for Mac OS X v10.5; for Mac OS X v10.4 use version tr35-4)."
I've gone through the documentation and it seems like my Unicode format is okay, but I'm still getting this odd result.
Any help?
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

I think you need to set the formatter behavior to NSDateFormatterBehavior10_4 using +[NSDateFormatter setDefaultFormatterBehavior:] or -[NSDateFormatter setFormatterBehavior:].
EDIT: my reasoning is based on the comment in the NSDateFormatter overview saying:
By default, on Mac OS X v10.4
instances of NSDateFormatter have the
same behavior as they did on Mac OS X
versions 10.0 to 10.3. On Mac OS X
v10.5 and later, NSDateFormatter
defaults to the 10.4+ behavior.

Related

NSNumberFormatter for iOS 13 users ignore setRoundingIncrement

NSLocale *locale = [NSLocale currentLocale];
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setLocale:locale];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setCurrencySymbol:[countriesCurrency objectForKey:currency]];
[currencyStyle setRoundingIncrement:[NSNumber numberWithFloat:0.01]];
Since 2012 code like below returned "0.70 €" and that was great as it's exactly what is expected.
po [currencyStyle stringFromNumber:[NSNumber numberWithFloat:0.70]]
BUT since iOS 13 (app build with xCode 10.3, just users with iOS 13 on their iPhones installed app from the AppStore. Same for build throw xCode 11 on iOS 13.0 and 13.1 beta) exactly same code returns "0,69999998435378074 €"
After quick midnight investigations found that if add
currencyStyle.usesSignificantDigits = YES;
then result will be "0.7 €" - much better but still not exactly what was expected ("0.70 €").
Any ideas how to make setRoundingIncrement great again?
[UPDATE]:
Rewriting the same code to swift solve the problem but this sounds like temp solution in the case all the rest app is in Obj-C.

Date formatting in iOS 9 not working [duplicate]

This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 7 years ago.
I'm have a problem with date formatting in iOS 9, it works ok with iOS 8 and also it works when I'm testing it in simulator with iOS 9, but when a test it on real device with iOS 9 I'm getting null. Below is code that I'm using
NSLog(#"String Date: '%#'", stringDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:stringDate];
NSLog(#"Date: %#", date);
In log I'm getting:
String Date: '8/29/2015 4:13:39 PM'
Date: (null)
Also if I use uppercase h (H or HH) I'm always getting that hour is 10.
Try setting locale on your date formatter.
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
From Apple Documentation:
If you're working with fixed-format dates, you should first set the
locale of the date formatter to something appropriate for your fixed
format. In most cases the best locale to choose is en_US_POSIX, a
locale that's specifically designed to yield US English results
regardless of both user and system preferences.
EDIT: Swift Version
dateFormat.locale = NSLocale(localeIdentifier: "en_US_POSIX")

Where can I find a table of the format string patterns usable by NSDateFormatter?

An NSDateFormatter instance can have it's date and time formats set with:
[formatter setDateFormat: formatString];
Or you can have your format string localised with:
[formatter setDateFormat:
[NSDateFormatter
dateFormatFromTemplate: formatStringWithPatterns
options: 0
locale: [NSLocale currentLocale]];
But where's a handy table of all the codes that can be used in format strings, giving examples of each's output?
I've just pulled this out of Apple's docs because I can never find it when I need it. Hopefully S.O. will be better linked!
For the latest versions of iOS and OS X, the answer is:
Formatters in OS X v10.8+ and iOS 6.0+, use version tr35-25.
For prior versions, you'll need to look more closely:
Formatters in iOS 5.0-5.1 use version tr35-19.
Formatters in OS X v10.7 and iOS 4.3 use version tr35-17.
Formatters in iOS 4.0-4.2 use version tr35-15.
Formatters in iOS 3.2 use version tr35-12.
Formatters in OS X v10.6 and iOS 3.0-3.1 use version tr35-10.

Objective-C - How to Get Date Formatter to Add Colon in Time Zone Offset

I'm trying to format a date to match the format expected on the server side.
Wanted: 1985-01-24T00:00:00-07:00
Got: 1985-01-24T00:00:00-0700
Using: yyyy-MM-dd'T'HH:mm:ssZZZ
Is there a date format trick I can use to get that colon in there?
Here is my code. _birthdate is the date supplied by the birthdate selector:
NSDate *birthdate = (NSDate *)resultObject;
[_birthdate setNewTitle:[IRDate mmddyyFromNSDate:birthdate]];
//Set server-ready birthdate format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
serverFormattedBirthDate = [formatter stringFromDate:birthdate];
NSLog(#"Birthdate: %#", serverFormattedBirthDate);
According to the Date Format Specifiers documentation, it looks like you'll need 5 Z's. That will get you things like "-08:00".
Aha, I see what you're getting at. If you run the formatting on OS X 10.8, you'll get the string you're expecting. However, if you run the formatting on iOS 5.1, you'll get the extra "GMT" in the string.
I'm guessing that the underlying data has changed in recent versions of the CLDR. In that case, I'm not sure what the correct answer is.

dateFromString works different in iOS 4.3.4 ?

Is there any changes regarding NSDateFormatter in iOS 4.3.4? Because i upgraded my iTouch os to 4.3.4 yesterday and found that some of my code related to NSDateFormatter is stop to work.
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
NSDate *date_ = [outputFormatter dateFromString:#"08:00 AM"];
NSLog(#"Date:%#",date_);
[outputFormatter release];
OUTPUT
iOS 4.3.1 : 1970-01-01 02:30:00 +0000
iOS 4.3.4 : null.
Any ideas????
First of all, your code does NOT work in iOS 4.3.1... If you look at the date, you should notice that it is January 1970, or the common "year zero" in computer programming.
I think you should have put "HH:mm" instead of "h:mm a". The doubled h doesn't mean two digits, it is just some convention made up by ISO. And regarding the "AM-PM" parameter, I think it comes from locale.
Look at the documentation of NSDate for setLocale. Your parameter should be #"en-US" I think.
I hope it helped you.
Regards.