< C++ .NET Formatting 6 | Index #1 | Index # 2 | C++ .NET Formatting 8 >


 

 

Playing With C++ .NET Formatting 7

 

 

The following are the topics available in this part.

  1. Custom DateTime Format Strings...continue

  2. Enumeration Format Strings

 

 

The following code example shows that DateTimeFormatInfo ignores the punctuation in the era name, only if the calendar is Gregorian and the culture uses the era name "A.D.".

// testprog.cpp : main project file.

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Globalization;

using namespace System::Collections;

 

int main()

{

      // Creates strings with punctuation and without.

      String^ strADPunc = "A.D.";

      String^ strADNoPunc = "AD";

      String^ strCEPunc = "C.E.";

      String^ strCENoPunc = "CE";

 

      // Calls DTFI::GetEra for each culture that uses GregorianCalendar

      // as the default calendar.

      Console::WriteLine("            ----- AD -----  ----- CE -----");

      Console::WriteLine("CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR");

      IEnumerator^ en = CultureInfo::GetCultures(CultureTypes::SpecificCultures)->GetEnumerator();

 

      while (en->MoveNext())

      {

         CultureInfo^ myCI = safe_cast<CultureInfo^>(en->Current);

         Console::Write("{0, -12}", myCI);

         Console::Write("{0,-7}{1,-9}", myCI->DateTimeFormat->GetEra(strADPunc), myCI->DateTimeFormat->GetEra(strADNoPunc));

         Console::Write("{0, -7}{1, -9}", myCI->DateTimeFormat->GetEra(strCEPunc), myCI->DateTimeFormat->GetEra(strCENoPunc));

         Console::Write("{0}", myCI->Calendar);

         Console::WriteLine();

      }

      return 0;

}

 

Output:

            ----- AD -----  ----- CE -----

CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR

ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar

bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar

ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar

zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar

he-IL       -1     -1       -1     -1       System.Globalization.GregorianCalendar

hu-HU       -1     -1       -1     -1       System.Globalization.GregorianCalendar

is-IS       1      1        -1     -1       System.Globalization.GregorianCalendar

it-IT       -1     -1       -1     -1       System.Globalization.GregorianCalendar

[trimmed]

sk-SK       -1     -1       -1     -1       System.Globalization.GregorianCalendar

sq-AL       1      1        -1     -1       System.Globalization.GregorianCalendar

sv-SE       1      1        -1     -1       System.Globalization.GregorianCalendar

th-TH       -1     -1       -1     -1       System.Globalization.ThaiBuddhistCalendar

tr-TR       1      1        -1     -1       System.Globalization.GregorianCalendar

[trimmed]

pa-IN       1      1        -1     -1       System.Globalization.GregorianCalendar

gu-IN       1      1        -1     -1       System.Globalization.GregorianCalendar

syr-SY      1      1        -1     -1       System.Globalization.GregorianCalendar

div-MV      -1     -1       -1     -1       System.Globalization.HijriCalendar

ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar

zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar

de-CH       -1     -1       -1     -1       System.Globalization.GregorianCalendar

[trimmed]

sv-FI       1      1        -1     -1       System.Globalization.GregorianCalendar

az-AZ-Cyrl  1      1        -1     -1       System.Globalization.GregorianCalendar

es-PR       -1     -1       -1     -1       System.Globalization.GregorianCalendar

[trimmed]

sma-NO      1      1        -1     -1       System.Globalization.GregorianCalendar

sr-BA-Cyrl  1      1        -1     -1       System.Globalization.GregorianCalendar

zu-ZA       1      1        -1     -1       System.Globalization.GregorianCalendar

smj-SE      1      1        -1     -1       System.Globalization.GregorianCalendar

ns-ZA       1      1        -1     -1       System.Globalization.GregorianCalendar

quz-BO      1      1        -1     -1       System.Globalization.GregorianCalendar

se-NO       1      1        -1     -1       System.Globalization.GregorianCalendar

mt-MT       1      1        -1     -1       System.Globalization.GregorianCalendar

Press any key to continue . . .

The following code example creates several TimeSpan objects and displays the properties of each.

// testprog.cpp : main project file.

// Example of the TimeSpan class properties.

#include "stdafx.h"

 

using namespace System;

 

// Display the properties of the TimeSpan parameter.

static void ShowTimeSpanProperties( TimeSpan interval )

