Creating a date and time formatter

The Format and Scan functions of TDateTimeFormatter derived classes call these protected member functions to perform the conversion between text data and the time fields in the calendar:

TextToTime converts a text string to data for calendar fields. This function is called by the Scan function.

TimeToText converts the data in the calendar fields to a text string. This function is called by the Format function.

If you override TPatternBasedDateTimeFormatter, you can probably use the TextToTime and TimeToText implementations provided by this class.

However, these functions perform conversions based on a specific calendar. You must override the functions that set and return the calendar used by your formatter. GetCalendar is called by the Format and Scan functions to retrieve the calendar data member, which is then passed to the TimeToText or TextToTime function. The calendar data member for each formatter is initialized by the constructor, but it can also be set or reset using the SetCalendar function.

Creating a new calendar

If your TDateTimeFormatter uses a calendar other than the Gregorian calendar, you need to create a subclass of TCalendar. You must override the functions responsible for conversion between time and calendar fields.


DateToDays performs the opposite conversion from the calendar fields to the number of days from the base date.

DaysToDate converts the number of days from the base date (March 1, 2000 A.D.) into the era, year, month, and day fields in the calendar.

Unless the new calendaring system also expresses hours, minutes, and seconds differently, you do not need to override the functions for computing these fields (SecondsToDate and DateToSeconds).

You must also override the TCalendar member functions that enforce synchronization whenever a field is changed:

BoundFieldToLimit adjusts the value of a field to the closest limit--for example, adjusting the day value for April 31 to April 30.

ClearConflictingFields clears any unrelated fields that might conflict with a field you have just set or rolled. Values for these fields are then reset based on the new value of the changed field.

IsValidDate checks whether the values in all the calendar fields are consistent with each other when you set an individual field.

ResolveConflictingFields resolves any conflicting values based on the field priorities of the conflicting fields. The lowest priority field is changed first. Fields in the Gregorian calendar are prioritized as follows:

0 = kEra 4 = kDayInYear 8 = kMinuteInHour
1 = kYearInEra 5 = kDayInWeek 9 = kSecondInMinute
2 = kMonthInYear 6 = kWeekInYear 10 = kHalfDayInDay
3 = kDayInMonth 7 = kHourInDay 11 = kHourInHalfDay


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker