查找指定时间段之间的当前时间

时间:2016-02-23 08:41:57

标签: ios objective-c iphone cocoa-touch

我正在尝试编写一种实用工具方法,通过考虑当前时间,将返回时间段(早餐,午餐,小吃或晚餐)。

以下是我采取的逐步程序来破解这一点。但是,我总是通过NextTimeslotNone方法重新nextTimeslot。当我打印breakfastBeginingDatebreakfastEndingDate时 - 两者都在控制台上打印2016-02-22 18:30:00 +0000

知道我在这里做错了什么吗?

第1步:为广告位标识定义了枚举

typedef NS_ENUM(NSInteger, NextTimeslot) {
    NextTimeslotBreakfast = 0,
    NextTimeslotLunch,
    NextTimeslotSnacks,
    NextTimeslotDinner,
    NextTimeslotNone
};

第2步:编写一个方法来设置一些任意日期组件(在比较时间段时忽略日期)

+ (NSDate *)dateByNeutralizingDateComponentsOfDate:(NSDate *)originalDate {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    // Get the components for this date
    NSDateComponents *components = [gregorian components:  (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate: originalDate];

    // Set the year, month and day to same arbitrary values
    [components setYear:2000];
    [components setMonth:1];
    [components setDay:1];

    return [gregorian dateFromComponents:components];
}

步骤3:在比较日期是否在两个指定日期之间时,写了一个设置return BOOL的方法

+ (BOOL)isTimeOfDate:(NSDate *)targetDate betweenStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate {
    if (!targetDate || !startDate || !endDate) {
        return NO;
    }

    // Make sure all the dates have the same date component.
    NSDate *newStartDate = [MyUtility dateByNeutralizingDateComponentsOfDate:startDate];
    NSDate *newEndDate = [MyUtility dateByNeutralizingDateComponentsOfDate:endDate];
    NSDate *newTargetDate = [MyUtility dateByNeutralizingDateComponentsOfDate:targetDate];

    // Compare the target with the start and end dates
    NSComparisonResult compareTargetToStart = [newTargetDate compare:newStartDate];
    NSComparisonResult compareTargetToEnd = [newTargetDate compare:newEndDate];

    return (compareTargetToStart == NSOrderedDescending && compareTargetToEnd == NSOrderedAscending);
}

第4步:编写方法以返回当前时间的正确时段。

+ (NextTimeslot)nextTimeslot {
    NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:3600];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm a"];

    // Breakfast slot (9:00 AM - 11:00 AM)
    NSString *breakfastBeginingTime = @"2016-02-23 09:00 am";
    NSString *breakfastEndingTime = @"2016-02-23 11:00 am";
    NSDate *breakfastBeginingDate = [formatter dateFromString:breakfastBeginingTime];
    NSDate *breakfastEndingDate = [formatter dateFromString:breakfastEndingTime];

    if ([MyUtility isTimeOfDate:currentDate betweenStartDate:breakfastBeginingDate andEndDate:breakfastEndingDate]) {
        return NextTimeslotBreakfast;
    }

    // Lunch slot (11:30 AM - 2:00 PM)
    NSString *lunchBeginingTime = @"2016-02-23 11:30 am";
    NSString *lunchEndingTime = @"2016-02-23 02:00 pm";
    NSDate *lunchBeginingDate = [formatter dateFromString:lunchBeginingTime];
    NSDate *lunchEndingDate = [formatter dateFromString:lunchEndingTime];

    if ([MyUtility isTimeOfDate:currentDate betweenStartDate:lunchBeginingDate andEndDate:lunchEndingDate]) {
        return NextTimeslotLunch;
    }

    // Snacks slot (3:30 PM - 6:30 PM)
    NSString *snacksBeginingTime = @"2016-02-23 03:30 pm";
    NSString *snacksEndingTime = @"2016-02-23 06:30 pm";
    NSDate *snacksBeginingDate = [formatter dateFromString:snacksBeginingTime];
    NSDate *snacksEndingDate = [formatter dateFromString:snacksEndingTime];

    if ([MyUtility isTimeOfDate:currentDate betweenStartDate:snacksBeginingDate andEndDate:snacksEndingDate]) {
        return NextTimeslotSnacks;
    }

    // Dinner slot (07:30 PM - 10:00 PM)
    NSString *dinnerBeginingTime = @"2016-02-23 07:30 pm";
    NSString *dinnerEndingTime = @"2016-02-23 10:00 pm";
    NSDate *dinnerBeginingDate = [formatter dateFromString:dinnerBeginingTime];
    NSDate *dinnerEndingDate = [formatter dateFromString:dinnerEndingTime];

    if ([MyUtility isTimeOfDate:currentDate betweenStartDate:dinnerBeginingDate andEndDate:dinnerEndingDate]) {
        return NextTimeslotDinner;
    }

    return NextTimeslotNone;
}

