TDateTimeFormatter protocol

TDateTimeFormatter provides the abstract protocol for all date and time formatters. Each class derived from TDateTimeFormatter is designed to work with only a single calendaring system (for example, the Gregorian calendar). Within a calendaring system, you create different instances of TDateTimeFormatter to produce different date formats--for example, a full date (January 1, 1999) or a numeric date (1-1-99).

Each TDateTimeFormatter instance has a localizable name for use with the locale mechanism. You can use the date and time formatter from the current locale to format time in a meaningful format. The TLocale class provides identifiers for a default time format, a full date format, an abbreviated date format, and a numeric date format. See "Predefined locale items" on page 265 for more information.

This sample code shows how to format the current time using the full date formatter from the current locale:

      // Get the correct date formatter from the current locale.
      TLocale currentLocale = TLocale::GetCurrentLocale();
      TLocaleItem<TDateTimeFormatter> item;
      TDeleterFor<TDateTimeFormatter> dateFormatter = 
                  item.CopyItem( TLocale::kFullDateFormatID, currentLocale );
      
      // Get the current time.
      TSeconds currentTime;
      TDateTimeClock().Now( currentTime );
      
      // Use the formatter.
      TStandardText formattedDate;
      TFormatResult result;
      
      dateFormatter->Format( TFormattableTime(currentTime), formattedDate, result );
TDateTimeFormatter also provides interfaces for controlling specific formatting options:

Using a pattern-based formatter

The abstract class TPatternBasedDateTimeFormatter, derived from TDateTimeFormatter, provides the basic protocol for date and time formatters that use patterns to scan and format date and time information.

TGregorianDateTimeFormatter is a pattern-based formatter. This type of formatter uses a specific pattern, encapsulated by a TDateTimePattern instance, as a template for formatting and scanning. A pattern-based formatter also supports flexible scanning. You can specify more than one TDateTimePattern instance for use during scanning, and the formatter returns the pattern that produces the best results.

TDateTimePattern derives from TParameterFormatter (see "Using TParameterFormatter" on page 219). You create a TDateTimePattern the same way you create a TParameterFormatter:

  1. Create a text template and attach it to the TDateTimePattern with the SetTemplate function.
  2. Use the SetDateTimeFieldFormat function to identify each variable range, setting each range to a particular date or time field type, and attaching a formatter.
  3. Use the pattern by attaching it to the formatter with the TPatternBasedDateTimeFormatter::AdoptPattern member function.
The pattern-based formatter formats each field accordingly, returning a single text string representing the entire formatted date. For example, you might create the text template month-day-year, hour:min:ampm, producing the output 1-26-99, 12:12 pm.

This mechanism lets you individually set the formatter for each field; typically this will be a number formatter. You might also use a TChoiceFormatter to provide text-number mappings--for example, mapping the kMonthInYear field to the month names.

NOTE In a future release, the system will provide an editor for creating and modifying date and time patterns.

You can also use the TAlternateTextMatch mechanism to extend the scanning function. For example, you might want to scan either the hyphen or the slash as a date separator.


