time-only UIDatePicker displays 2 hours ahead of the current time
I have a button that displays the hour - when the view loads it set to the current time. Then, when i load the date picker subview it's always set to be 2 hours ahead of it. something like that: date button: 10:31 date picker: 12:31 after changing the hour in the date picker to: 13:31 the date butto开发者_JAVA技巧n changes to be : 11:31.
code:
-(IBAction) timeClicked
{
[[NSBundle mainBundle] loadNibNamed:@"timePicker" owner:self options:nil];
//timeView = [[UIView alloc]initWithNibName:@"timePicker" bundle:nil];
[timeView setFrame:CGRectMake(0, 480, 320, 431)];
NSLog(@"time clicked date: %@", select);
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
//[timePick setTime:destinationDate animated:YES];
timePick.date=destinationDate;
NSLog(@"befoer delegate");
//[timeView setDelegate:self];
[self.view addSubview:timeView];
CGRect frame = timeView.frame;
frame.origin.y = 110;
[UIView beginAnimations:nil context:NULL];
timeView.frame = frame;
[UIView commitAnimations];
NSLog(@"after");
}
-(IBAction) done
{
select = [timePick date];
[UIView beginAnimations:@"RemoveDatePicker" context:NULL];
[UIView setAnimationDidStopSelector:@selector(transitionDidStop:finished:context:)];
CGRect rect = timeView.frame;
rect.origin.y = 460;
timeView.frame = rect;
[UIView commitAnimations];
}
- (void)transitionDidStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{
if ([animationID isEqualToString:@"RemoveDatePicker"]){
[timeView removeFromSuperview];
NSLog(@"RemoveDatePicker");
}
}
-(IBAction) timeChanged
{
select =[timePick date];
[timeButton setTitle:[Path stringFromTime:select] forState:UIControlStateNormal];
//[timeButton setTitle:@"time" forState:UIControlStateNormal];
}
+(NSString *) stringFromTime: (NSDate *) date
{
NSString * stringDate = [date description];
stringDate = [[stringDate substringFromIndex:11] substringToIndex:5];
NSLog(@"[Path]stringTime: %@", stringDate);
return stringDate;
}
from the view did load:
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Jerusalem"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
timeModeChange.selectedSegmentIndex=1;
select=[[NSDate alloc]initWithDate:destinationDate ];
NSLog(@"date %@", select);
[timeButton setTitle:[Path stringFromTime:select] forState:UIControlStateNormal];
While I've not developed for the iPhone or in objective-C, so I'm not 100% on the syntax and such, but it would seem to me that you are at one point translating your input time to GMT (or UTC) and at the other you're not. This could account for the 2 hour discrepancy since your chosen timezone, that of Israel/Jerusalem, is UTC+2 (+3 IST). I would try not to "move" the time and see if that helps. If it does then you have atleast found the problem, I'd say.
Solved. The problem was with the stringFromTime - [date discription] returns wrong value.
精彩评论