gpt4 book ai didi

iphone - 判断当前本地时间是否在两个时间之间(忽略日期部分)

转载 作者:搜寻专家 更新时间:2023-10-30 19:40:51 26 4
gpt4 key购买 nike

考虑到 Cocoa-Touch(iPhone 上的 Objective-C)中没有 NSTime,并且给定两次作为 NSStrings 和一个时区作为 NSString,你如何计算当前本地时间是否在这两个时间之间.请记住,时间字符串中的日期无关紧要,并且填充了虚拟日期。

例如:

 TimeZone: Pacific Time (US & Canada)
Start Time: 2000-01-01T10:00:00Z
End Time: 2000-01-01T17:00:00Z

Local Time: now

如何确认本地时间是否在指定的时间范围内(确保首先将开始/结束时间转换为正确的时区)?

最佳答案

最大的问题似乎是时间可能跨越两天(最初它们可能不会,但是当您进行时区转换时,之后它们可能)。因此,如果我们要完全忽略给定的日期信息,则必须对如何处理这些日期跨度做出一些假设。你的问题不完全是关于如何处理这个问题(即我不知道你想要达到什么目的)所以这只是一种解决方法,这可能不是你想要的,但是希望它将引导您朝着正确的方向前进:

  • 将给定的字符串解析为 NSDate 对象,忽略日期信息(结果:时间被处理为假定它们是同一天)并执行时区转换
  • 获取从较早的NSDate到较晚的NSDate的时间间隔
  • “今天在较早的给定时间”“昨天在较早的给定时间”创建 NSDate 对象
  • 比较从这两个 NSDate 到当前日期/时间的时间间隔与两个给定日期/时间之间的时间间隔

另请注意,NSTimeZone 无法理解您所提供格式的时区字符串("Pacific Time (US & Canada)"),因此您需要在那里做一些转换。

这是一个代码示例(我是在 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;
}

关于iphone - 判断当前本地时间是否在两个时间之间(忽略日期部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2108223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com