/**
		 * MacDateTime::LocalTZA
		 * 
		 * Determines the Local Time Zone Adjustment, in seconds from GMT
		 */
		double MacDateTime::LocalTZA(double time)
		{
			CFTimeZoneRef tz = CFTimeZoneCopySystem();
			if (tz) 
			{			
				CFAbsoluteTime at = ECMADateToCFAbsoluteTime(time);
				// pass now as the reference. 
				// This will include any daylight savings offsets in the result.
				CFTimeInterval result = CFTimeZoneGetSecondsFromGMT(tz, at);
						
				CFRelease(tz);	

				result *= kMsecPerSecond;
		
				// Remove the effects of daylight saving time
				return ( result -= MacDateTime::DaylightSavingTA(time) );
			}

			MachineLocation location;
			ReadMachineLocation(&location);

			// Extract the GMT delta
			long gmtDelta = location.u.gmtDelta & 0xFFFFFF;
			if (gmtDelta & 0x800000) {
				gmtDelta |= 0xFF000000;
			}

			// Remove the effects of daylight saving time
			if (location.u.dlsDelta < 0) {
				gmtDelta -= 3600;
			}
 
			return (double)gmtDelta * kMsecPerSecond;
		}
// returns false if event occurs at 8PM and now it's 10PM
// returns true if event occurs at 8PM, now it's 9AM
static bool
upcomingToday(CFDictionaryRef event, int today_cf)
{
    static const int        kAllowScheduleWindowSeconds = 5;
    CFGregorianDate         greg_now;
    CFTimeZoneRef           tizzy;
    uint32_t                secondsToday;
    int                     secondsScheduled;
	int						days_mask;
    if(!event) return false;
    
    // Determine if the scheduled event falls on today's day of week
    days_mask = getRepeatingDictionaryDayMask(event);
    if(!(days_mask & (1 << (today_cf-1)))) return false;
    
    // get gregorian date for right now
    tizzy = CFTimeZoneCopySystem();    
    greg_now = CFAbsoluteTimeGetGregorianDate(
                    CFAbsoluteTimeGetCurrent(), tizzy);
    CFRelease(tizzy);
    
    secondsToday = 60 * ((greg_now.hour*60) + greg_now.minute);
    secondsScheduled = 60 * getRepeatingDictionaryMinutes(event);

    // Initially, we required a 2 minute safety window before scheduling the next
    // power event. Now, we throw caution to the wind and try a 5 second window.
    // Lost events will simply be lost events.
    if(secondsScheduled >= (secondsToday + kAllowScheduleWindowSeconds))
        return true;
    else 
        return false;
}
Beispiel #3
0
void filecontainer_doopen(t_filecontainer *x, t_symbol *arg)
{
    t_atom			a[4];
    int				err = 0;
    char			filename[256];
    short			path;
    t_fourcc		outtype = 0;
    t_fourcc		type = 'cO0p';
#ifdef MAC_VERSION
    char			*temppath;
    FSRef			ref;
    Boolean			isDir;
    FSCatalogInfo	catalogInfo;
#else // WIN_VERSION
    char			temppath[512];
#endif
    char			fullpath[512];
    t_object		*result = NULL;
    t_object		*result2 = NULL;
    char			**record = NULL;		// sqlite records
    char			**record2 = NULL;		// sqlite records
    t_filehandle	file_handle;
    t_ptr_size		len = 0;
    char			*blob;
    char			sql[512];

    if(!arg || !arg->s_name[0]) {
        if(open_dialog(filename, &path, &outtype, NULL, -1))			// Returns 0 if successful
            return;
    }
    else {
        t_fourcc typelist[1];
        typelist[0] = 'cO0p';
        strcpy(filename, arg->s_name);
        path = 0;
        locatefile_extended(filename, &path, &type, typelist, 0);
    }
    path_topotentialname(path, filename, fullpath, 0);
#ifdef MAC_VERSION
    temppath = strchr(fullpath, ':');
    temppath += 1;
#else // WIN_VERSION
    path_nameconform(fullpath, temppath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE);
#endif
    x->name = gensym(temppath);

    // Create our temp folder for extracted files
    filecontainer_gettemppath(x);

    // Create the SQLite instance
    atom_setsym(&a[0], gensym("@rambased"));
    atom_setlong(&a[1], 0);
    atom_setsym(&a[2], gensym("@db"));
    atom_setsym(&a[3], x->name);
    x->sqlite = object_new_typed(CLASS_NOBOX, _sym_sqlite, 4, a);

    // Operate on the open DB
    if(x->sqlite) {
        object_method(x->sqlite, ps_starttransaction);
        object_method(x->sqlite, _sym_execstring, TABLEDEF_FILES, NULL);
        object_method(x->sqlite, _sym_execstring, TABLEDEF_ATTRS, NULL);

        object_method(x->sqlite, _sym_execstring, "UPDATE files SET valid = 1", NULL);
        object_method(x->sqlite, _sym_execstring, "SELECT file_id, filename, moddate FROM files", &result);
        while(record = (char **)object_method(result, _sym_nextrecord)) {
            // Here we check for the optional 'platform' attr for this file.
            // If a flag exists for the other platform, but not for the current platform, then we ignore this file.
#ifdef MAC_VERSION
            sprintf(sql, "SELECT file_id_ext FROM attrs \
							WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]);
            object_method(x->sqlite, _sym_execstring, sql, &result2);
            record2 = (char **)object_method(result2, _sym_nextrecord);
            if(record2) {
                sprintf(sql, "SELECT file_id_ext FROM attrs \
								WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]);
                object_method(x->sqlite, _sym_execstring, sql, &result2);
                record2 = (char **)object_method(result2, _sym_nextrecord);
                if(!record2) {
                    sprintf(sql, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]);
                    object_method(x->sqlite, _sym_execstring, sql, NULL);
                    continue;
                }
            }
#else // WIN_VERSION
            snprintf(sql, 512, "SELECT file_id_ext FROM attrs \
							WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]);
            object_method(x->sqlite, _sym_execstring, sql, &result2);
            record2 = (char **)object_method(result2, _sym_nextrecord);
            if(record2) {
                snprintf(sql, 512, "SELECT file_id_ext FROM attrs \
								WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]);
                object_method(x->sqlite, _sym_execstring, sql, &result2);
                record2 = (char **)object_method(result2, _sym_nextrecord);
                if(!record2) {
                    snprintf(sql, 512, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]);
                    object_method(x->sqlite, _sym_execstring, sql, NULL);
                    continue;
                }
            }
#endif
            // At this point we have a file (record[0]), and we have determined that it is indeed a file we want to cache
            // So cache it to a new file in our temp path
            err = path_createsysfile(record[1], x->temp_path, type, &file_handle);
            if(err) {																// Handle any errors that occur
                object_error((t_object *)x, "%s - error %d creating file", filename, err);
            }
            else {
                snprintf(sql, 512, "SELECT content FROM files WHERE file_id = %s", record[0]);
                object_method(x->sqlite, ps_getblob, sql, &blob, &len);
                err = sysfile_write(file_handle, &len, blob);
                if(err) {
                    object_error((t_object *)x, "sysfile_write error (%d)", err);
                }
            }
            err = sysfile_seteof(file_handle, len);
            if(err) {
                object_error((t_object *)x, "%s - error %d setting EOF", filename, err);
            }
            sysfile_close(file_handle);		// close file reference
            sysmem_freeptr(blob);
            blob = NULL;

            // Set the moddate
#ifdef MAC_VERSION
//			FSCatalogInfo		catalogInfo;
//			Boolean             status;
            CFGregorianDate     gdate;
            CFAbsoluteTime      abstime;
            CFTimeZoneRef		tz;
            UTCDateTime			utc;

            sscanf(record[2], "%4ld-%02hd-%02hd %02hd:%02hd:%02lf", &gdate.year, (signed short*)&gdate.month, (signed short*)&gdate.day,
                   (signed short*)&gdate.hour, (signed short*)&gdate.minute, &gdate.second);
            tz = CFTimeZoneCopySystem();
            abstime = CFGregorianDateGetAbsoluteTime(gdate, tz);
            UCConvertCFAbsoluteTimeToUTCDateTime(abstime, &utc);
            catalogInfo.contentModDate = utc;

            strcpy(s_tempstr, x->temp_fullpath->s_name);
            temppath = strchr(s_tempstr, ':');
            temppath++;
            strcat(temppath, "/");
            strcat(temppath, record[1]);
            FSPathMakeRef((UInt8*)temppath, &ref, &isDir);
            err = FSSetCatalogInfo(&ref, kFSCatInfoContentMod, &catalogInfo);

#else // WIN_VERSION
            char				winpath[512];
            HANDLE				hFile;
            FILETIME			fileTime;
            SYSTEMTIME			systemTime;

            sscanf(record[2], "%4lu-%02lu-%02lu %02lu:%02lu:%02lu", &systemTime.wYear, &systemTime.wMonth, &systemTime.wDay,
                   &systemTime.wHour, &systemTime.wMinute, &systemTime.wSecond);
            err = SystemTimeToFileTime(&systemTime, &fileTime);

            strcpy(s_tempstr, x->temp_fullpath->s_name);
            path_nameconform(s_tempstr, winpath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE);
            strcat(winpath, "\\");
            strcat(winpath, record[1]);
            hFile = CreateFile((LPCSTR)winpath , GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
            if(hFile == INVALID_HANDLE_VALUE) {
                object_error((t_object *)x, "invalid handle value");
                goto out;
            }
            err = SetFileTime(hFile, &fileTime, &fileTime, &fileTime);
            if(err == 0) {
                err = GetLastError();
                object_error((t_object *)x, "Error setting date: %i", err);
            }
            CloseHandle(hFile);
out:
            ;
#endif
        }
        object_method(x->sqlite, ps_endtransaction);
    }
// performs a locale sensitive date formatting operation on the struct tm parameter
nsresult nsDateTimeFormatMac::FormatTMTime(nsILocale* locale, 
                                           const nsDateFormatSelector  dateFormatSelector, 
                                           const nsTimeFormatSelector timeFormatSelector, 
                                           const struct tm*  tmTime, 
                                           nsAString& stringOut)
{
  nsresult res = NS_OK;

  // set up locale data
  (void) Initialize(locale);
  
  // return, nothing to format
  if (dateFormatSelector == kDateFormatNone && timeFormatSelector == kTimeFormatNone) {
    stringOut.Truncate();
    return NS_OK;
  }

  NS_ASSERTION(tmTime->tm_mon >= 0, "tm is not set correctly");
  NS_ASSERTION(tmTime->tm_mday >= 1, "tm is not set correctly");
  NS_ASSERTION(tmTime->tm_hour >= 0, "tm is not set correctly");
  NS_ASSERTION(tmTime->tm_min >= 0, "tm is not set correctly");
  NS_ASSERTION(tmTime->tm_sec >= 0, "tm is not set correctly");
  NS_ASSERTION(tmTime->tm_wday >= 0, "tm is not set correctly");

  // Got the locale for the formatter:
  CFLocaleRef formatterLocale;
  if (!locale) {
    formatterLocale = CFLocaleCopyCurrent();
  } else {
    CFStringRef localeStr = CFStringCreateWithCharacters(nullptr,
                                                         reinterpret_cast<const UniChar*>(mLocale.get()),
                                                         mLocale.Length());
    formatterLocale = CFLocaleCreate(nullptr, localeStr);
    CFRelease(localeStr);
  }

  // Get the date style for the formatter:  
  CFDateFormatterStyle dateStyle;
  switch (dateFormatSelector) {
    case kDateFormatLong:
      dateStyle = kCFDateFormatterLongStyle;
      break;
    case kDateFormatShort:
      dateStyle = kCFDateFormatterShortStyle;
      break;
    case kDateFormatYearMonth:
    case kDateFormatWeekday:
      dateStyle = kCFDateFormatterNoStyle; // formats handled below
      break;
    case kDateFormatNone:
      dateStyle = kCFDateFormatterNoStyle;
      break;
    default:
      NS_ERROR("Unknown nsDateFormatSelector");
      res = NS_ERROR_FAILURE;
      dateStyle = kCFDateFormatterNoStyle;
  }
  
  // Get the time style for the formatter:
  CFDateFormatterStyle timeStyle;
  switch (timeFormatSelector) {
    case kTimeFormatSeconds:
    case kTimeFormatSecondsForce24Hour: // 24 hour part fixed below
      timeStyle = kCFDateFormatterMediumStyle;
      break;
    case kTimeFormatNoSeconds:
    case kTimeFormatNoSecondsForce24Hour: // 24 hour part fixed below
      timeStyle = kCFDateFormatterShortStyle;
      break;
    case kTimeFormatNone:
      timeStyle = kCFDateFormatterNoStyle;
      break;
    default:
      NS_ERROR("Unknown nsTimeFormatSelector");
      res = NS_ERROR_FAILURE;
      timeStyle = kCFDateFormatterNoStyle;
  }
  
  // Create the formatter and fix up its formatting as necessary:
  CFDateFormatterRef formatter =
    CFDateFormatterCreate(nullptr, formatterLocale, dateStyle, timeStyle);
  
  CFRelease(formatterLocale);
  
  if (dateFormatSelector == kDateFormatYearMonth ||
      dateFormatSelector == kDateFormatWeekday) {
    CFStringRef dateFormat =
      dateFormatSelector == kDateFormatYearMonth ? CFSTR("yyyy/MM ") : CFSTR("EEE ");
    
    CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
    CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat);
    CFStringInsert(newFormat, 0, dateFormat);
    CFDateFormatterSetFormat(formatter, newFormat);
    CFRelease(newFormat); // note we don't own oldFormat
  }
  
  if (timeFormatSelector == kTimeFormatSecondsForce24Hour ||
      timeFormatSelector == kTimeFormatNoSecondsForce24Hour) {
    // Replace "h" with "H", and remove "a":
    CFStringRef oldFormat = CFDateFormatterGetFormat(formatter);
    CFMutableStringRef newFormat = CFStringCreateMutableCopy(nullptr, 0, oldFormat);
    CFIndex replaceCount = CFStringFindAndReplace(newFormat,
                                                  CFSTR("h"), CFSTR("H"),
                                                  CFRangeMake(0, CFStringGetLength(newFormat)),	
                                                  0);
    NS_ASSERTION(replaceCount <= 2, "Unexpected number of \"h\" occurrences");
    replaceCount = CFStringFindAndReplace(newFormat,
                                          CFSTR("a"), CFSTR(""),
                                          CFRangeMake(0, CFStringGetLength(newFormat)),	
                                          0);
    NS_ASSERTION(replaceCount <= 1, "Unexpected number of \"a\" occurrences");
    CFDateFormatterSetFormat(formatter, newFormat);
    CFRelease(newFormat); // note we don't own oldFormat
  }
  
  // Now get the formatted date:
  CFGregorianDate date;
  date.second = tmTime->tm_sec;
  date.minute = tmTime->tm_min;
  date.hour = tmTime->tm_hour;
  date.day = tmTime->tm_mday;      // Mac is 1-based, tm is 1-based
  date.month = tmTime->tm_mon + 1; // Mac is 1-based, tm is 0-based
  date.year = tmTime->tm_year + 1900;

  CFTimeZoneRef timeZone = CFTimeZoneCopySystem(); // tmTime is in local time
  CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime(date, timeZone);
  CFRelease(timeZone);

  CFStringRef formattedDate = CFDateFormatterCreateStringWithAbsoluteTime(nullptr,
                                                                          formatter,
                                                                          absTime);

  CFIndex stringLen = CFStringGetLength(formattedDate);

  AutoTArray<UniChar, 256> stringBuffer;
  stringBuffer.SetLength(stringLen + 1);
  CFStringGetCharacters(formattedDate, CFRangeMake(0, stringLen), stringBuffer.Elements());
  stringOut.Assign(reinterpret_cast<char16_t*>(stringBuffer.Elements()), stringLen);

  CFRelease(formattedDate);
  CFRelease(formatter);

  return res;
}
Beispiel #5
0
CFStringRef
_SCCopyDescription(CFTypeRef cf, CFDictionaryRef formatOptions)
{
#ifdef	ENABLE_SC_FORMATTING
	CFMutableDictionaryRef	nFormatOptions;
	CFStringRef		prefix1;
	CFStringRef		prefix2;
	CFTypeID		type	= CFGetTypeID(cf);

	if (!formatOptions ||
	    !CFDictionaryGetValueIfPresent(formatOptions, CFSTR("PREFIX1"), (const void **)&prefix1)) {
		prefix1 = CFSTR("");
	}

	if (type == CFStringGetTypeID()) {
		return CFStringCreateWithFormat(NULL,
						formatOptions,
						CFSTR("%@%@"),
						prefix1,
						cf);
	}

	if (type == CFBooleanGetTypeID()) {
		return CFStringCreateWithFormat(NULL,
						formatOptions,
						CFSTR("%@%s"),
						prefix1,
						CFBooleanGetValue(cf) ? "TRUE" : "FALSE");
	}

	if (type == CFDataGetTypeID()) {
		const uint8_t		*data;
		CFIndex			dataLen;
		CFIndex			i;
		CFMutableStringRef	str;

		str = CFStringCreateMutable(NULL, 0);
		CFStringAppendFormat(str, formatOptions, CFSTR("%@<data> 0x"), prefix1);

		data    = CFDataGetBytePtr(cf);
		dataLen = CFDataGetLength(cf);
		for (i = 0; i < dataLen; i++) {
			CFStringAppendFormat(str, NULL, CFSTR("%02x"), data[i]);
		}

		return str;
	}

	if (type == CFNumberGetTypeID()) {
		return CFStringCreateWithFormat(NULL,
						formatOptions,
						CFSTR("%@%@"),
						prefix1,
						cf);
	}

	if (type == CFDateGetTypeID()) {
		CFCalendarRef	calendar;
		CFStringRef	str;
		CFTimeZoneRef	tz;
		int		MM, DD, YYYY, hh, mm, ss;

		calendar = CFCalendarCreateWithIdentifier(NULL, kCFGregorianCalendar);
		tz = CFTimeZoneCopySystem();
		CFCalendarSetTimeZone(calendar, tz);
		CFRelease(tz);
		CFCalendarDecomposeAbsoluteTime(calendar,
						CFDateGetAbsoluteTime(cf),
						"MdyHms",
						&MM, &DD, &YYYY, &hh, &mm, &ss);
		CFRelease(calendar);

		str = CFStringCreateWithFormat(NULL,
					       formatOptions,
					       CFSTR("%@%02d/%02d/%04d %02d:%02d:%02d"),
					       prefix1,
					       MM, DD, YYYY, hh, mm, ss);
		return str;
	}

	if ((formatOptions == NULL) ||
	    !CFDictionaryGetValueIfPresent(formatOptions, CFSTR("PREFIX2"), (const void **)&prefix2)) {
		prefix2 = prefix1;
	}

	if (formatOptions != NULL) {
		nFormatOptions = CFDictionaryCreateMutableCopy(NULL, 0, formatOptions);
	} else {
		nFormatOptions = CFDictionaryCreateMutable(NULL,
							   0,
							   &kCFTypeDictionaryKeyCallBacks,
							   &kCFTypeDictionaryValueCallBacks);
	}
	assert(nFormatOptions != NULL);

#define	N_QUICK	32

	if (type == CFArrayGetTypeID()) {
		const void *		elements_q[N_QUICK];
		const void **		elements	= elements_q;
		CFIndex			i;
		CFIndex			nElements;
		CFMutableStringRef	str;

		str = CFStringCreateMutable(NULL, 0);
		CFStringAppendFormat(str, formatOptions, CFSTR("%@<array> {"), prefix1);

		nElements = CFArrayGetCount(cf);
		if (nElements > 0) {
			if (nElements > (CFIndex)(sizeof(elements_q)/sizeof(CFTypeRef)))
				elements  = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
			CFArrayGetValues(cf, CFRangeMake(0, nElements), elements);
			for (i = 0; i < nElements; i++) {
				CFMutableStringRef	nPrefix1;
				CFMutableStringRef	nPrefix2;
				CFStringRef		nStr;
				CFStringRef		vStr;

				nStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%ld"), i);

				nPrefix1 = CFStringCreateMutable(NULL, 0);
				CFStringAppendFormat(nPrefix1,
						     formatOptions,
						     CFSTR("%@  %@ : "),
						     prefix2,
						     nStr);
				nPrefix2 = CFStringCreateMutable(NULL, 0);
				CFStringAppendFormat(nPrefix2,
						     formatOptions,
						     CFSTR("%@  "),
						     prefix2);

				CFDictionarySetValue(nFormatOptions, CFSTR("PREFIX1"), nPrefix1);
				CFDictionarySetValue(nFormatOptions, CFSTR("PREFIX2"), nPrefix2);
				CFRelease(nPrefix1);
				CFRelease(nPrefix2);
				CFRelease(nStr);

				vStr = _SCCopyDescription((CFTypeRef)elements[i], nFormatOptions);
				CFStringAppendFormat(str,
						     formatOptions,
						     CFSTR("\n%@"),
						     vStr);
				CFRelease(vStr);
			}
			if (elements != elements_q) CFAllocatorDeallocate(NULL, elements);
		}
		CFStringAppendFormat(str, formatOptions, CFSTR("\n%@}"), prefix2);

		CFRelease(nFormatOptions);
		return str;
	}

	if (type == CFDictionaryGetTypeID()) {
		const void *		keys_q[N_QUICK];
		const void **		keys	= keys_q;
		CFIndex			i;
		CFIndex			nElements;
		CFMutableStringRef	nPrefix1;
		CFMutableStringRef	nPrefix2;
		CFMutableStringRef	str;

		str = CFStringCreateMutable(NULL, 0);
		CFStringAppendFormat(str, formatOptions, CFSTR("%@<dictionary> {"), prefix1);

		nElements = CFDictionaryGetCount(cf);
		if (nElements > 0) {
			CFComparatorFunction	compFunc	= NULL;
			CFMutableArrayRef	sortedKeys;

			if (nElements > (CFIndex)(sizeof(keys_q) / sizeof(CFTypeRef))) {
				keys = CFAllocatorAllocate(NULL, nElements * sizeof(CFTypeRef), 0);
			}
			CFDictionaryGetKeysAndValues(cf, keys, NULL);

			sortedKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
			for (i = 0; i < nElements; i++) {
				CFArrayAppendValue(sortedKeys, (CFStringRef)keys[i]);
			}

			if (isA_CFString(keys[0])) {
				compFunc = (CFComparatorFunction)CFStringCompare;
			}
			else if (isA_CFNumber(keys[0])) {
				compFunc = (CFComparatorFunction)CFNumberCompare;
			}
			else if (isA_CFDate(keys[0])) {
				compFunc = (CFComparatorFunction)CFDateCompare;
			}

			if (compFunc != NULL) {
				CFArraySortValues(sortedKeys,
						  CFRangeMake(0, nElements),
						  compFunc,
						  NULL);
			}

			for (i = 0; i < nElements; i++) {
				CFStringRef		key;
				CFStringRef		kStr;
				CFTypeRef		val;
				CFStringRef		vStr;

				key  = CFArrayGetValueAtIndex(sortedKeys, i);
				kStr = _SCCopyDescription((CFTypeRef)key, NULL);

				nPrefix1 = CFStringCreateMutable(NULL, 0);
				CFStringAppendFormat(nPrefix1,
						     formatOptions,
						     CFSTR("%@  %@ : "),
						     prefix2,
						     kStr);
				nPrefix2 = CFStringCreateMutable(NULL, 0);
				CFStringAppendFormat(nPrefix2,
						     formatOptions,
						     CFSTR("%@  "),
						     prefix2);

				CFDictionarySetValue(nFormatOptions, CFSTR("PREFIX1"), nPrefix1);
				CFDictionarySetValue(nFormatOptions, CFSTR("PREFIX2"), nPrefix2);
				CFRelease(nPrefix1);
				CFRelease(nPrefix2);
				CFRelease(kStr);

				val  = CFDictionaryGetValue(cf, key);
				vStr = _SCCopyDescription((CFTypeRef)val, nFormatOptions);
				CFStringAppendFormat(str,
						     formatOptions,
						     CFSTR("\n%@"),
						     vStr);
				CFRelease(vStr);
			}

			CFRelease(sortedKeys);

			if (keys != keys_q) {
				CFAllocatorDeallocate(NULL, keys);
			}
		}
		CFStringAppendFormat(str, formatOptions, CFSTR("\n%@}"), prefix2);

		CFRelease(nFormatOptions);
		return str;
	}

	CFRelease(nFormatOptions);
#endif	/* ENABLE_SC_FORMATTING */

	return CFStringCreateWithFormat(NULL,
					formatOptions,
					CFSTR("%@%@"),
					prefix1,
					cf);
}
/*
 * 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;
}
Beispiel #7
0
/*
 * Parse a GeneralizedTime string into a CFAbsoluteTime. Returns NULL on parse error.
 * Fractional parts of a second are discarded.
 */
CFAbsoluteTime genTimeToCFAbsTime(
	const CSSM_DATA *strData)
{
	if((strData == NULL) || (strData->Data == NULL) || (strData->Length == 0)) {
    	return NULL_TIME;
  	}

	uint8 *timeStr = strData->Data;
	size_t timeStrLen = strData->Length;

  	/* tolerate NULL terminated or not */
  	if(timeStr[timeStrLen - 1] == '\0') {
  		timeStrLen--;
  	}

	/* start with a fresh editable copy */
	uint8 *str = (uint8 *)malloc(timeStrLen);
	uint32 strLen = 0;

	/*
	 * If there is a decimal point, strip it and all trailing digits off
	 */
	const uint8 *inCp = timeStr;
	uint8 *outCp = str;
	int foundDecimal = 0;
	int minutesOffset = 0;
	int hoursOffset = 0;
	bool minusOffset = false;
	bool isGMT = false;
	size_t toGo = timeStrLen;

	do {
		if(*inCp == '.') {
			if(foundDecimal) {
				/* only legal once */ {
					free(str);
					return NULL_TIME;
				}
			}
			foundDecimal++;

			/* skip the decimal point... */
			inCp++;
			toGo--;
			if(toGo == 0) {
				/* all done */
				break;
			}
			/* then all subsequent contiguous digits */
			while(isdigit(*inCp) && (toGo != 0)) {
				inCp++;
				toGo--;
			}
		}	/* decimal point processing */
		else if((*inCp == '+') || (*inCp == '-')) {
			/* Time zone offset - handle 2 or 4 chars */
			if((toGo != 2) & (toGo != 4)) {
				free(str);
				return NULL_TIME;
			}
			if(*inCp == '-') {
				minusOffset = true;
			}
			inCp++;
			hoursOffset = (10 * (inCp[0] - '0')) + (inCp[1] - '0');
			toGo -= 2;
			if(toGo) {
				minutesOffset = (10 * (inCp[0] - '0')) + (inCp[1] - '0');
				toGo -= 2;
			}
		}
		else {
			*outCp++ = *inCp++;
			strLen++;
			toGo--;
		}
	} while(toGo != 0);

	if(str[strLen - 1] == 'Z') {
		isGMT = true;
		strLen--;
	}

	CFAbsoluteTime absTime;
	absTime = parseGenTime(str, strLen);
	free(str);
	if(absTime == NULL_TIME) {
		return NULL_TIME;
	}

	/* post processing needed? */
	if(isGMT) {
		/* Nope, string was in GMT */
		return absTime;
	}
	if((minutesOffset != 0) || (hoursOffset != 0)) {
		/* string contained explicit offset from GMT */
		if(minusOffset) {
			absTime -= (minutesOffset * 60);
			absTime -= (hoursOffset * 3600);
		}
		else {
			absTime += (minutesOffset * 60);
			absTime += (hoursOffset * 3600);
		}
	}
	else {
		/* implciit offset = local */
		CFTimeInterval tzDelta;
		CFTimeZoneRef localZone = CFTimeZoneCopySystem();
		tzDelta = CFTimeZoneGetSecondsFromGMT (localZone, CFAbsoluteTimeGetCurrent());
		CFRelease(localZone);
		absTime += tzDelta;
	}
	return absTime;
}