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;
}
网友评论