setHour函数更改函数的时区

时间:2014-08-27 03:04:15

标签: ios objective-c datetime timezone nsdate

所以我试图获取当前的日期和时间,然后尝试覆盖小时和分钟,以便设置开火时间。但每当我使用[comp sethour]功能时,它会因某种原因将其恢复为GMT时序。

在头文件中我有:

static int re_hour = 19;
static int re_minute = 52;

并在.m文件中我有这个功能

- (void) ScheduleTimer {

    NSCalendar* myCalendar = [NSCalendar currentCalendar];
    //Get The GMT Time
    NSDate* now = [NSDate date];

    //Get The Current Timezone and then convert the Time
    NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
    NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:now];
    NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:now];
    NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
    NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:now];

   //Setting the restart Time
   NSDateComponents *comp = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:nowDate];

   [comp setHour:re_hour];
   [comp setMinute:re_minute];

   NSDate *restartTime = [myCalendar dateFromComponents:comp];
   NSLog(@"The time of restart :%@",restartTime);
}

似乎总是回归:

 2014-08-26 19:52:34.032 SASMobile[2605:60b] The time of restart :2014-08-27 02:52:34 +0000

0 个答案:

没有答案