如何计算时间间隔的总和
how to calculate sum of time intervals
请告诉我如何根据 google 的距离矩阵 api 计算时间间隔总和 return - 例如我得到 2 天 15 小时或 5 小时 49 分钟或 41 分钟甲酸。那么我怎样才能总结所有的时间间隔。
请看我的回复-
{
"status": "OK",
"origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ],
"destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
"rows": [ {
"elements": [ {
"status": "OK",
"duration": {
"value": 340110,
"text": "3 jours 22 heures"
},
"distance": {
"value": 1734542,
"text": "1 735 km"
}
}, {
"status": "OK",
"duration": {
"value": 24487,
"text": "6 heures 48 minutes"
},
"distance": {
"value": 129324,
"text": "129 km"
}
} ]
}, {
"elements": [ {
"status": "OK",
"duration": {
"value": 288834,
"text": "3 jours 8 heures"
},
"distance": {
"value": 1489604,
"text": "1 490 km"
}
}, {
"status": "OK",
"duration": {
"value": 14388,
"text": "4 heures 0 minutes"
},
"distance": {
"value": 135822,
"text": "136 km"
}
} ]
} ]
}
创建一个名为 previousTime 的 属性。
@property (nonatomic, strong) NSDate *previousTime;
使用此方法查找时差。
- (NSTimeInterval)timeDifferenceSinceLastOpen
{
if (!previousTime) self.previousTime = [NSDate date];
NSDate *currentTime = [NSDate date];
NSTimeInterval timeDifference = [currentTime timeIntervalSinceDate:prevTime];
self.prevTime = currentTime;
return timeDifference;
}
并做总结。
希望对你有帮助。
根据您发布的数据,服务器似乎以文本格式和秒数提供了时差:
"value": 24487,
"text": "6 heures 48 minutes"
(6 小时 48 分是 24480 秒。)
以秒为单位处理间隔比尝试将时间间隔字符串转换回数字时间间隔要容易得多。只需获取 "value" 键的值并将这些值相加即可。 (这些值似乎是与字符串时间间隔匹配的秒数。)
然后您可以使用 NSDateComponentsFormatter
将时间间隔转换回显示字符串。
您也可以使用 NSDateComponentsFormatter
将您的时间间隔字符串转换为数字时间间隔,但您需要确保您的区域设置正确,然后您必须处理两者之间的差异您服务器的字符串格式和 iOS 的。
在这里你也可以使用简单的函数从Google距离矩阵API.
获取时间计算
您也可以根据需要调整此方法。
- (NSString *)getTotalTimeFromGoogleDistanceAPI:(NSArray *)timeArray
{
double days = 0;
double hours = 0;
double mins = 0;
for (NSString *str in timeArray) {
// Split string into array to get values at index
NSArray *stringSplitterArray = [str componentsSeparatedByString:@" "];
if ([str containsString:@"day"] && [str containsString:@"hour"]) {
days += [[stringSplitterArray objectAtIndex:0] integerValue];
hours += [[stringSplitterArray objectAtIndex:2] integerValue];
}else
if ([str containsString:@"hour"] && [str containsString:@"min"])
{
hours += [[stringSplitterArray objectAtIndex:0] integerValue];
mins += [[stringSplitterArray objectAtIndex:2] integerValue];
}
else
{
mins += [[stringSplitterArray objectAtIndex:0] integerValue];
}
}
// Check for hours -->> if its is greater than 24 then convert it into Day
if (hours > 23) {
double extractDay = floor(hours/24);
days += extractDay;
double extractHours = fmod(hours, 24);
hours = extractHours;
}
// Check for mins -->> if its is greater than 60 then convert it into Hours
if (mins > 59) {
double extractHours = floor(mins/60);
hours += extractHours;
double extractMins = fmod(mins, 60);
mins = extractMins;
}
// Calculate final time
NSString *timeString;
if (days == 0) {
timeString = [NSString stringWithFormat:@"%g hours %g mins", hours , mins];
}else
if (days == 0 && hours == 0){
timeString = [NSString stringWithFormat:@"%g mins", mins];
}else
{
timeString = [NSString stringWithFormat:@"%g days %g hours %g mins", days, round(hours) , round(mins)];
}
return timeString;
}
希望对您有所帮助!!
请告诉我如何根据 google 的距离矩阵 api 计算时间间隔总和 return - 例如我得到 2 天 15 小时或 5 小时 49 分钟或 41 分钟甲酸。那么我怎样才能总结所有的时间间隔。 请看我的回复-
{
"status": "OK",
"origin_addresses": [ "Vancouver, BC, Canada", "Seattle, État de Washington, États-Unis" ],
"destination_addresses": [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
"rows": [ {
"elements": [ {
"status": "OK",
"duration": {
"value": 340110,
"text": "3 jours 22 heures"
},
"distance": {
"value": 1734542,
"text": "1 735 km"
}
}, {
"status": "OK",
"duration": {
"value": 24487,
"text": "6 heures 48 minutes"
},
"distance": {
"value": 129324,
"text": "129 km"
}
} ]
}, {
"elements": [ {
"status": "OK",
"duration": {
"value": 288834,
"text": "3 jours 8 heures"
},
"distance": {
"value": 1489604,
"text": "1 490 km"
}
}, {
"status": "OK",
"duration": {
"value": 14388,
"text": "4 heures 0 minutes"
},
"distance": {
"value": 135822,
"text": "136 km"
}
} ]
} ]
}
创建一个名为 previousTime 的 属性。
@property (nonatomic, strong) NSDate *previousTime;
使用此方法查找时差。
- (NSTimeInterval)timeDifferenceSinceLastOpen
{
if (!previousTime) self.previousTime = [NSDate date];
NSDate *currentTime = [NSDate date];
NSTimeInterval timeDifference = [currentTime timeIntervalSinceDate:prevTime];
self.prevTime = currentTime;
return timeDifference;
}
并做总结。 希望对你有帮助。
根据您发布的数据,服务器似乎以文本格式和秒数提供了时差:
"value": 24487,
"text": "6 heures 48 minutes"
(6 小时 48 分是 24480 秒。)
以秒为单位处理间隔比尝试将时间间隔字符串转换回数字时间间隔要容易得多。只需获取 "value" 键的值并将这些值相加即可。 (这些值似乎是与字符串时间间隔匹配的秒数。)
然后您可以使用 NSDateComponentsFormatter
将时间间隔转换回显示字符串。
您也可以使用 NSDateComponentsFormatter
将您的时间间隔字符串转换为数字时间间隔,但您需要确保您的区域设置正确,然后您必须处理两者之间的差异您服务器的字符串格式和 iOS 的。
在这里你也可以使用简单的函数从Google距离矩阵API.
获取时间计算您也可以根据需要调整此方法。
- (NSString *)getTotalTimeFromGoogleDistanceAPI:(NSArray *)timeArray
{
double days = 0;
double hours = 0;
double mins = 0;
for (NSString *str in timeArray) {
// Split string into array to get values at index
NSArray *stringSplitterArray = [str componentsSeparatedByString:@" "];
if ([str containsString:@"day"] && [str containsString:@"hour"]) {
days += [[stringSplitterArray objectAtIndex:0] integerValue];
hours += [[stringSplitterArray objectAtIndex:2] integerValue];
}else
if ([str containsString:@"hour"] && [str containsString:@"min"])
{
hours += [[stringSplitterArray objectAtIndex:0] integerValue];
mins += [[stringSplitterArray objectAtIndex:2] integerValue];
}
else
{
mins += [[stringSplitterArray objectAtIndex:0] integerValue];
}
}
// Check for hours -->> if its is greater than 24 then convert it into Day
if (hours > 23) {
double extractDay = floor(hours/24);
days += extractDay;
double extractHours = fmod(hours, 24);
hours = extractHours;
}
// Check for mins -->> if its is greater than 60 then convert it into Hours
if (mins > 59) {
double extractHours = floor(mins/60);
hours += extractHours;
double extractMins = fmod(mins, 60);
mins = extractMins;
}
// Calculate final time
NSString *timeString;
if (days == 0) {
timeString = [NSString stringWithFormat:@"%g hours %g mins", hours , mins];
}else
if (days == 0 && hours == 0){
timeString = [NSString stringWithFormat:@"%g mins", mins];
}else
{
timeString = [NSString stringWithFormat:@"%g days %g hours %g mins", days, round(hours) , round(mins)];
}
return timeString;
}
希望对您有所帮助!!