CFCalendarRef CFCalendarCreateWithIdentifier(CFAllocatorRef allocator, CFStringRef identifier) { if (allocator == NULL) allocator = __CFGetDefaultAllocator(); __CFGenericValidateType(allocator, CFAllocatorGetTypeID()); __CFGenericValidateType(identifier, CFStringGetTypeID()); // return NULL until Chinese calendar is available if (identifier != kCFGregorianCalendar && identifier != kCFBuddhistCalendar && identifier != kCFJapaneseCalendar && identifier != kCFIslamicCalendar && identifier != kCFIslamicCivilCalendar && identifier != kCFHebrewCalendar) { // if (identifier != kCFGregorianCalendar && identifier != kCFBuddhistCalendar && identifier != kCFJapaneseCalendar && identifier != kCFIslamicCalendar && identifier != kCFIslamicCivilCalendar && identifier != kCFHebrewCalendar && identifier != kCFChineseCalendar) { if (CFEqual(kCFGregorianCalendar, identifier)) identifier = kCFGregorianCalendar; else if (CFEqual(kCFBuddhistCalendar, identifier)) identifier = kCFBuddhistCalendar; else if (CFEqual(kCFJapaneseCalendar, identifier)) identifier = kCFJapaneseCalendar; else if (CFEqual(kCFIslamicCalendar, identifier)) identifier = kCFIslamicCalendar; else if (CFEqual(kCFIslamicCivilCalendar, identifier)) identifier = kCFIslamicCivilCalendar; else if (CFEqual(kCFHebrewCalendar, identifier)) identifier = kCFHebrewCalendar; // else if (CFEqual(kCFChineseCalendar, identifier)) identifier = kCFChineseCalendar; else return NULL; } struct __CFCalendar *calendar = NULL; uint32_t size = sizeof(struct __CFCalendar) - sizeof(CFRuntimeBase); calendar = (struct __CFCalendar *)_CFRuntimeCreateInstance(allocator, CFCalendarGetTypeID(), size, NULL); if (NULL == calendar) { return NULL; } calendar->_identifier = (CFStringRef)CFRetain(identifier); calendar->_locale = NULL; calendar->_localeID = CFLocaleGetIdentifier(CFLocaleGetSystem()); calendar->_tz = CFTimeZoneCopyDefault(); calendar->_cal = NULL; return (CFCalendarRef)calendar; }
void MacLongDateTimeToTimeString(const sint64 &inMacDate, uint32 inLength, void *outData) { // @@@ this code is close, but on the fringe case of a daylight savings time it will be off for a little while CFAbsoluteTime absTime = inMacDate - kCFAbsoluteTimeIntervalSince1904; // Remove local timezone component from absTime CFTimeZoneRef timeZone = CFTimeZoneCopyDefault(); absTime -= CFTimeZoneGetSecondsFromGMT(timeZone, absTime); CFRelease(timeZone); timeZone = CFTimeZoneCreateWithTimeIntervalFromGMT(NULL, 0); CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(absTime, timeZone); CFRelease(timeZone); if (inLength == 16) { sprintf((char *)(outData), "%04d%02d%02d%02d%02d%02dZ", int(date.year % 10000), date.month, date.day, date.hour, date.minute, int(date.second)); } else if (inLength == 14) { /* UTC - 2 year digits - code which parses this assumes that * (2-digit) years between 0 and 49 are in century 21 */ sprintf((char *)(outData), "%02d%02d%02d%02d%02d%02dZ", int(date.year % 100), date.month, date.day, date.hour, date.minute, int(date.second)); } else MacOSError::throwMe(paramErr); }
void GetCurrentMacLongDateTime(sint64 &outMacDate) { CFTimeZoneRef timeZone = CFTimeZoneCopyDefault(); CFAbsoluteTime absTime = CFAbsoluteTimeGetCurrent(); absTime += CFTimeZoneGetSecondsFromGMT(timeZone, absTime); CFRelease(timeZone); outMacDate = sint64(double(absTime + kCFAbsoluteTimeIntervalSince1904)); }
void CFCalendarSetTimeZone(CFCalendarRef calendar, CFTimeZoneRef tz) { CF_OBJC_FUNCDISPATCHV(CFCalendarGetTypeID(), void, calendar, setTimeZone:tz); __CFGenericValidateType(calendar, CFCalendarGetTypeID()); if (tz) __CFGenericValidateType(tz, CFTimeZoneGetTypeID()); if (tz != calendar->_tz) { CFRelease(calendar->_tz); calendar->_tz = tz ? (CFTimeZoneRef)CFRetain(tz) : CFTimeZoneCopyDefault(); if (calendar->_cal) __CFCalendarZapCal(calendar); } }
/** * MacDateTime::DaylightSavingTA * * Determines whether the specified time is affected by * Daylight Saving Time in the current system time zone */ double MacDateTime::DaylightSavingTA(double time) { CFTimeZoneRef tz = CFTimeZoneCopyDefault(); if (tz) { CFAbsoluteTime at = ECMADateToCFAbsoluteTime(time); Boolean isDST = CFTimeZoneIsDaylightSavingTime(tz, at); CFRelease(tz); return (isDST ? kMsecPerHour : 0); } time; // On Macintosh, punt and just report the daylight // saving adjustment currently in effect, rather // than the daylight saving time adjustment for the // exact time passed in. MachineLocation location; ReadMachineLocation(&location); return (location.u.dlsDelta < 0) ? kMsecPerHour : 0; }
dng_time_zone LocalTimeZone (const dng_date_time &dt) { dng_time_zone result; if (dt.IsValid ()) { #if qMacOS CFTimeZoneRef zoneRef = CFTimeZoneCopyDefault (); if (zoneRef) { CFGregorianDate gregDate; gregDate.year = dt.fYear; gregDate.month = dt.fMonth; gregDate.day = dt.fDay; gregDate.hour = dt.fHour; gregDate.minute = dt.fMinute; gregDate.second = dt.fSecond; CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime (gregDate, zoneRef); CFTimeInterval secondsDelta = CFTimeZoneGetSecondsFromGMT (zoneRef, absTime); CFRelease (zoneRef); result.SetOffsetSeconds (secondsDelta); if (result.IsValid ()) { return result; } } #endif #if qWinOS if (GetTimeZoneInformation != NULL && SystemTimeToTzSpecificLocalTime != NULL && SystemTimeToFileTime != NULL) { TIME_ZONE_INFORMATION tzInfo; DWORD x = GetTimeZoneInformation (&tzInfo); SYSTEMTIME localST; memset (&localST, 0, sizeof (localST)); localST.wYear = (WORD) dt.fYear; localST.wMonth = (WORD) dt.fMonth; localST.wDay = (WORD) dt.fDay; localST.wHour = (WORD) dt.fHour; localST.wMinute = (WORD) dt.fMinute; localST.wSecond = (WORD) dt.fSecond; SYSTEMTIME utcST; if (TzSpecificLocalTimeToSystemTime (&tzInfo, &localST, &utcST)) { FILETIME localFT; FILETIME utcFT; (void) SystemTimeToFileTime (&localST, &localFT); (void) SystemTimeToFileTime (&utcST , &utcFT ); uint64 time1 = (((uint64) localFT.dwHighDateTime) << 32) + localFT.dwLowDateTime; uint64 time2 = (((uint64) utcFT .dwHighDateTime) << 32) + utcFT .dwLowDateTime; // FILETIMEs are in units to 100 ns. Convert to seconds. int64 time1Sec = time1 / 10000000; int64 time2Sec = time2 / 10000000; int32 delta = (int32) (time1Sec - time2Sec); result.SetOffsetSeconds (delta); if (result.IsValid ()) { return result; } } } #endif } // Figure out local time zone. dng_date_time_info current_info; CurrentDateTimeAndZone (current_info); result = current_info.TimeZone (); return result; }
/* * Given a CSSM_DATA containing either a UTC-style or "generalized time" * time string, convert to 32-bit Mac time in seconds. * Returns nonzero on error. */ void TimeStringToMacLongDateTime (const CSSM_DATA &inUTCTime, sint64 &outMacDate) { char szTemp[5]; unsigned len; int isUtc; sint32 x; sint32 i; char *cp; CFGregorianDate date; ::memset( &date, 0, sizeof(date) ); if ((inUTCTime.Data == NULL) || (inUTCTime.Length == 0)) { MacOSError::throwMe(paramErr); } /* tolerate NULL terminated or not */ len = inUTCTime.Length; if (inUTCTime.Data[len - 1] == '\0') len--; switch(len) { case UTC_TIME_STRLEN: // 2-digit year, not Y2K compliant isUtc = 1; break; case GENERALIZED_TIME_STRLEN: // 4-digit year isUtc = 0; break; default: // unknown format MacOSError::throwMe(paramErr); } cp = (char *)inUTCTime.Data; /* check that all characters except last are digits */ for(i=0; i<(sint32)(len - 1); i++) { if ( !(isdigit(cp[i])) ) { MacOSError::throwMe(paramErr); } } /* check last character is a 'Z' */ if(cp[len - 1] != 'Z' ) { MacOSError::throwMe(paramErr); } /* YEAR */ szTemp[0] = *cp++; szTemp[1] = *cp++; if(!isUtc) { /* two more digits */ szTemp[2] = *cp++; szTemp[3] = *cp++; szTemp[4] = '\0'; } else { szTemp[2] = '\0'; } x = atoi( szTemp ); if(isUtc) { /* * 2-digit year. * 0 <= year <= 50 : assume century 21 * 50 < year < 70 : illegal per PKIX * 70 < year <= 99 : assume century 20 */ if(x <= 50) { x += 100; } else if(x < 70) { MacOSError::throwMe(paramErr); } /* else century 20, OK */ /* bug fix... we need to end up with a 4-digit year! */ x += 1900; } /* by definition - tm_year is year - 1900 */ //tmp->tm_year = x - 1900; date.year = x; /* MONTH */ szTemp[0] = *cp++; szTemp[1] = *cp++; szTemp[2] = '\0'; x = atoi( szTemp ); /* in the string, months are from 1 to 12 */ if((x > 12) || (x <= 0)) { MacOSError::throwMe(paramErr); } /* in a tm, 0 to 11 */ //tmp->tm_mon = x - 1; date.month = x; /* DAY */ szTemp[0] = *cp++; szTemp[1] = *cp++; szTemp[2] = '\0'; x = atoi( szTemp ); /* 1..31 in both formats */ if((x > 31) || (x <= 0)) { MacOSError::throwMe(paramErr); } //tmp->tm_mday = x; date.day = x; /* HOUR */ szTemp[0] = *cp++; szTemp[1] = *cp++; szTemp[2] = '\0'; x = atoi( szTemp ); if((x > 23) || (x < 0)) { MacOSError::throwMe(paramErr); } //tmp->tm_hour = x; date.hour = x; /* MINUTE */ szTemp[0] = *cp++; szTemp[1] = *cp++; szTemp[2] = '\0'; x = atoi( szTemp ); if((x > 59) || (x < 0)) { MacOSError::throwMe(paramErr); } //tmp->tm_min = x; date.minute = x; /* SECOND */ szTemp[0] = *cp++; szTemp[1] = *cp++; szTemp[2] = '\0'; x = atoi( szTemp ); if((x > 59) || (x < 0)) { MacOSError::throwMe(paramErr); } //tmp->tm_sec = x; date.second = x; CFTimeZoneRef timeZone = CFTimeZoneCreateWithTimeIntervalFromGMT(NULL, 0); CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime(date, timeZone); CFRelease(timeZone); // Adjust abstime to local timezone timeZone = CFTimeZoneCopyDefault(); absTime += CFTimeZoneGetSecondsFromGMT(timeZone, absTime); CFRelease(timeZone); outMacDate = sint64(double(absTime + kCFAbsoluteTimeIntervalSince1904)); }