NTP协议网络时间同步 ios中应用 实现秒杀倒计时
2014-07-08 23:39
597 查看
项目中需要实现秒杀倒计时功能,手机客户端本地系统时间有被修改的可能,因此无法获取本地时间来做,下面看实现代码吧
-(void)startTimerShow
{
self.ysmagTimer = [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFireMethod)
userInfo:nil
repeats:YES];
}
- (void)timerFireMethod
{
//当天
NSDate *todayNet = [NSDate
networkDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc]
init];
[dateFormatter
setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone
timeZoneWithName:@"CCD"]];
NSString *destDateString = [dateFormatter
stringFromDate:todayNet];
NSTimeInterval interVal =
24*60*60;
NSDate *tomorrowNet = [todayNet
dateByAddingTimeInterval:interVal];
NSString *destDateStringqq = [dateFormatter
stringFromDate:tomorrowNet];
NSString *qgksTime = [NSString
stringWithFormat:@"%@-%@-%@ 10:00:00",[destDateStringqq
substringWithRange:NSMakeRange(0,
4)],[destDateStringqq
substringWithRange:NSMakeRange(5,
2)],[destDateStringqq
substringWithRange:NSMakeRange(8,
2)]];
NSDate *today = [dateFormatter
dateFromString:qgksTime];
NSLog(@"难道就这样成功了吗 %@",today);
- (NSString *)intervalFromLastDate: (NSDate *) d1 toTheDate:(NSDate *)
d2
{
// NSArray *timeArray1=[dateString1 componentsSeparatedByString:@"."];
// dateString1=[timeArray1 objectAtIndex:0];
//
//
// NSArray *timeArray2=[dateString2 componentsSeparatedByString:@"."];
// dateString2=[timeArray2 objectAtIndex:0];
//
// NSLog(@"%@.....%@",dateString1,dateString2);
// NSDateFormatter *date=[[NSDateFormatter alloc] init];
// [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSDate *d1=[date dateFromString:dateString1];
NSTimeInterval late1=[d1
timeIntervalSince1970]*1;
// NSDate *d2=[date dateFromString:dateString2];
NSTimeInterval late2=[d2
timeIntervalSince1970]*1;
NSTimeInterval cha=late2-late1;
NSString *timeString=@"";
NSString *house=@"00";
NSString *min=@"00";
NSString *sen=@"00";
sen = [NSString
stringWithFormat:@"%d", (int)cha%60];
// min = [min substringToIndex:min.length-7];
//
秒
sen=[NSString
stringWithFormat:@"%@", sen];
min = [NSString
stringWithFormat:@"%d", (int)cha/60%60];
// min = [min substringToIndex:min.length-7];
//
分
min=[NSString
stringWithFormat:@"%@", min];
//
小时
house = [NSString
stringWithFormat:@"%d", (int)cha/3600];
// house = [house substringToIndex:house.length-7];
house=[NSString
stringWithFormat:@"%@", house];
if ([house intValue]<10) {
house = [NSString
stringWithFormat:@"0%@",house];
}else{
house = [NSString
stringWithFormat:@"%@",house];
}
if ([min intValue]<10) {
min = [NSString
stringWithFormat:@"0%@",min];
}else{
min = [NSString
stringWithFormat:@"%@",min];
}
if ([sen intValue]<10) {
sen = [NSString
stringWithFormat:@"0%@",sen];
}else{
sen = [NSString
stringWithFormat:@"%@",sen];
}
ytLbl_.text = [house
substringWithRange:NSMakeRange(0,
1)];
atLbl_.text = [house
substringWithRange:NSMakeRange(1,
1)];
ntLbl_.text = [min
substringWithRange:NSMakeRange(0,
1)];
gtLbl_.text = [min
substringWithRange:NSMakeRange(1,
1)];
stLbl_.text = [sen
substringWithRange:NSMakeRange(0,
1)];
htLbl_.text = [sen
substringWithRange:NSMakeRange(1,
1)];
timeString=[NSString
stringWithFormat:@"%@:%@:%@",house,min,sen];
// [date release];
return timeString;
}
-(void)startTimerShow
{
self.ysmagTimer = [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(timerFireMethod)
userInfo:nil
repeats:YES];
}
- (void)timerFireMethod
{
//当天
NSDate *todayNet = [NSDate
networkDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter
alloc]
init];
[dateFormatter
setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone
timeZoneWithName:@"CCD"]];
NSString *destDateString = [dateFormatter
stringFromDate:todayNet];
NSTimeInterval interVal =
24*60*60;
NSDate *tomorrowNet = [todayNet
dateByAddingTimeInterval:interVal];
NSString *destDateStringqq = [dateFormatter
stringFromDate:tomorrowNet];
NSString *qgksTime = [NSString
stringWithFormat:@"%@-%@-%@ 10:00:00",[destDateStringqq
substringWithRange:NSMakeRange(0,
4)],[destDateStringqq
substringWithRange:NSMakeRange(5,
2)],[destDateStringqq
substringWithRange:NSMakeRange(8,
2)]];
NSDate *today = [dateFormatter
dateFromString:qgksTime];
NSLog(@"难道就这样成功了吗 %@",today);
- (NSString *)intervalFromLastDate: (NSDate *) d1 toTheDate:(NSDate *)
d2
{
// NSArray *timeArray1=[dateString1 componentsSeparatedByString:@"."];
// dateString1=[timeArray1 objectAtIndex:0];
//
//
// NSArray *timeArray2=[dateString2 componentsSeparatedByString:@"."];
// dateString2=[timeArray2 objectAtIndex:0];
//
// NSLog(@"%@.....%@",dateString1,dateString2);
// NSDateFormatter *date=[[NSDateFormatter alloc] init];
// [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSDate *d1=[date dateFromString:dateString1];
NSTimeInterval late1=[d1
timeIntervalSince1970]*1;
// NSDate *d2=[date dateFromString:dateString2];
NSTimeInterval late2=[d2
timeIntervalSince1970]*1;
NSTimeInterval cha=late2-late1;
NSString *timeString=@"";
NSString *house=@"00";
NSString *min=@"00";
NSString *sen=@"00";
sen = [NSString
stringWithFormat:@"%d", (int)cha%60];
// min = [min substringToIndex:min.length-7];
//
秒
sen=[NSString
stringWithFormat:@"%@", sen];
min = [NSString
stringWithFormat:@"%d", (int)cha/60%60];
// min = [min substringToIndex:min.length-7];
//
分
min=[NSString
stringWithFormat:@"%@", min];
//
小时
house = [NSString
stringWithFormat:@"%d", (int)cha/3600];
// house = [house substringToIndex:house.length-7];
house=[NSString
stringWithFormat:@"%@", house];
if ([house intValue]<10) {
house = [NSString
stringWithFormat:@"0%@",house];
}else{
house = [NSString
stringWithFormat:@"%@",house];
}
if ([min intValue]<10) {
min = [NSString
stringWithFormat:@"0%@",min];
}else{
min = [NSString
stringWithFormat:@"%@",min];
}
if ([sen intValue]<10) {
sen = [NSString
stringWithFormat:@"0%@",sen];
}else{
sen = [NSString
stringWithFormat:@"%@",sen];
}
ytLbl_.text = [house
substringWithRange:NSMakeRange(0,
1)];
atLbl_.text = [house
substringWithRange:NSMakeRange(1,
1)];
ntLbl_.text = [min
substringWithRange:NSMakeRange(0,
1)];
gtLbl_.text = [min
substringWithRange:NSMakeRange(1,
1)];
stLbl_.text = [sen
substringWithRange:NSMakeRange(0,
1)];
htLbl_.text = [sen
substringWithRange:NSMakeRange(1,
1)];
timeString=[NSString
stringWithFormat:@"%@:%@:%@",house,min,sen];
// [date release];
return timeString;
}
相关文章推荐
- Linux之通过NTP协议实现服务器时间同步的源码
- NTP 及其在电信时间同步网络中的应用
- 同步网络时间的 NTP 协议被发现存在8个漏洞
- NTP协议应用(获取网络时间)
- 通过网络NTP协议进行时间同步
- 同步网络时间的 NTP 协议被发现存在8个漏洞
- NTP协议以及ntpclinet使用 From Evernote: NTP协议以及ntpclinet使用 NTP全称是Network Time Protocol,是用来让计算机之间实现时间同步的协议
- 关于RTP时间戳及多媒体通信同步的问题/H264关于RTP协议的实现
- 使用C#实现网络时间同步功能
- 时间同步系统的实现和应用
- 【iOS Clock Synchronization】iOS时钟同步:基于NTP协议的开源项目ios-ntp
- 网络时间协议Network Time Protocol(NTP)
- 移动网络应用开发中,使用 HTTP 协议比起使用 socket 实现基于 TCP 的自定义协议有哪些优势?
- 网络时间协议简介-----NTP(Network Time Protocol)
- 通过NTP协议实现的网络对时程序
- 通过NTP协议进行时间同步
- HP-UX ntpclient 时间同步 NTP HP-UX系统和网络管理
- 转载:基于NTP的高精度时钟同步系统实现_电子技术应用
- 网络时间协议(NTP)的安装
- 模拟NTP协议实现时钟同步