iOS 8 Europe/Moscow Time Zone Issue - ios

This strange issue appeared with ios 8 release. Here is sample code:
NSDate * date = [NSDate dateWithTimeIntervalSince1970:1414785600];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Moscow"]];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:#"MM"];
NSString * month = [dateFormatter stringFromDate: date];
Date is 2014-11-01 00:00:00 MSK (or 2014-10-31 20:00:00 +0000)
Running ios 7, month value is 11. But on ios 8 it is 10.
Any ideas what's wrong?
Thanks.
PS. Checking Asia/Muscat timezone right now (+4 like MSK). Everything is OK, month is 11.

[timezone secondsFromGMTForDate:date] returns 14400 at iOs7 and 10800 at iOs8 for the given date. It should reflect the changes done (again) by the russian government http://www.timeanddate.com/time/change/russia/moscow which iOs7 isn't aware of yet.

Related

Issue with Caracas time zone in iOS

I am testing different time zones UTC offsets in application. And finally this code is properly working almost with all timezones. But i have an issue with Caracas.
Code that shows UTC offset.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
//This NSDateFormatter will return timezone in format "UTC+XX:XX"
[dateFormatter setDateFormat:#"'UTC'xxxxx"];
NSString *formattedTimeZone = [dateFormatter stringFromDate:[NSDate date]];
return formattedTimeZone;
In Ukraine i receive UTC+03:00 and it is correct. In Caracas i receive UTC-04:00 but real offset is UTC-04:30.
Question is why i am missing -30 minutes in Caracas?
This is not a programming problem, Caracas(Venezuela) timezone has changed recently.
UTC-04:00 is correct right now.
Presidents of Venezuela had changed this a couple of times:
UTC-04:30 was used since 2007.
It was recently changed again to UTC-04:00.
http://www.bloomberg.com/news/articles/2016-04-14/maduro-orders-time-zone-change-to-battle-venezuela-power-crisis

iOS how to parse "MM/DD/YY HH:MM AM" using NSDateFormatter on a device with 24 hour clock? [duplicate]

I'm having a problem. I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when it's in 24 Hour format, things go wrong. Here's some sample code to demonstrate:
NSString *theTime = #"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];
In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG
In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT
So... question 1: why is the device's 24 hour setting overriding my date formatter setting?
and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?
I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.
Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.
The reason for this behaviour is Locale, set the correct Locale
NSString *strAgendaDate = #"01/17/2012 12:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setDateFormat:AgendaDateFormatForMeeting];
NSDate *meetingDate = [dateFormatter dateFromString:aStrDate];
[dateFormatter setDateFormat:AgendaDateRepresentation];
strAgendaDate = [dateFormatter stringFromDate:meetingDate];
It works for both 24-hour and 12 hour format
I believe the #"h:mm a" should be #"HH:mm a".
If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties.
Using those will make sure you respect the settings the user has selected for dates and times.
The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.
I changed from #"hh:mm:ss" to #"HH:mm:ss" and time style was changed from "1:03 PM" to "13:03".
Hope this will help you.
Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:
Thanks. I gave it a whirl with this code:
NSString *theTime = #"3:19 PM";
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDate *date = [timeFormatter dateFromString:theTime];
NSString *theString = [timeFormatter stringFromDate:date];
And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.

NSDate different in simulator and device(ipad)

Hi I am converting a GMT time to local time in my project - I getting the correct value in all simulator and in iPhone/iPod - but when I run this in iPad I am getting null,
Here is my code -
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *startDate = [dateFormatter dateFromString:#"14:37:00"];
NSLog(#"startDate---%#",startDate);
//This is startDate---2000-01-01 14:37:00 +0000 (in simulator/iPod/iphone)
// startDate---(null) (in ipad)
Issue is with user's ipad's time format - if it is in 12 hour format - then the above code results null
What do I do for this issue ??
bacause you time format is set to 12 hours on ipad device, the date string is in 24 hours format look at string #"14:37:00".
use
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Reference
if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.
Use hh instead of HH.
hh usually is 12 hour time while HH is usually 24 hour time.
A good official reference for this, as linked by Apple themselves, is here. Another good table is here, as mentioned by Zaph.

NSDateFormatter return different results depending on iOS version

I have a problem related to date formatter, it is returning null for the date "2012-10-21".
I created a sample project only with the code that I want to test, just to be sure that other things are not interfering in the results.
In iOS 5 it is executing as expected, but in iOS 6 the date formatter is returning null.
This is my code:
NSString *myStringDate = #"2012-10-21";
//------------------------//
NSLog(#"Test 1");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *myDate = [formatter dateFromString:myStringDate];
NSLog(#"Date: %#", myDate);
//------------------------//
NSLog(#"Test 2");
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"yyyy-MM-dd"];
[formatter2 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate2 = [formatter2 dateFromString:myStringDate];
NSLog(#"TimeZone: %#", [NSTimeZone systemTimeZone]);
NSLog(#"Date: %#", myDate2);
//------------------------//
NSLog(#"Test 3");
NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];
[formatter3 setDateFormat:#"yyyy-MM-dd"];
[formatter3 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-7200]];
NSDate *myDate3 = [formatter3 dateFromString:myStringDate];
NSLog(#"TimeZone: %#", [NSTimeZone timeZoneForSecondsFromGMT:-7200]);
NSLog(#"Date: %#", myDate3);
//------------------------//
iOS 5.1 simulator results:
Test 1
Date: 2012-10-21 03:00:00 +0000
Test 2
TimeZone: America/Sao_Paulo (BRST) offset -7200 (Daylight)
Date: 2012-10-21 03:00:00 +0000
Test 3
TimeZone: GMT-0200 (GMT-02:00) offset -7200
Date: 2012-10-21 02:00:00 +0000
iOS 6.0 simulator results:
Test 1
Date: (null)
Test 2
TimeZone: America/Sao_Paulo (GMT-03:00) offset -7200 (Daylight)
Date: (null)
Test 3
TimeZone: GMT-0200 (GMT-02:00) offset -7200
Date: 2012-10-21 02:00:00 +0000
NSDateFormatter returns null in iOS 6 only when the date is "2012-10-21". For all others dates of 2012 year, the result its correct on both iOS versions.
Anyone can explain why? Its a bug? My code is wrong?
Looks like Sau Paulo had a Daylight Saving change on the 20th/21st of October going forward an hour (http://www.timeanddate.com/worldclock/clockchange.html?n=233). This doesn't seem like a coincidence. It's possible that the time calculated for Tests 1 and 2 fell between 12am-1am on the 21st October, which isn't possible. iOS 6 may be a bit more strict in this regard.
As far as I can tell, your code looks fine, it may be a bug within iOS (or it could be intended behavior, and iOS 5 was the issue, I can't tell!). The fact that other dates seemed to work suggests an OS issue, the documentation doesn't explain whether this should happen.
This is the code I am using to decrement the date:
(IBAction)showPrevDate:(id)sender
{
NSString *dateForDecrement = _showDateLbl.text;
[dateFormatter setDateFormat:#"MMM d, yyyy (EEE)"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateObjectForDecrement = [dateFormatter dateFromString:dateForDecrement];
int subtractDays = 1;
NSDate *dateAfterDecrement=[dateObjectForDecrement dateByAddingTimeInterval:-(24*60*60 * subtractDays)];
[dateFormatter setDateFormat:#"MMM d, yyyy (EEE)"];
_showDateLbl.text = [NSString stringWithFormat:#"%#", [dateFormatter stringFromDate:dateAfterDecrement]];
}
Doesn't work with iOS6 but works with iOS5. Can you verify?

NSDateFormatter dateFromString behaviour changed in iOS6

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"h:mm a"];
return [dateFormat dateFromString:text];
where text is 9:16 AM used to return a date starting from 1970 in iOS6 its returning a date like this
2000-01-01 09:16:00 +0000
is this a bug? anyway to switch to old vehaviour
on solution is to simply add "1970" to time text and then add "yyyy" to format but that seems kludgy
They've definitely changed something in iOS 6 with the date parsing. I already submitted a bug to Apple about it. There's noting in the release notes about any NSDateFormatter changes. Also see my similar bug around this parsing I added today.

Resources