考虑到Cocoa-Touch(iPhone上的Objective-C)没有NSTime,并且NSStrings和NSZtring的时区分别是两次,你如何计算当前LOCAL时间是否在这两次之间。请记住,时间字符串中的日期并不重要,并且填充了虚拟日期。
例如:
TimeZone: Pacific Time (US & Canada)
Start Time: 2000-01-01T10:00:00Z
End Time: 2000-01-01T17:00:00Z
Local Time: now
如何确认当地时间是否在指定的时间范围之间(确保首先将开始/结束时间转换为正确的时区)?
答案 0 :(得分:5)
这个问题的最大问题似乎来自于时间可能跨越两天(原来他们可能没有,但是当你进行时区转换时,他们可能会这样做)。因此,如果我们完全忽略给定的日期信息,则必须对如何处理这些日期跨度做出一些假设。你的问题并不完全是关于如何处理这个问题(即我不知道你想要实现的目标)所以这里只是一种方法,这可能不是你想要的,但是希望它能引导你朝着正确的方向前进:
NSDate
个对象,忽略日期信息(结果:处理时间使得它们被假定为同一天)并执行时区转换NSDate
到后期NSDate
NSDate
对象和“昨天在给定时间” NSDate
到当前日期/时间的时间间隔到两个给定日期/时间之间的时间间隔另请注意,"Pacific Time (US & Canada)"
无法理解您提供的格式(NSTimeZone
)的时区字符串,因此您需要在那里进行一些转换。
这是一个代码示例(我在OS X上写了这个,因为我没有iPhone SDK所以希望所有使用过的API也可以在iPhone上使用):
- (BOOL)checkTimes
{
// won't work:
//NSString *tzs = @"Pacific Time (US & Canada)";
//
// will work (need to translate given timezone information
// to abbreviations accepted by NSTimeZone -- won't cover
// that here):
NSString *tzs = @"PST";
NSString *ds1 = @"2000-01-01T10:00:00Z";
NSString *ds2 = @"2000-01-01T17:00:00Z";
// remove dates from given strings (requirement was to ignore
// the dates completely)
ds1 = [ds1 substringFromIndex:11];
ds2 = [ds2 substringFromIndex:11];
// remove the UTC time zone designator from the end (don't know
// what it's doing there since the time zone is given as a
// separate field but I'll assume for the sake of this example
// that the time zone designator for the given dates will
// always be 'Z' and we'll always ignore it)
ds1 = [ds1 substringToIndex:8];
ds2 = [ds2 substringToIndex:8];
// parse given dates into NSDate objects
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:tzs]];
NSDate *date1 = [df dateFromString:ds1];
NSDate *date2 = [df dateFromString:ds2];
// get time interval from earlier to later given date
NSDate *earlierDate = date1;
NSTimeInterval ti = [date2 timeIntervalSinceDate:date1];
if (ti < 0)
{
earlierDate = date2;
ti = [date1 timeIntervalSinceDate:date2];
}
// get current date/time
NSDate *now = [NSDate date];
// create an NSDate for today at the earlier given time
NSDateComponents *todayDateComps = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:now];
NSDateComponents *earlierTimeComps = [[NSCalendar currentCalendar]
components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
fromDate:earlierDate];
NSDateComponents *todayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
[todayEarlierTimeComps setYear:[todayDateComps year]];
[todayEarlierTimeComps setMonth:[todayDateComps month]];
[todayEarlierTimeComps setDay:[todayDateComps day]];
[todayEarlierTimeComps setHour:[earlierTimeComps hour]];
[todayEarlierTimeComps setMinute:[earlierTimeComps minute]];
[todayEarlierTimeComps setSecond:[earlierTimeComps second]];
NSDate *todayEarlierTime = [[NSCalendar currentCalendar]
dateFromComponents:todayEarlierTimeComps];
// create an NSDate for yesterday at the earlier given time
NSDateComponents *minusOneDayComps = [[[NSDateComponents alloc] init] autorelease];
[minusOneDayComps setDay:-1];
NSDate *yesterday = [[NSCalendar currentCalendar]
dateByAddingComponents:minusOneDayComps
toDate:now
options:0];
NSDateComponents *yesterdayDateComps = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:yesterday];
NSDateComponents *yesterdayEarlierTimeComps = [[[NSDateComponents alloc] init] autorelease];
[yesterdayEarlierTimeComps setYear:[yesterdayDateComps year]];
[yesterdayEarlierTimeComps setMonth:[yesterdayDateComps month]];
[yesterdayEarlierTimeComps setDay:[yesterdayDateComps day]];
[yesterdayEarlierTimeComps setHour:[earlierTimeComps hour]];
[yesterdayEarlierTimeComps setMinute:[earlierTimeComps minute]];
[yesterdayEarlierTimeComps setSecond:[earlierTimeComps second]];
NSDate *yesterdayEarlierTime = [[NSCalendar currentCalendar]
dateFromComponents:yesterdayEarlierTimeComps];
// check time interval from [today at the earlier given time] to [now]
NSTimeInterval ti_todayEarlierTimeTillNow = [now timeIntervalSinceDate:todayEarlierTime];
if (0 <= ti_todayEarlierTimeTillNow && ti_todayEarlierTimeTillNow <= ti)
return YES;
// check time interval from [yesterday at the earlier given time] to [now]
NSTimeInterval ti_yesterdayEarlierTimeTillNow = [now timeIntervalSinceDate:yesterdayEarlierTime];
if (0 <= ti_yesterdayEarlierTimeTillNow && ti_yesterdayEarlierTimeTillNow <= ti)
return YES;
return NO;
}
答案 1 :(得分:0)
您应该查看NSCalendar
和NSDateComponents
。我正在使用它们为iPhone应用程序做一些日期数学运算。我没有看到内置方法直接做你想要的东西,但是你可以把它分成我期望的碎片来解决它?
答案 2 :(得分:0)
您应该能够使用NSDate
和NSDateFormatter
的组合来完成您想要做的事情。
NSDateFormatter
有dateFromString:
方法可让您将字符串转换为NSDate
个对象,然后您可以使用compare:
NSDate
方法进行比较两个针对[NSDate date]
的当前时间。
(NSDate
包含日期和时间)