{

   Object^ null = nullptr;

   String^ dataFmt = "{0,-12}{1,8}       {2,-18}{3,21}";

   Console::WriteLine("{0,21}", interval);

   Console::WriteLine(dataFmt, "Days", interval.Days, "TotalDays", interval.TotalDays);

   Console::WriteLine(dataFmt, "Hours", interval.Hours, "TotalHours", interval.TotalHours);

   Console::WriteLine(dataFmt, "Minutes", interval.Minutes, "TotalMinutes", interval.TotalMinutes);

   Console::WriteLine(dataFmt, "Seconds", interval.Seconds, "TotalSeconds", interval.TotalSeconds);

   Console::WriteLine(dataFmt, "Milliseconds", interval.Milliseconds, "TotalMilliseconds", interval.TotalMilliseconds);

   Console::WriteLine(dataFmt, null, null, "Ticks", interval.Ticks);

}

 

int main()

{

      String^ headerFmt = "\n{0,-45}";

      Console::WriteLine("This example of the TimeSpan class properties "

            "generates the \nfollowing output. It "

            "creates several TimeSpan objects and \ndisplays "

            "the values of the TimeSpan properties for each.");

      // Create and display a TimeSpan value of 1 tick.

      Console::Write(headerFmt, "TimeSpan(1)");

      ShowTimeSpanProperties(TimeSpan(1));

      // Create a TimeSpan value with a large number of ticks.

      Console::Write(headerFmt, "TimeSpan(111222333444555 )");

      ShowTimeSpanProperties(TimeSpan(111222333444555));

      // This TimeSpan has all fields specified.

      Console::Write(headerFmt, "TimeSpan(10, 20, 30, 40, 50)");

      ShowTimeSpanProperties(TimeSpan(10,20,30,40,50));

      // This TimeSpan has all fields overflowing.

      Console::Write(headerFmt, "TimeSpan(1111, 2222, 3333, 4444, 5555)");

      ShowTimeSpanProperties(TimeSpan(1111,2222,3333,4444,5555));

      // This TimeSpan is based on a number of days.

      Console::Write(headerFmt, "FromDays(20.84745602)");

      ShowTimeSpanProperties(TimeSpan::FromDays(20.84745602));

 

      return 0;

}

 

Output:

 

 

 

This example of the TimeSpan class properties generates the

following output. It creates several TimeSpan objects and

displays the values of the TimeSpan properties for each.

 

TimeSpan(1)                                       00:00:00.0000001

Days               0       TotalDays          1.15740740740741E-12

Hours              0       TotalHours         2.77777777777778E-11

Minutes            0       TotalMinutes       1.66666666666667E-09

Seconds            0       TotalSeconds                      1E-07

Milliseconds       0       TotalMilliseconds                0.0001

                           Ticks                                 1

 

TimeSpan(111222333444555 )                    128.17:30:33.3444555

Days             128       TotalDays              128.729552597865

Hours             17       TotalHours             3089.50926234875

Minutes           30       TotalMinutes           185370.555740925

Seconds           33       TotalSeconds           11122233.3444555

Milliseconds     344       TotalMilliseconds      11122233344.4555

                           Ticks                   111222333444555

 

TimeSpan(10, 20, 30, 40, 50)                   10.20:30:40.0500000

Days              10       TotalDays              10.8546302083333

Hours             20       TotalHours                   260.511125

Minutes           30       TotalMinutes                 15630.6675

Seconds           40       TotalSeconds                  937840.05

Milliseconds      50       TotalMilliseconds             937840050

                           Ticks                     9378400500000

 

TimeSpan(1111, 2222, 3333, 4444, 5555)       1205.22:47:09.5550000

Days            1205       TotalDays              1205.94941614583

Hours             22       TotalHours                28942.7859875

Minutes           47       TotalMinutes              1736567.15925

Seconds            9       TotalSeconds              104194029.555

Milliseconds     555       TotalMilliseconds          104194029555

                           Ticks                  1041940295550000

 

FromDays(20.84745602)                          20.20:20:20.2000000

Days              20       TotalDays              20.8474560185185

Hours             20       TotalHours             500.338944444444

Minutes           20       TotalMinutes           30020.3366666667

Seconds           20       TotalSeconds                  1801220.2

Milliseconds     200       TotalMilliseconds            1801220200

                           Ticks                    18012202000000

