Changeset 6231


Ignore:
Timestamp:
Jun 19, 2008, 3:54:59 PM (13 years ago)
Author:
livings124
Message:

finish purse of NSCalendarDate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/Controller.m

    r6226 r6231  
    23522352    if (![fDefaults boolForKey: @"SpeedLimitAuto"])
    23532353        return;
    2354  
    2355     NSCalendarDate * onDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2356                                 [[fDefaults objectForKey: @"SpeedLimitAutoOnDate"] timeIntervalSinceReferenceDate]],
    2357                     * offDate = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
    2358                                 [[fDefaults objectForKey: @"SpeedLimitAutoOffDate"] timeIntervalSinceReferenceDate]],
    2359                     * nowDate = [NSCalendarDate calendarDate];
     2354   
     2355    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
     2356    NSDateComponents * nowComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
     2357                                        | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: [NSDate date]],
     2358                    * onComponents = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit
     2359                                        fromDate: [fDefaults objectForKey: @"SpeedLimitAutoOnDate"]],
     2360                    * offComponents = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit
     2361                                        fromDate: [fDefaults objectForKey: @"SpeedLimitAutoOffDate"]];
     2362    [calendar release];
    23602363   
    23612364    //check if should be on if within range
    2362     int onTime = [onDate hourOfDay] * 60 + [onDate minuteOfHour],
    2363         offTime = [offDate hourOfDay] * 60 + [offDate minuteOfHour],
    2364         nowTime = [nowDate hourOfDay] * 60 + [nowDate minuteOfHour];
     2365    int onTime = [onComponents hour] * 60 + [onComponents minute],
     2366        offTime = [offComponents hour] * 60 + [offComponents minute],
     2367        nowTime = [nowComponents hour] * 60 + [nowComponents minute];
    23652368   
    23662369    BOOL shouldBeOn = NO;
     
    23742377        [self toggleSpeedLimit: nil];
    23752378   
    2376     //no need to check if both times are equal
     2379    //no need to set the timer if both times are equal
    23772380    if (onTime == offTime)
    23782381        return;
     
    23812384}
    23822385
    2383 //only called from fSpeedLimitTimer
     2386//only called by fSpeedLimitTimer
    23842387- (void) autoSpeedLimit: (NSTimer *) timer
    23852388{
     
    24022405- (void) setAutoSpeedLimitTimer: (BOOL) nextIsLimit
    24032406{
    2404     NSDate * timerDate = [fDefaults objectForKey: nextIsLimit ? @"SpeedLimitAutoOnDate" : @"SpeedLimitAutoOffDate"],
    2405             * nowDate = [NSDate date];
     2407    NSDate * timerDate = [fDefaults objectForKey: nextIsLimit ? @"SpeedLimitAutoOnDate" : @"SpeedLimitAutoOffDate"];
    24062408   
    24072409    //create date with combination of the current date and the date to go off
    24082410    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    2409    
    24102411    NSDateComponents * nowComponents = [calendar components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
    2411                                         | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: nowDate];
    2412     NSDateComponents * timerComponents = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: timerDate];
    2413    
     2412                                        | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: [NSDate date]],
     2413                    * timerComponents = [calendar components: NSHourCalendarUnit | NSMinuteCalendarUnit fromDate: timerDate];
     2414   
     2415    //check if should be the next day
    24142416    int nowTime = [nowComponents hour] * 60 + [nowComponents minute],
    24152417        timerTime = [timerComponents hour] * 60 + [timerComponents minute];
     2418    if (timerTime < nowTime)
     2419        [nowComponents setDay: [nowComponents day] + 1]; //properly goes to next month when appropriate
    24162420   
    24172421    [nowComponents setHour: [timerComponents hour]];
     
    24212425    NSDate * dateToUse = [calendar dateFromComponents: nowComponents];
    24222426    [calendar release];
    2423    
    2424     //check if should be the next day
    2425     if (timerTime < nowTime)
    2426         dateToUse = [dateToUse addTimeInterval: 60 * 60 * 24]; //60 sec * 60 min * 24 hr
    24272427   
    24282428    fSpeedLimitTimer = [[NSTimer alloc] initWithFireDate: dateToUse interval: 0 target: self selector: @selector(autoSpeedLimit:)
Note: See TracChangeset for help on using the changeset viewer.