Tag: time

UTCdate和UTC时间计算

我有一个string,我知道将引用UTC时间戳 @"2016-01-20T17:00:00" 现在当它转换为一个实际的NSDate它打印时返回这个: NSLog("%@", [TimeHelper stringToDateInUtc:@"2016-01-20T17:00:00"]); -> 2016-01-20 18:00:00 +0000 与UTCdate相同的东西: NSLog(@"Time in UTC Now: %@", [TimeHelper getNowUtcAsDate]); 我也有这种方法获取UTC的当前时间,并将string转换为UTC: + (NSDate*) stringToDateInUtc:(NSString*)str { NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; return [formatter dateFromString:str]; } + (NSString*) getNowUtc { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; NSDate *now […]

iOS的开机时间可以漂移吗?

我正在使用此代码来确定我的iOS设备上次重新启动的时间: int mib[MIB_SIZE]; size_t size; struct timeval boottime; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; size = sizeof(boottime); if (sysctl(mib, MIB_SIZE, &boottime, &size, NULL, 0) != -1) { return boottime.tv_sec; } return 0; 这一次我看到一些exception。 特别是,我省了很长时间,几个星期后又检查了上面代码返回的值。 我不确定,但我想我正在看到一些漂移。 这对我没有任何意义。 我不转换到NSDate,以防止漂移。 我认为引导时间是由内核启动时logging的,不再计算,只是存储。 但是,iOS可以将启动时间保存为一个NSDate,有任何固有的漂移问题呢?

iOS格式的string分为几秒钟

我收到来自YouTube JSONC API的string,但是持续时间将以2321而不是23:21或2而不是0:02来表示。 我怎么去解决这个问题? JSON C. 编辑: int duration = [videos valueForKey:@"duration"]; int minutes = duration / 60; int seconds = duration % 60; NSString *time = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];

使用NSTimer的秒表不正确地包括暂停时间

这是我的iPhone秒表的代码。 它按预期工作,当点击button时停止并继续。 然而,当我点击“停止”时,计时器不会停止在后台运行,当我点击“开始”恢复时,它会更新时间并跳到当前的位置,而不是从停止的时间恢复。 我怎样才能停止NSTimer ? 是什么导致这种情况发生? @implementation FirstViewController; @synthesize stopWatchLabel; NSDate *startDate; NSTimer *stopWatchTimer; int touchCount; -(void)showActivity { NSDate *currentDate = [NSDate date]; NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"mm:ss.SS"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; NSString *timeString=[dateFormatter stringFromDate:timerDate]; stopWatchLabel.text = timeString; [dateFormatter release]; } – (IBAction)onStartPressed:(id)sender […]