iOS开发实战 - 使用网络时间实现租赁周期、送还货时间选择
使用网络时间进行租赁周期选择、送还货时间选择,避免当前时间和服务器时间不同步/一些用户恶搞,修改本地时间,造成无效订单,业务看起来不复杂,但处理起来还是稍微有点麻烦:
1.除了时间真实性和及时性的问题,还要注意上方加减周期和下方换货时间的联动问题,送货时间和换货时间切换后再改变上方加减周期的联动问题;
2.下面时间的获取、判定、比较代码只是其中的一部分
一起来看一下效果:
项目实战
技术点:
1.获取网络时间/服务器时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//获取网络时间 + ( void )getNetWorkDateSuccess:( void (^)(NSDate *networkDate, NSString *networkDateStr))success {
NSString *urlString = @ "https://www.baidu.com" ;
urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
[request setTimeoutInterval: 5 ];
[request setHTTPShouldHandleCookies:FALSE];
[request setHTTPMethod:@ "GET" ];
__block NSDate *localDate;
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (!connectionError) {
//要把网络数据强转 不然用不了下面那个方法获取不到内容(个人感觉,不知道对不)
NSHTTPURLResponse *responsee = (NSHTTPURLResponse *)response;
NSString *date = [[responsee 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];
NSLog(@ "interval: %ld" , interval);
localDate = [netDate dateByAddingTimeInterval: interval];
NSLog(@ "%@________1" ,localDate);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "YYYY-MM-dd HH:mm:ss" ];
NSString *nowtimeStr = [NSString string];
nowtimeStr = [formatter stringFromDate:localDate];
NSLog(@ "%@--------2" ,nowtimeStr);
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
success(localDate, nowtimeStr);
}
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showError:@ "网络异常, 请稍后重试" ];
});
}
}];
} |
2.时间计算-增/减日期
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
//根据时间间隔计算新的时间 年-月-日 字符串格式(年-月-日) + (NSString *)updateDateWithDateStr:(NSString *)dateStr Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd { NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "yyyy-MM-dd" ];
NSTimeInterval fromTime = [[formatter dateFromString:dateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
} else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@ "yyyy-MM-dd" ];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
} //根据时间间隔计算新的时间 年-月-日 + (NSString *)updateDateWithDate:(NSDate *)date Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd { NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "yyyy-MM-dd" ];
NSString *fromDateStr = [formatter stringFromDate:date];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
} else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@ "yyyy-MM-dd" ];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
} //根据时间间隔计算新的时间 年-月-日 时:分:秒 + (NSString *)updatePreciseDateWithDate:(NSDate *)date Interval:(NSTimeInterval)interval IsAdd:(BOOL)isAdd { NSTimeInterval endTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "yyyy-MM-dd HH:mm:ss" ];
NSString *fromDateStr = [formatter stringFromDate:date];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
if (isAdd) {
endTime = fromTime + interval;
} else {
endTime = fromTime - interval;
}
//时间戳转时间
NSDate *endDate = [NSDate dateWithTimeIntervalSince1970:endTime];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@ "yyyy-MM-dd HH:mm:ss" ];
NSString *endDateStr = [dateformatter stringFromDate:endDate];
return endDateStr;
} |
3.时间间隔判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#pragma mark - 判断当前时间是否在某一时间段 /** @param fromDate 开始时间 @“10:00”
@param toDate 结束时间 @“15:00”
@return 判断结果
*/
- (BOOL)isOpenTimeWithFromDate:(NSString *)fromDate toDate:(NSString *)toDate { //获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "yyyy-MM-dd" ];
NSString *dateStr = [formatter stringFromDate:_networkDate];
//开始时间和结束时间
NSString *fromDateStr = [NSString stringWithFormat:@ "%@ %@" , dateStr, fromDate];
NSString *endDateStr = [NSString stringWithFormat:@ "%@ %@" , dateStr, toDate];
[formatter setDateFormat:@ "yyyy-MM-dd HH:mm" ];
NSTimeInterval fromTime = [[formatter dateFromString:fromDateStr] timeIntervalSince1970];
NSTimeInterval endTime = [[formatter dateFromString:endDateStr] timeIntervalSince1970];
NSTimeInterval currentTime = [_networkDate timeIntervalSince1970];
if (currentTime > fromTime && currentTime < endTime) {
return YES;
} else {
return NO;
}
} #pragma mark - 判断当前时间是否处于某一时间之前 /** 判断当前时间是否处于某一时间之前
@param currentDate 当前时间
@param toDate 比较时间
@return 比较结果
*/
- (BOOL)isGreaterThanWithCurrentDate:(NSDate *)currentDate ToDate:(NSString *)toDate { //获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@ "yyyy-MM-dd" ];
NSString *dateStr = [formatter stringFromDate:currentDate];
//开始时间和结束时间
NSString *toDateStr = [NSString stringWithFormat:@ "%@ %@" , dateStr, toDate];
[formatter setDateFormat:@ "yyyy-MM-dd HH:mm" ];
NSTimeInterval toTime = [[formatter dateFromString:toDateStr] timeIntervalSince1970];
NSTimeInterval currentTime = [currentDate timeIntervalSince1970];
if (currentTime > toTime) {
return YES;
} else {
return NO;
}
}
|