UIDatePicker for time returns very strange results

367 views Asked by At

I've searched the forum and there are quite a few posts on strange behavior of the UIDatePicker, but none of the solutions solved my problem.

I have a simple UIDatePicker initialized like this:

self.picker = [[UIDatePicker alloc] init];
self.picker.datePickerMode = UIDatePickerModeTime;
self.picker.calendar = [NSCalendar currentCalendar];
self.picker.timeZone = [NSTimeZone localTimeZone];
self.picker.date = self.initialDate;
[self.view addSubview:self.picker];

This is done in the viewDidLoad method of a simple view (no xib, no interface builder, just plain code).

Now I have a button, which retrieves the date and "processes" it.

NSDate* selected = self.picker.date;
NSLog(@"%@", selected);

NSDateComponents* components = [self.picker.calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:selected];
NSLog(@"0001-01-01 %02i:%02i:%02i +0000", components.hour, components.minute, components.second);

[components setSecond:0];
NSLog(@"0001-01-01 %02i:%02i:%02i +0000", components.hour, components.minute, components.second);

selected = [self.picker.calendar dateFromComponents:components];
NSLog(@"%@", selected);

Since the date picker will not allow the user to set seconds, I just want to reset them to zero (because I need to compare dates later).

When I select "16:00" (which is 4:00 pm) The log output is:

2012-05-21 17:43:48.428 Green Thumb[26828:fb03] 0001-01-01 14:55:36 +0000
2012-05-21 17:43:48.428 Green Thumb[26828:fb03] 0001-01-01 16:00:56 +0000
2012-05-21 17:43:48.429 Green Thumb[26828:fb03] 0001-01-01 16:00:00 +0000
2012-05-21 17:43:48.429 Green Thumb[26828:fb03] 0001-01-01 14:54:40 +0000

So the original date taken from the picker is completely off. The time taken from the components (which is created based on the date from the picker) is correct, as is the corrected time. But when I put the components back in a NSDate instance, it all gets messed up again (but differently).

Me and google we are both out of ideas, so I hope we can create some new ones here!

Thanks in advance, LetzFlow

PS: I'm testing this with the iPhone 5.1 Simulator.

EDIT #1:

I've tracked down the actual problem, but I'm stuck again:

I've tracked it down to the following point: I'm storing the dates in the NSUserDefaults object.

NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[NSNumber numberWithFloat:[time timeIntervalSince1970]] forKey:@"notification.time"];

And here I get them out again:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber num = [defaults valueForKey:@"notification.time"];
NSDate* time = [NSDate dateWithTimeIntervalSince1970:[num floatValue]];

Interesting enough: What goes in, is not what comes out! But why? :)

EDIT #2: And last but not least I've solved it myself. Thankfully, because it's the most stupidest mistake ever. NSTimeInterval is not a float but a double. And while up until now I never ran into any problems with using a float saving it in the NSUserDefaults seems to have brought forward the errors.

1

There are 1 answers

0
melsam On

Your NSLog statements seem to have the wrong format (i.e. the "0001-01-01" part). Try this instead:

NSString *output = [NSDateFormatter localizedStringFromDate:selected
                                                  dateStyle:NSDateFormatterShortStyle 
                                                  timeStyle:NSDateFormatterShortStyle];

NSLog(@"%@", output);