The following sample code shows how to create a pattern-based date formatter and attach an alternate text match:

      TGregorianDateTimeFormatter dateFormatter;
      TDateTimePattern* datePattern = new TDateTimePattern;
      TPositionalNumberFormatter dateNumberFormatter;
      TFormattableTime date;
      TScanResult dateScanResult;
      TAlternateTextMatch dateAlternateTextMatch;
      
      // Create the template for the pattern; for example, 1/1/1999
      TStandardText textTemplate("<month>/<day>/<year>");
      dateFormatter.SetTemplate(textTemplate);
      
      // Define the fields and attach formatters.
      datePattern->SetDateTimeFieldFormat(TTextRange(TTextOffset(0),TTextOffset(7)),
                                          0, kMonthInYear, dateNumberFormatter);
      datePattern->SetDateTimeFieldFormat(TTextRange(TTextOffset(8),TTextOffset(13)),
                                          0, kDayInMonth, dateNumberFormatter);
      datePattern->SetDateTimeFieldFormat(TTextRange(TTextOffset(14),TTextOffset(20)),
                                          0, kYearInEra, dateNumberFormatter);
      
      // Attach the pattern to the date formatter.
      dateFormatter->AdoptPattern(datePattern);
      
      // Add an alternate text match so that the pattern 1-1-1999 scans in.
      dateAlternateTextMatch.AddAlternateText(TStandardText("-"));
      dateFormatter.SetTextMatch(TTextRange(TTextOffset(7),TTextOffset(8)),
                                  dateAlternateTextMatch);
      dateFormatter.SetTextMatch(TTextRange(TTextOffset(13),TTextOffset(14)),
                                  dateAlternateTextMatch);
      
      // Perform the scanning operation.
      TStandardText scanText("1-1-1999");
      dateFormatter.Scan(scanText,TTextRange::GetMaximumRange(),date,dateScanResult);

Calendars

TCalendar provides the basic protocol for all calendar classes. A calendar class is used by a TDateTimeFormatter to map a local time to text fields representing aspects of time and date information.

Each TCalendar instance has a localizable name for use with the locale mechanism. When you need the current TCalendar instance, you can get it from the current locale using the identifier TLocale::kCalendarID.

Calendar fields

Each calendar contains a specific set of date and time fields, enumerated by TCalendar::EDateTimeFieldType, with a range of possible values. TCalendar allows the upper limit of the range to have a minimum and maximum value, providing for fields whose upper limit can vary. For example, the maximum number of days in a month varies depending on the specific month.

This table lists the TCalendar fields, along with the values that appear in the U. S. Gregorian calendar.

Field Minimum value Lower maximum value Upper maximum value Value for date 12:12:12 p.m. January 1, 1999 A.D.
kEra 0 (B.C.) 1 (A.D.) 1 1
kYearInEra 1 5,000,000 5,000,000 1999
kMonthInYear 1
(January)
12 (December) 12 1
kDayInMonth 1 28 31 1
kHourInDay 0 23 23 12
kMinuteInHour 0 59 59 12
kSecondInMinute 0 59 59 12
kHalfDayInDay 0 (A.M.) 1 (P.M.) 1 1
kHourInHalfDay 0 11 11 0
kWeekInYear 1 53 54 1
kDayInWeek 0 (Sunday) 6 (Saturday) 6 5 (Friday)
kDayInYear 1 365 366 1

Changing calendar fields

TCalendar includes member functions that enforce consistent, valid results when you change the value of individual fields. You can change a field in one of three ways:

This table compares the results of the TCalendar field modification functions. The first argument indicates the field to modify. For the SetField function, the second argument indicates the value to set that field to; for the RollField and ShiftField functions, the second argument indicates the value by which to increment or decrement the current value of that field.

Operation Result (Current date: 6-26-1999)
SetField(kMonthInYear,1) 1-26-1999
SetField(kMonthInYear,13) 1-26-1999
RollField(kMonthInYear,1) 7-26-1999
RollField(kMonthInYear,7) 1-26-1999
ShiftField(kMonthInYear, 1) 7-26-1999
ShiftField(kMonthInYear,7) 1-26-2000

Time zones

TTimeZone provides the member functions used by calendars and formatters to map the system time, UTC time, to the local time zone. TTimeZone encapsulates a local time zone in terms of an offset from UTC, based on Greenwich Mean Time. You construct a new TTimeZone by specifying the correct offset in an instance of TTime.

Each TTimeZone instance can have a localizable name that can be used with the locale mechanism. When you need the current TTimeZone instance, you can get it from the current locale using the identifier TLocale::kTimeZoneID.

The TTimeZone class does not support using a single TTimeZone instance to represent standard and daylight savings time for the same zone. Daylight savings time for a region can be represented by a second TTimeZone instance. For example, you might have two TTimeZone instances for the Pacific time zone, one for Pacific Standard Time and one for Pacific Daylight Time.


[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