美文网首页
ios 时间显示时 当前时间同步服务器时间

ios 时间显示时 当前时间同步服务器时间

作者: 贪吃的猫cx | 来源:发表于2017-06-12 16:40 被阅读0次

    RCDateTimeUtils.h文件

    #import@interface RCDateTimeUtils : NSObject

    @property(nonatomic,assign)NSTimeInterval timeIntevalDifference;

    +(void)updateServerTime:(NSTimeInterval)timestamp;

    + (NSDate*)currentTime ;

    +(RCDateTimeUtils*)sharedInstance;

    @end

    //RCDateTimeUtils.h文件

    #import "RCDateTimeUtils.h"

    @implementation RCDateTimeUtils

    //创建单例

    +(RCDateTimeUtils*)sharedInstance{

    static RCDateTimeUtils *manager=nil;

    static dispatch_once_t token;

    dispatch_once(&token,^{

    if(manager == nil){

    manager = [[RCDateTimeUtils alloc]init];

    }

    } );

    return manager;

    }

    //计算时间差

    +(void)updateServerTime:(NSTimeInterval)timestamp {

    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

    [outputFormatter setLocale:[NSLocale currentLocale]];

    [outputFormatter setDateFormat:@"YYYYMMddHHmmss"];

    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];

    NSTimeInterval timeInteval =timestamp - interval;

    NSLog(@"_%f",timeInteval);

    [RCDateTimeUtils sharedInstance].timeIntevalDifference = timeInteval;

    }

    //本地时间与服务器时间同步

    + (NSDate*)currentTime {

    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];

    [outputFormatter setLocale:[NSLocale currentLocale]];

    [outputFormatter setDateFormat:@"YYYYMMddHHmmss"];

    NSDate *dateT=[NSDate date];

    NSTimeInterval interval = [dateT timeIntervalSince1970];

    interval+=[self sharedInstance].timeIntevalDifference;

    dateT = [NSDate dateWithTimeIntervalSince1970:interval];

    return dateT;

    }

    相关文章

      网友评论

          本文标题:ios 时间显示时 当前时间同步服务器时间

          本文链接:https://www.haomeiwen.com/subject/jrglqxtx.html