仅限时间的UIDatePicker比当前时间提前2小时显示

时间:2010-12-07 08:35:57

标签: iphone objective-c timezone uidatepicker

我有一个显示小时的按钮 - 当视图加载它时设置为当前时间。 然后,当我加载日期选择器子视图时,它总是设置为提前2小时。 类似的东西: 日期按钮:10:3​​1日期选择器:12:31 将日期选择器中的小时更改为:13:31后,日期按钮变为:11:31。

代码:

-(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;

}

从视图中加载:

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];

2 个答案:

答案 0 :(得分:0)

虽然我没有为iPhone或Objective-C开发,所以我不是100%的语法等等,但在我看来,你在某一点上将你的输入时间转换为GMT(或者UTC)而在另一方面你不是。这可能是因为您所选择的时区(以色列/耶路撒冷的时区)为UTC + 2(+3 IST),因此可能导致2小时的差异。我会尽量不“移动”时间,看看是否有帮助。如果它确实那么你至少发现了问题,我会说。

答案 1 :(得分:0)

解决。 问题出在stringFromTime - [date discription]返回错误的值。