View Full Version : Changing TimeZones??

Mar 21, 2012, 06:24 AM
Hi to All,

I am in the process of developping a Logbook app using Cocoa in XCode.
The application has a button where you can enter the time of a record in GMT or Local Time.

I have the part working where I can enter the time into the database as GMT but when I click the button to convert back to local time, the record still enters the database as a GMT timestamp.

My code is:

- (IBAction)changeTimeZine:(id)sender {
if ([buttonTimeZine state] == NSOffState)
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

[buttonTimeZine setTitle:@"UTC"];
[qsoDate setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[qsoTime setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[qsoDate setDateValue:[NSDate date]];
[qsoTime setDateValue:[NSDate date]];
[NSTimeZone setDefaultTimeZone:[NSTimeZone systemTimeZone]];

[buttonTimeZine setTitle:@"LOCAL"];
[qsoDate setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Brisbane"]];
[qsoTime setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Brisbane"]];
[qsoDate setDateValue:[NSDate date]];
[qsoTime setDateValue:[NSDate date]];

So, where am I going wrong with this??


Mar 21, 2012, 08:23 AM
Not sure exactly what you are trying to do, but all time in a computer is stored as GMT. When you ask an API to give you local time, it would reference the 'TimeZone' settings and convert GMT to your time zone. So, unless you do a TimeZone calc on GMT, then you would just get GMT back.

In Windows .NET, there are static properties like DateTime.Now and DateTime.Utc (local and GMT respectively) you can use for that conversion. I'm not sure what is the Cocoa equivalent.

Mar 21, 2012, 10:52 AM
What class are qsoTime and qsoDate? Without knowing what they are, you're basically asking us to tell you why an unidentified class isn't working with timezones.

Why are there two objects? Why are they being assigned separate date and timezone objects? Why not one object, qsoDateTime? Entities should not be multiplied beyond necessity. Especially when doing so pointlessly duplicates things.

Why are you forcing Brisbane instead of using localTimeZone, a class method of NSTimeZone? Or defaultTimeZone, also a class method of NSTimeZone?

Have you looked at any of the sample code associated with NSTimeZone? Any other examples?

Mar 21, 2012, 06:57 PM
I have managed to suss it out finally. After setting the default TimeZone to the area I wanted, I needed to re-initalise the DateFormatter.

qsoDate & qsoTime are both NSDatePicker's.

The reason why I was trying 'Australia/Brisbane' was that I had tried just about every avenue and nothing was doing the trick. I have now gone back and using 'systemTimeZone' to get me back to the local time.

The application is an Amateur Radio Logbook which generally requires two entries, one being date and the other being time, so I decided to create two variables.