Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2011, 05:25 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 32
Trent McDougall is on a distinguished road
Default Time Zone conversion troubles: a day off

Hi,

I know if you google about this there are a lot of results that come up but unfortunately nothing seems to be working for me.

I am trying to make a simple app that among other things converts from one time zone (gained from a UIDatePicker) to another but it in some instances it returns a date that is a day off.

I tried doing things this way:
Code:
 
viewDidLoad {
datePicker.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
[super viewDidLoad];
}
- (IBAction)datePickerValueDidChange:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];   

NSLog(@"%@",[dateFormatter stringFromDate:datePicker.date]);
}
and this way:
Code:
 
- (IBAction)datePickerValueDidChange:(id)sender {
NSTimeZone* sourceTimeZone = datePicker.timeZone;
        
        NSTimeZone* destinationTimeZone = NSTimeZone timeZoneWithName:@"Australia/Sydney"];
        
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datePicker.date];
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datePicker.date];
        NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
        
        NSDate* date = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datePicker.date] autorelease];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];  

        NSLog(@"%@",[dateFormatter stringFromDate:date]);
But regardless of which method I use, if I input 7:01 am 7/16/11 (LA time) into the date picker for example it returns the date 12:01 am 7/16/11 (sydney time) when it should actually read 12:01 am 7/17/11 (one day later).

Im not sure if Im just doing something sill or what but has anyone got any ideas where I'm going wrong?
__________________
Check out the Pokedex Plus. The ultimate app for all things Pokemon. Check it out here.
Trent McDougall is offline   Reply With Quote
Old 07-16-2011, 07:21 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Trent McDougall View Post
Hi,

I know if you google about this there are a lot of results that come up but unfortunately nothing seems to be working for me.

I am trying to make a simple app that among other things converts from one time zone (gained from a UIDatePicker) to another but it in some instances it returns a date that is a day off.

I tried doing things this way:
Code:
 
viewDidLoad {
datePicker.timeZone = [NSTimeZone timeZoneWithName:@"America/Los_Angeles"];
[super viewDidLoad];
}
- (IBAction)datePickerValueDidChange:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];   

NSLog(@"%@",[dateFormatter stringFromDate:datePicker.date]);
}
and this way:
Code:
 
- (IBAction)datePickerValueDidChange:(id)sender {
NSTimeZone* sourceTimeZone = datePicker.timeZone;
        
        NSTimeZone* destinationTimeZone = NSTimeZone timeZoneWithName:@"Australia/Sydney"];
        
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:datePicker.date];
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:datePicker.date];
        NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
        
        NSDate* date = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:datePicker.date] autorelease];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"];  

        NSLog(@"%@",[dateFormatter stringFromDate:date]);
But regardless of which method I use, if I input 7:01 am 7/16/11 (LA time) into the date picker for example it returns the date 12:01 am 7/16/11 (sydney time) when it should actually read 12:01 am 7/17/11 (one day later).

Im not sure if Im just doing something sill or what but has anyone got any ideas where I'm going wrong?
I'm not an expert on this stuff, but your code looks reasonable. I wonder if there's a bug in the system date conversion code?

P.S. I didn't realize there was such a huge time difference between LA and Sidney. Time differences of greater than 12 hours seem counter-intuitive.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 402
16 members and 386 guests
blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny, toon4413
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0