CFCalendar Reference
| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Declared in | CFCalendar.h |
| Companion guides |
Overview
The CFCalendar opaque type represents a calendar system. The associated API provides information about a calendar and supports calendrical computations such as determining the range of a given calendrical unit and adding units to a given absolute time.
CFAbsoluteTime is the operational lingua franca of CFCalendar—to do calendar arithmetic, you start and end with an absolute time; to convert between a decomposed date in one calendar and another calendar, you first convert to an absolute time. CFAbsoluteTime provides the absolute scale and epoch for dates and times, which can then be rendered into a particular calendar, for calendrical computations or user display.
In a calendar, day, week, weekday, month, and year numbers are generally 1-based, but there may be calendar-specific exceptions. Ordinal numbers, where they occur, are 1-based. Some calendars represented by this API may have to map their basic unit concepts into year/month/week/day/… nomenclature. For example, a calendar composed of 4 quarters in a year instead of 12 months uses the “month” unit to represent quarters. The particular values of the unit are defined by each calendar, and are not necessarily “consistent with” or have a “correspondence with,” values for that unit in another calendar.
Several CFCalendar functions (CFCalendarComposeAbsoluteTime, CFCalendarDecomposeAbsoluteTime, CFCalendarAddComponents, and CFCalendarGetComponentDifference) take a description string that describes the calendrical components provided in a varargs parameter area. You can provide as many components as you need (or choose to), in whatever order you choose. When there is incomplete information to compute an absolute time, default values similar to 0 and 1 are usually chosen by a calendar, but this is a calendar-specific choice. If you provide inconsistent information, calendar-specific disambiguation is performed (which may involve ignoring one or more of the parameters). The characters of the description string specify the units and order of the parameters which follow. The characters are adopted from the corresponding format characters used by CFDateFormatter when possible, as shown in Table 1.
Symbol |
Meaning |
Value Type |
|---|---|---|
y |
year |
int |
M |
month |
int |
d |
day |
int |
H |
hour |
int |
m |
minute |
int |
s |
second |
int |
Information related to formatting dates and times and name-related calendar information is managed by CFDateFormatter.
CFCalendar is subject to some limitations. There is no leap second handling—the existence of leap seconds is ignored as in the other CoreFoundation API. In general, historical accuracy of calendars is not guaranteed. There is currently no API for defining your own calendars.
CFCalendar is “toll-free bridged” with its Cocoa Foundation counterpart, NSCalendar. This means that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object. Therefore, in a method where you see an NSCalendar * parameter, you can pass in a CFCalendarRef, and in a function where you see a CFCalendarRef parameter, you can pass in an NSCalendar instance. See “Toll-Free Bridged Types” for more information on toll-free bridging.
Functions by Task
Creating a Calendar
Calendrical Calculations
-
CFCalendarAddComponents -
CFCalendarComposeAbsoluteTime -
CFCalendarDecomposeAbsoluteTime -
CFCalendarGetComponentDifference
Getting Ranges of Units
-
CFCalendarGetRangeOfUnit -
CFCalendarGetOrdinalityOfUnit -
CFCalendarGetTimeRangeOfUnit -
CFCalendarGetMaximumRangeOfUnit -
CFCalendarGetMinimumRangeOfUnit
Getting and Setting the Time Zone
Getting the Identifier
Getting and Setting the Locale
Getting and Setting Day Information
-
CFCalendarGetFirstWeekday -
CFCalendarSetFirstWeekday -
CFCalendarGetMinimumDaysInFirstWeek -
CFCalendarSetMinimumDaysInFirstWeek
Getting the Type ID
Functions
CFCalendarAddComponents
Computes the absolute time when specified components are added to a given absolute time.
Boolean CFCalendarAddComponents ( CFCalendarRef calendar, CFAbsoluteTime *at, CFOptionFlags options, const unsigned char *componentDesc, ... );
Parameters
- calendar
The calendar to use for the computation.
- at
A reference to an absolute time. On input, points to the absolute time to which components are to be added; on output, points to the result of the computation.
- options
Options for the calculation. For valid values, see “Constants.”
- componentDesc
A string that describes the components provided in the vararg parameters.
- ...
Vararg parameters giving amounts of each calendrical component in the order specified by componentDesc. The amounts to add may be negative, zero, positive, or any combination thereof.
Return Value
TRUE—and in at the computed time—if at falls inside the defined range of the calendar and it is possible to calculate the absolute time when the components (the calendrical components specified by componentDesc and given in the varargs) are added to the input absolute time at; otherwise FALSE.
Discussion
Some operations can be ambiguous, and the behavior of the computation is calendar-specific, but generally components are added in the order specified.
If you specify a “wrap” option (kCFCalendarComponentsWrap), the specified components should be incremented and wrap around to zero/one on overflow, but should not cause higher units to be incremented. When “Wrap” is false, overflow in a unit carries into the higher units, as in typical addition.
Note that some computations can take a relatively long time to perform.
The following example shows how to add 2 months and 3 days to absolute time at‘s current value using an existing calendar (gregorian):
CFCalendarAddComponents(gregorian, &at, 0, "Md", 2, 3); |
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarComposeAbsoluteTime
Computes the absolute time from components in a description string.
Boolean CFCalendarComposeAbsoluteTime ( CFCalendarRef calendar, CFAbsoluteTime *at, const unsigned char *componentDesc, ... );
Parameters
- calendar
The calendar to use for the computation.
- at
Upon return, contains the computed absolute time.
- componentDesc
A string that describes the components provided in the vararg parameters.
- ...
Vararg parameters giving amounts of each calendrical component in the order specified by componentDesc. The amounts to add may be negative, zero, positive, or any combination thereof.
Return Value
TRUE—and in at the absolute time computed from the given components—if the componentDesc description string can be converted into an absolute time, otherwise FALSE. Also returns FALSE for out-of-range values.
Discussion
When there are insufficient components provided to completely specify an absolute time, a calendar uses default values of its choice. When there is inconsistent information, a calendar may ignore some of the parameters or the function may return FALSE. Unnecessary components are ignored (for example, Day takes precedence over Weekday + Weekday ordinal). Note that some computations can take a relatively long time to perform.
The following example shows how to use this function to initialize an absolute time, at, to 6 January 1965 14:10:00, for a given calendar gregorian .
CFCalendarComposeAbsoluteTime(gregorian, &at, "yMdHms", 1965, 1, 6, 14, 10, 00); |
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarCopyCurrent
Returns a copy of the logical calendar for the current user.
CFCalendarRef CFCalendarCopyCurrent ( void );
Return Value
The logical calendar for the current user that is formed from the settings for the current user’s chosen system locale overlaid with any custom settings the user has specified in System Preferences. This function may return a retained cached object, not a new object. Ownership follows the Create Rule.
Discussion
Settings you get from this calendar do not change if user defaults change so that your operations are consistent.
Typically you perform some operations on the returned object and then release it. The returned object may be cached, so you do not need to hold on to it indefinitely.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarCopyLocale
Returns a locale object for a specified calendar.
CFLocaleRef CFCalendarCopyLocale ( CFCalendarRef calendar );
Parameters
- calendar
The calendar to examine.
Return Value
A copy of the locale object for the specified calendar. Ownership follows the Create Rule.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarCopyTimeZone
Returns a time zone object for a specified calendar.
CFTimeZoneRef CFCalendarCopyTimeZone ( CFCalendarRef calendar );
Parameters
- calendar
The calendar to examine.
Return Value
A copy of the time zone object for the specified calendar. Ownership follows the Create Rule.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarCreateWithIdentifier
Returns a calendar object for the calendar identified by a calendar identifier.
CFCalendarRef CFCalendarCreateWithIdentifier ( CFAllocatorRef allocator, CFStringRef identifier );
Parameters
- alloc
The allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.- ident
A calendar identifier. Calendar identifier constants are given in
CFLocaleRef.
Return Value
A calendar object for the calendar identified by ident. If the identifier is unknown (if, for example, it is either an unrecognized string, or the calendar is not supported by the current version of the operating system), returns NULL. Ownership follows the Create Rule.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarDecomposeAbsoluteTime
Computes the components which are indicated by the componentDesc description string for the given absolute time.
Boolean CFCalendarDecomposeAbsoluteTime ( CFCalendarRef calendar, CFAbsoluteTime at, const unsigned char *componentDesc, ... );
Parameters
- calendar
The calendar to use for the computation.
- at
An absolute time.
- componentDesc
A string that describes the components provided in the vararg parameters.
- ...
Vararg pointers to storage for each of the desired components. On successful return, the pointers are filled with values of the corresponding components. The type of all units is
int.
Return Value
TRUE if the function is able to compute the components indicated by the componentDesc description string for the given absolute time, and fills the values to the components given in the varargs. Returns FALSE if the absolute time falls outside the defined range of the calendar, or the computation cannot be performed.
Discussion
The Weekday ordinality, when requested, refers to the next larger (than Week) of the requested units. Some computations can take a relatively long time to perform.
The following example shows how to use this function to determine the current year, month, and day, using an existing calendar (gregorian):
CFCalendarDecomposeAbsoluteTime(gregorian, CFAbsoluteTimeGetCurrent(), "yMd", &year, &month, &day); |
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetComponentDifference
Computes the difference between the two absolute times, in terms of specified calendrical components.
Boolean CFCalendarGetComponentDifference ( CFCalendarRef calendar, CFAbsoluteTime startingAT, CFAbsoluteTime resultAT, CFOptionFlags options, const unsigned char *componentDesc, ... );
Parameters
- calendar
The calendar to use for the computation.
- startingAT
The starting absolute time.
- resultAT
The result absolute time.
- options
Options for the calculation. For valid values, see “Constants.”
- componentDesc
A string that describes the components provided in the vararg parameters.
- ...
Vararg pointers to storage for each of the desired components. On successful return, the pointers are filled with values of the corresponding components. The type of all units is
int.
Return Value
TRUE—and in the varargs the differences—if it is possible to calculate the difference (result - starting) between resultAT and startingAT in terms of the calendrical components specified by componentDesc. Returns FALSE if either absolute time falls outside the defined range of the calendar, or the computation cannot be performed.
Discussion
The result is lossy if there isn’t a small enough unit requested to hold the full precision of the difference. Some operations can be ambiguous, and the behavior of the computation is calendar-specific, but generally larger components will be computed before smaller components; for example, in the Gregorian calendar a result might be 1 month and 5 days, instead of, for example, 0 months and 35 days. The resulting component values may be negative if later is before earlier.
This computation is roughly the inverse of the CFCalendarAddComponents operation, but calendrical arithmetic is invertible only in simple cases. This computation tends to be several times more expensive than the Add operation.
The following example shows how to get the approximate number of days between two absolute times (at1, at2) using an existing calendar (gregorian):
CFCalendarGetComponentDifference(gregorian, at1, at2, 0, "d", &days); |
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetFirstWeekday
Returns the index of first weekday for a specified calendar.
CFIndex CFCalendarGetFirstWeekday ( CFCalendarRef calendar );
Parameters
- calendar
The calendar to examine.
Return Value
The index of the first weekday of the specified calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetIdentifier
Returns the given calendar’s identifier.
CFStringRef CFCalendarGetIdentifier ( CFCalendarRef calendar );
Parameters
- calendar
The calendar to examine.
Return Value
A string representation of calendar’s identifier. Calendar identifier constants can be found in CFLocaleRef. Ownership follows the Get Rule.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetMaximumRangeOfUnit
Returns the maximum range limits of the values that a specified unit can take on in a given calendar.
CFRange CFCalendarGetMaximumRangeOfUnit ( CFCalendarRef calendar, CFCalendarUnit unit );
Parameters
- calendar
The calendar to examine.
- unit
A calendar unit. For valid values see
CFCalendarUnit.
Return Value
The maximum range limits of the values that the specified unit can take on in calendar. For example, in the Gregorian calendar the maximum ranges for the Day unit is 1-31.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetMinimumDaysInFirstWeek
Returns the minimum number of days in the first week of a specified calendar.
CFIndex CFCalendarGetMinimumDaysInFirstWeek ( CFCalendarRef calendar );
Parameters
- calendar
The calendar to examine.
Return Value
The minimum number of days in the first week of calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetMinimumRangeOfUnit
Returns the minimum range limits of the values that a specified unit can take on in a given calendar.
CFRange CFCalendarGetMinimumRangeOfUnit ( CFCalendarRef calendar, CFCalendarUnit unit );
Parameters
- calendar
The calendar to examine.
- unit
A calendar unit. For valid values see
CFCalendarUnit.
Return Value
The minimum range limits of the values that the specified unit can take on in calendar. For example, in the Gregorian calendar the minimum ranges for the Day unit is 1-28.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetOrdinalityOfUnit
Returns the ordinal number of a calendrical unit within a larger unit at a specified absolute time.
CFIndex CFCalendarGetOrdinalityOfUnit ( CFCalendarRef calendar, CFCalendarUnit smallerUnit, CFCalendarUnit biggerUnit, CFAbsoluteTime at );
Parameters
- calendar
The calendar to examine.
- smallerUnit
A calendar unit. For valid values see
CFCalendarUnit.- biggerUnit
A calendar unit. For valid values see
CFCalendarUnit.- at
An absolute time.
Return Value
The ordinal number of the calendar unit specified by smallerUnit within the calendar unit specified by biggerUnit at the absolute time at. For example, the time 00:45 is in the first hour of the day, and for units Hour and Day respectively, the result would be 1.
If the biggerUnit parameter is not logically bigger than the smallerUnit parameter in the calendar, or the given combination of units does not make sense (or is a computation which is undefined), the result is kCFNotFound.
Discussion
The ordinality is in most cases not the same as the decomposed value of the unit. Typically return values are 1 and greater; an exception is the week-in-month calculation, which returns 0 for days before the first week in the month containing the date. Note that some computations can take a relatively long time to perform.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetRangeOfUnit
Returns the range of values that one unit can take on within a larger unit during which a specific absolute time occurs.
CFRange CFCalendarGetRangeOfUnit ( CFCalendarRef calendar, CFCalendarUnit smallerUnit, CFCalendarUnit biggerUnit, CFAbsoluteTime at );
Parameters
- calendar
The calendar to examine.
- smallerUnit
A calendar unit. For valid values see
CFCalendarUnit.- biggerUnit
A calendar unit. For valid values see
CFCalendarUnit.- at
An absolute time.
Return Value
The range of values that the calendar unit specified by smallerUnit can take on within the calendar unit specified by biggerUnit that includes the absolute time at. For example, the range the Day unit can take on in the Month in which the absolute time lies.
If biggerUnit is not logically bigger than smallerUnit in the calendar, or the given combination of units does not make sense (or is a computation which is undefined), the result is {kCFNotFound, kCFNotFound}.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarGetTimeRangeOfUnit
Returns by reference the start time and duration of a given calendar unit that contains a given absolute time.
Boolean CFCalendarGetTimeRangeOfUnit ( CFCalendarRef calendar, CFCalendarUnit unit, CFAbsoluteTime at, CFAbsoluteTime *startp, CFTimeInterval *tip );
Parameters
- calendar
The calendar to examine.
- unit
A calendar unit (for valid values, see
CFCalendarUnit).- at
An absolute time.
- startp
Upon return, contains the beginning of the calendar unit specified by unit that contains the time at.
- tip
Upon return, contains the duration of the calendar unit specified by unit that contains the time at.
Return Value
true if the values of startp and tip could be calculated, otherwise false.
Discussion
The function may fail if, for example, you try to get the range of a kCFCalendarUnitWeekday and specify a time (at) that is during a weekend.
Availability
- Available in OS X v10.5 and later.
Declared In
CFCalendar.hCFCalendarGetTypeID
Returns the type identifier for the CFCalendar opaque type.
CFTypeID CFCalendarGetTypeID ( void );
Return Value
The type identifier for the CFCalendar opaque type.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarSetFirstWeekday
Sets the first weekday for a calendar.
void CFCalendarSetFirstWeekday ( CFCalendarRef calendar, CFIndex wkdy );
Parameters
- calendar
The calendar to modify.
- wkdy
The index to set for the first weekday of calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarSetLocale
Sets the locale for a calendar.
void CFCalendarSetLocale ( CFCalendarRef calendar, CFLocaleRef locale );
Parameters
- calendar
The calendar to modify.
- locale
The locale to set for calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarSetMinimumDaysInFirstWeek
Sets the minimum number of days in the first week of a specified calendar.
void CFCalendarSetMinimumDaysInFirstWeek ( CFCalendarRef calendar, CFIndex mwd );
Parameters
- calendar
The calendar to modify.
- mwd
The number to set as the minimum number of days in the first week of calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hCFCalendarSetTimeZone
Sets the time zone for a calendar.
void CFCalendarSetTimeZone ( CFCalendarRef calendar, CFTimeZoneRef tz );
Parameters
- calendar
The calendar to modify.
- locale
The time zone to set for calendar.
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hData Types
CFCalendarRef
A reference to a CFCalendar object.
typedef const struct __CFCalendar *CFCalendarRef;
Availability
- Available in OS X v10.4 and later.
Declared In
CFCalendar.hConstants
CFCalendarUnit
CFCalendarUnit constants are used to specify calendrical units, such as day or month, in various calendar calculations.
typedef enum {
kCFCalendarUnitEra = (1 << 1),
kCFCalendarUnitYear = (1 << 2),
kCFCalendarUnitMonth = (1 << 3),
kCFCalendarUnitDay = (1 << 4),
kCFCalendarUnitHour = (1 << 5),
kCFCalendarUnitMinute = (1 << 6),
kCFCalendarUnitSecond = (1 << 7),
kCFCalendarUnitWeek = (1 << 8),
kCFCalendarUnitWeekday = (1 << 9),
kCFCalendarUnitWeekdayOrdinal = (1 << 10),
kCFCalendarUnitQuarter = (1UL << 11),
kCFCalendarUnitWeekOfMonth = (1UL << 12),
kCFCalendarUnitWeekOfYear = (1UL << 13),
kCFCalendarUnitYearForWeekOfYear = (1UL << 14),
} CFCalendarUnit;
Constants
kCFCalendarUnitEraSpecifies the era unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitYearSpecifies the year unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitMonthSpecifies the month unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitDaySpecifies the day unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitHourSpecifies the hour unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitMinuteSpecifies the minute unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitSecondSpecifies the second unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitWeekSpecifies the week unit.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitWeekdaySpecifies the weekday unit.
The weekday units are the numbers
1-N(where for the Gregorian calendarN=7and1is Sunday).Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitWeekdayOrdinalSpecifies the ordinal weekday unit.
The weekday ordinal unit describes ordinal position within the month unit of the corresponding weekday unit. For example, in the Gregorian calendar a weekday ordinal unit of
2for a weekday unit3indicates "the second Tuesday in the month".Available in OS X v10.4 and later.
Declared in
CFCalendar.h.kCFCalendarUnitQuarterSpecifies the quarter-year unit.
Available in OS X v10.6 and later.
Declared in
CFCalendar.h.kCFCalendarUnitWeekOfMonthSpecifies the original week of a month calendar unit.
Available in OS X v10.7 and later.
Declared in
CFCalendar.h.kCFCalendarUnitWeekOfYearSpecifies the original week of the year calendar unit.
Available in OS X v10.7 and later.
Declared in
CFCalendar.h.kCFCalendarUnitYearForWeekOfYearSpecifies the relative year for a week within a year calendar unit.
Available in OS X v10.7 and later.
Declared in
CFCalendar.h.
Component Wrapping Options
The wrapping option specifies overflow behavior for calendar components in calendrical calculations—see CFCalendarAddComponents and CFCalendarGetComponentDifference.
enum {
kCFCalendarComponentsWrap = (1 << 0)
}
Constants
kCFCalendarComponentsWrapSpecifies that the components specified for calendar components should be incremented and wrap around to zero/one on overflow, but should not cause higher units to be incremented.
Available in OS X v10.4 and later.
Declared in
CFCalendar.h.
© 2004, 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-10-12)