Press any key to continue . . .

 

Enumeration Format Strings

 

You can use the ToString method to create a new string object that represents the numeric, hexadecimal, or string value of an Enum. This method takes one of the enumeration formatting strings to specify the value that you want returned. The following table lists the enumeration formatting strings and the values they return. These format specifiers are not case-sensitive.

 

Format string

Result

G or g

Displays the enumeration entry as a string value, if possible, and otherwise displays the integer value of the current instance. If the enumeration is defined with the Flags attribute set, the string values of each valid entry are concatenated together, separated by commas. If the Flags attribute is not set, an invalid value is displayed as a numeric entry.

F or f

Displays the enumeration entry as a string value, if possible. If the value can be completely displayed as a summation of the entries in the enumeration (even if the Flags attribute is not present), the string values of each valid entry are concatenated together, separated by commas. If the value cannot be completely determined by the enumeration entries, then the value is formatted as the integer value.

D or d

Displays the enumeration entry as an integer value in the shortest representation possible.

X or x

Displays the enumeration entry as a hexadecimal value. The value is represented with leading zeros as necessary, to ensure that the value is a minimum eight digits in length.

 

Table 7

 

The following example defines an enumeration called Colors that consists of three entries: Red, Blue, and Green.

enum class Colors

{

   Red = 1,

   Blue = 2,

   Green = 3

};

After the enumeration is defined, an instance can be declared in the following manner.

Colors MyColors = Colors::Green;

The following example uses the enumeration formatting methods to assign string, numeric, and hexadecimal representations of a DayOfWeek enumeration to the string MyString. This code creates a new instance of the DayOfWeek enumeration called MyDays and assigns it the value Friday. Next, it uses the "G", "F", "D", and "X" formatting strings to assign the different enumeration representations to MyString.

// testprog.cpp : main project file.

// Example of the DayOfWeek enumeration

#include "stdafx.h"

 

using namespace System;

 

int main()

{

      DayOfWeek MyDays = DayOfWeek::Friday;

 

      String^ MyString = MyDays.ToString("G");

      // In the U.S. English culture, MyString has the value: "Friday".

      Console::WriteLine("G - {0}", MyString);

      MyString = MyDays.ToString("F");

      // In the U.S. English culture, MyString has the value: "Friday".

      Console::WriteLine("F - {0}", MyString);

      MyString = MyDays.ToString("D");

      // In the U.S. English culture, MyString has the value: "5".

      Console::WriteLine("D - {0}", MyString);

      MyString = MyDays.ToString("X");

      // In the U.S. English culture, MyString has the value: "00000005".

      Console::WriteLine("X - {0}", MyString);

 

      return 0;

}

 

Output:

C++ .NET formatting - console program output example of the DayOfWeek enumeration

 

DayOfWeek enumeration specifies the day of the week. The member of the enum is listed in the following Table.

 

Member name

Description

Symbol

Public method

Friday

Indicates Friday.

Monday

Indicates Monday.

Saturday

Indicates Saturday.

Sunday

Indicates Sunday.

Thursday

Indicates Thursday.

Tuesday

Indicates Tuesday.

Wednesday

Indicates Wednesday.

 

Table 8

 

The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. This enumeration ranges from zero, indicating Sunday, to six, indicating Saturday. This enumeration is useful when it is desirable to have a strongly typed specification of the day of the week. For example, this enumeration is the type of the property value for the DateTime.DayOfWeek property. The following example demonstrates the DateTime.DayOfWeek property and the DayOfWeek enumeration.

// testprog.cpp : main project file.

// This example demonstrates the DateTime.DayOfWeek property

#include "stdafx.h"

 

using namespace System;

 

int main()

{

  // Assume the current culture is en-US.

  // Create a DateTime for the tenth of March, 2007.

  DateTime dt = DateTime(2007,3,10);

  Console::WriteLine("Is Thursday the day of the week for {0:d}?: {1}", dt, dt.DayOfWeek == DayOfWeek::Thursday);

  Console::WriteLine("The day of the week for {0:d} is {1}.", dt, dt.DayOfWeek);

  return 0;

}

 

Output:

C++ .Net formatting - code example that demonstrates the DateTime.DayOfWeek property

 

 

 

 

 

Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7 | Part 8 | Part 9


< C++ .NET Formatting 6 | Index #1 | Index # 2 | C++ .NET Formatting 8 >