views:

493

answers:

4

Hi

I have an array of NSDates which I build from strings using [NSDate dateFromString] In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?

I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.

Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.

NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.

I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/

Thanks for any help given.

A: 

You should take one standard timezone like UTC/GMT format for all calculation.

Brij
Hi Brij, thanks. That is basically what I have now. The problem arises because my dates are not in the same timezones. So 10:00 is not necessarily 10:00 everywhere.I have 10:00 +0300, 10:00 −0400 etc. etc. and doing a comparison between two dates means I need to have a reference to that NSDates timezoneOffset with me always. Which is pretty error prone.
RickiG
Hi BrijI have been all over the place and it seems that your suggestion is the only way around this. I left a method in an answer below.
RickiG
A: 

According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).

Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.

Laurent Etiemble
This is what I talked about regarding the the NSDate documentation being terrible, dateWithString is deprecated since OS 2.0, but it doesn't say in the documentation. They imply you use a date format that looks like this:"YYYY-MM-DD HH:MM:SS ±HHMM", which is wrong, minutes should be lower case. There is a dateFromString on the NSDateformatter Class, but this will not accept a GMT appendix like ±HHMM.It is really a mess :/
RickiG
Sorry, I have pointed to MacOS reference and not iPhone reference.It seems really hard to get what you want as the only option seems either to convert the GMT offset to a NSTimeZone with "timeZoneForSecondsFromGMT:" or to implement a custom date formatter.
Laurent Etiemble
A: 

As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.

Once you have an NSDate you can make normal calculations and ignore the timezones.

You only need to take care of timezones when reading strings into NSDates and when printing them out.

Felix
Hi Felix, thanks for the input. I parse the dates as string from an xml file. So when doing [NSDateformatter dateFromString] the choice is already taken away from me. A NSDate build like this is always in the local timezone.In my interface I display the time and date in one field and the GMT offset in another. So there I actually want them in their individual time zones.
RickiG
A: 

I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)

+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset     
{

 NSDate *now = [NSDate date];
 NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];

 now = [now addTimeInterval:(localTimeZoneOffset * -1)];

 date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];

 return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
RickiG