【问题标题】:How can I get the Correct Current Time in iOS?如何在 iOS 中获取正确的当前时间?
【发布时间】:2016-05-30 12:44:42
【问题描述】:

我知道这是重复的问题。我查看了很多答案,但仍然没有得到任何解决方案。

我的问题是, 如果用户手动设置了设备时间,如何获取设备时间..?

我已经实现了一个聊天应用程序。但是在这个应用程序中,我遇到了时间问题。如果用户手动设置了时间,那么我们如何识别呢?

我想获得正确的当前 UTC 时间。因此,使用它,我可以识别设备时间和 UTC 时间之间的差异。

如果我们使用NSDate *currentDateAndTime = [NSDate date];,那么它只根据设备返回UTC时间,而不是当前的UTC时间。

有什么简单的方法可以找到这个解决方案吗?

任何帮助将不胜感激。

【问题讨论】:

标签: ios objective-c swift time nstimezone


【解决方案1】:

iOS 中没有可信的时间源。您只需使用单调和非单调时间进行操作。

单调时间(或绝对时间)表示自过去某个任意固定点以来经过的绝对挂钟时间。 CACurrentMediaTime()mach_absolute_time 返回单调时间。

非单调时间代表机器对当前挂钟时间的最佳猜测。随着系统时钟的改变,它可以向前和向后跳跃。 [NSDate date] 调用返回它。

作为一个选项,您可以从 http Date 响应标头中获取受信任的日期/时间。然后将它和当前单调时间(CACurrentMediaTime())保存在钥匙串中。 以下是获取当前可信时间的方法:

last known online time + (CACurrentMediaTime() - saved monotonic time)

另一种解决方案是使用 NTP 服务器。

【讨论】:

    【解决方案2】:

    这可能会解决您的问题

    NSDate * date = [NSDate date];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
            NSString *currentTime = [dateFormatter stringFromDate:date];
            NSLog(@"current time is:%@",currentTime);
    

    【讨论】:

      【解决方案3】:

      使用此代码对您有帮助

      NSDate * datee = [NSDate date];
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
      NSString *localDateString = [dateFormatter stringFromDate:currentDate];
      
      NSLog(@"%@",localDateString);
      

      【讨论】:

      • 我想得到正确的当前时间。它将返回设备的当前时间,而不是当前的 UTC 时间。
      【解决方案4】:

      使用:NSDate *currentDateAndTime = [NSDate date];

      这将为您提供 GMT 参考区的绝对日期和时间。

      【讨论】:

      • 如果您在不使用任何时区的情况下手动设置时间,则它不起作用。它将以 UTC 时间返回设备时间。
      • 设备始终使用 UTC 时间工作。只有时间的显示使用 timeZone 精度,但仅用于显示。
      【解决方案5】:

      试试这个:

      迅速:

      let date = NSDate()
      let dateFormatter = NSDateFormatter()
      let timeZone = NSTimeZone(name: "UTC")
      
      dateFormatter.timeZone = timeZone
      
      println(dateFormatter.stringFromDate(date))
      

      对象-C:

      NSDate *date = [NSDate date];
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
      
      [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
      [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
      [dateFormatter setTimeZone:timeZone];       
      NSLog(@"%@", [dateFormatter stringFromDate:date]);
      

      【讨论】:

        【解决方案6】:

        您可以使用此代码获取当前时间:-

        斯威夫特 3

        let date = Date()
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone(abbreviation: "GMT+0:00") //Current time zone
        formatter.dateFormat = "HH:MM a"
        let currentTime: String = formatter.stringFromDate(date)
        print("My Current Time is \(currentTime)")
        
        Result **
        Output : - "My Current Time is 11:05 AM\n"
        

        【讨论】:

          猜你喜欢
          • 2014-07-06
          • 1970-01-01
          • 2012-12-29
          • 1970-01-01
          • 2014-11-08
          • 1970-01-01
          • 2013-07-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多