您的位置:首页 > 理论基础 > 计算机网络

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: