当前位置:   article > 正文

iOS获取网络时间;_ios gettimeline

ios gettimeline

//获取网络时间

+ (NSString *)getTimeLine

{

    NSString *urlString = @"http://m.baidu.com";

    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString: urlString]];

    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];

    [request setTimeoutInterval: 2];

    [request setHTTPShouldHandleCookies:FALSE];

    [request setHTTPMethod:@"GET"];

    NSHTTPURLResponse *response;

    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    NSString *date = [[response allHeaderFields] objectForKey:@"Date"];

    date = [date substringFromIndex:5];

    date = [date substringToIndex:[date length]-4];

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

    dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

    [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];

    NSDate *netDate = [dMatter dateFromString:date];

    NSTimeZone *zone = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: netDate];

    //当前时间,时分秒

    NSDate *localeDate = [netDate  dateByAddingTimeInterval: interval];

    //再转换成毫秒

    NSTimeInterval logDateInterval= [localeDate timeIntervalSince1970];

    long long logTime= logDateInterval*1000;

    //默认是秒,毫秒  时间戳*1000 ,想取得微秒  时间戳 * 1000 * 1000

     return [NSString stringWithFormat:@"%lld", logTime];

}


//获取本地时间;

+ (NSString *)getTimeLine

{

    return [NSString stringWithFormat:@"%lld", (long long)([[NSDate date]timeIntervalSince1970] * 1000)];

}









声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/808217
推荐阅读
相关标签
  

闽ICP备14008679号