1 个答案:

答案 0 :(得分:0)

好的,通过添加另一种方法将UTC转换为当前系统时区来修复它。这非常好。分享它希望能为其他人节省宝贵的时间。

+ (NSDate *)systemDateForDate:(NSDate *)iDate {
    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    NSInteger seconds = [tz secondsFromGMTForDate:iDate];
    return [NSDate dateWithTimeInterval: seconds sinceDate:iDate];
}

在我的时段计算中使用此方法。

+ (NextTimeslot)nextTimeslot {
    NSDate *currentDate = [MyFCUtility systemDateForDate:[NSDate date]];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    // Breakfast slot (9:00 AM - 11:00 AM)
    NSString *breakfastBeginingTime = @"2016-02-23 09:00:00";
    NSString *breakfastEndingTime = @"2016-02-23 11:00:00";
    NSDate *breakfastBeginingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:breakfastBeginingTime]];
    NSDate *breakfastEndingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:breakfastEndingTime]];

    if ([MyFCUtility isTimeOfDate:currentDate betweenStartDate:breakfastBeginingDate andEndDate:breakfastEndingDate]) {
        return NextTimeslotBreakfast;
    }

    // Lunch slot (11:30 AM - 2:00 PM)
    NSString *lunchBeginingTime = @"2016-02-23 11:30:00";
    NSString *lunchEndingTime = @"2016-02-23 15:00:00";
    NSDate *lunchBeginingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:lunchBeginingTime]];
    NSDate *lunchEndingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:lunchEndingTime]];


    if ([MyFCUtility isTimeOfDate:currentDate betweenStartDate:lunchBeginingDate andEndDate:lunchEndingDate]) {
        return NextTimeslotLunch;
    }

    // Snacks slot (3:30 PM - 6:30 PM)
    NSString *snacksBeginingTime = @"2016-02-23 15:30:00";
    NSString *snacksEndingTime = @"2016-02-23 18:30:00";
    NSDate *snacksBeginingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:snacksBeginingTime]];
    NSDate *snacksEndingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:snacksEndingTime]];

    if ([MyFCUtility isTimeOfDate:currentDate betweenStartDate:snacksBeginingDate andEndDate:snacksEndingDate]) {
        return NextTimeslotSnacks;
    }

    // Dinner slot (07:30 PM - 10:00 PM)
    NSString *dinnerBeginingTime = @"2016-02-23 19:30:00";
    NSString *dinnerEndingTime = @"2016-02-23 22:00:00";
    NSDate *dinnerBeginingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:dinnerBeginingTime]];
    NSDate *dinnerEndingDate = [MyFCUtility systemDateForDate:[formatter dateFromString:dinnerEndingTime]];

    if ([MyFCUtility isTimeOfDate:currentDate betweenStartDate:dinnerBeginingDate andEndDate:dinnerEndingDate]) {
        return NextTimeslotDinner;
    }

    return NextTimeslotNone;
}