bool check_gregorian_dates () { Boolean status; CFGregorianDate gregDate; CFAbsoluteTime absTime; long weekOfYear, dayOfWeek; CFShow(CFSTR("Checking Gregorian date functions")); // Construct a Gregorian date. gregDate.year = 1999; gregDate.month = 11; gregDate.day = 23; gregDate.hour = 17; gregDate.minute = 33; gregDate.second = 22.7; // Check the validity of the date. status = CFGregorianDateIsValid(gregDate, kCFGregorianAllUnits); printf("Is my Gregorian date valid? %d\n", status); // Convert the Gregorian date to absolute time. absTime = CFGregorianDateGetAbsoluteTime(gregDate, NULL); printf("The Absolute Time from a Gregorian date is: %f\n", absTime); CFShow(CFSTR("This corresponds to the following:")); weekOfYear = CFAbsoluteTimeGetWeekOfYear (absTime, NULL); dayOfWeek = CFAbsoluteTimeGetDayOfWeek (absTime, NULL); printf("Week of the year for %d-%d-%d is %ld\n", gregDate.month, gregDate.day, gregDate.year, weekOfYear); printf("Day of the week for %d-%d-%d is %ld\n", gregDate.month, gregDate.day, gregDate.year, dayOfWeek); printf("\n"); return true; }
/* * Returns a copy of repeat event after changing the repeat date * into next event date. * * Caller is responsible for releasing the copy after use. */ __private_extern__ CFDictionaryRef copyNextRepeatingEvent(CFStringRef type) { CFDictionaryRef repeatDict = NULL; CFStringRef repeatDictType = NULL; CFMutableDictionaryRef repeatDictCopy = NULL; CFGregorianDate greg; CFTimeZoneRef tizzy; CFAbsoluteTime ev_time; CFDateRef ev_date; int days; int minutes_scheduled; int cf_day_of_week; /* * 'WakeOrPowerOn' repeat events are returned when caller asks * for 'Wake' events or 'PowerOn' events. * Don't bother to return anything if caller is looking specifically for * WakeOrPowerOn type repeat events. */ if( CFEqual(type, CFSTR(kIOPMAutoSleep)) || CFEqual(type, CFSTR(kIOPMAutoShutdown)) || CFEqual(type, CFSTR(kIOPMAutoRestart)) ) { repeatDict = repeatingPowerOff; } else if ( CFEqual(type, CFSTR(kIOPMAutoPowerOn)) || CFEqual(type, CFSTR(kIOPMAutoWake)) ) { repeatDict = repeatingPowerOn; } else return NULL; repeatDictType = getRepeatingDictionaryType(repeatDict); if (CFEqual(type, repeatDictType) || ( (CFEqual(repeatDictType, CFSTR(kIOPMAutoWakeOrPowerOn))) && (CFEqual(type, CFSTR(kIOPMAutoPowerOn)) || CFEqual(type, CFSTR(kIOPMAutoWake))) ) ) { repeatDictCopy = CFDictionaryCreateMutableCopy(0,0,repeatDict); tizzy = CFTimeZoneCopySystem(); // Massage the scheduled time into today's date cf_day_of_week = CFAbsoluteTimeGetDayOfWeek( CFAbsoluteTimeGetCurrent(), tizzy); days = daysUntil(repeatDict, cf_day_of_week); greg = CFAbsoluteTimeGetGregorianDate( CFAbsoluteTimeGetCurrent() + days*(60*60*24), tizzy); minutes_scheduled = getRepeatingDictionaryMinutes(repeatDict); greg.hour = minutes_scheduled/60; greg.minute = minutes_scheduled%60; greg.second = 0.0; ev_time = CFGregorianDateGetAbsoluteTime(greg, tizzy); ev_date = CFDateCreate(kCFAllocatorDefault, ev_time); CFDictionarySetValue(repeatDictCopy, CFSTR(kIOPMPowerEventTimeKey), ev_date); /* Set 'AppNameKey' to 'Repeating' */ CFDictionarySetValue(repeatDictCopy, CFSTR(kIOPMPowerEventAppNameKey), CFSTR(kIOPMRepeatingAppName)); CFRelease(ev_date); CFRelease(tizzy); } return repeatDictCopy; }