Skip to content

Commit 88352c9

Browse files
Fix the formatting of the ar-SA culture date in "Working with calendars" (#53877)
* Fix the formatting of the ar-SA culture calendar display * Adjust the example output comment * Briefly mention the fact of setting Calendar for culture change * Do not explicitly set the calendar when changing for ar-SA * Revert commits 152fac4..276462d * Set the encoding to UTF8 in example to display arabic letters * Adjust wording to style guide * Move snippets * Modernize code --------- Co-authored-by: Andy De George (from Dev Box) <adegeo@microsoft.com>
1 parent f950d1b commit 88352c9

6 files changed

Lines changed: 84 additions & 84 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// <ChangeCalendar>
2+
using System.Globalization;
3+
4+
DateTime date1 = new(2011, 6, 20);
5+
6+
Console.OutputEncoding = System.Text.Encoding.UTF8;
7+
DisplayCurrentInfo();
8+
// Display the date using the current culture and calendar.
9+
Console.WriteLine(date1.ToString("d"));
10+
Console.WriteLine();
11+
12+
CultureInfo arSA = CultureInfo.CreateSpecificCulture("ar-SA");
13+
14+
// Change the current culture to Arabic (Saudi Arabia).
15+
CultureInfo.CurrentCulture = arSA;
16+
// Display date and information about the current culture.
17+
DisplayCurrentInfo();
18+
Console.WriteLine(date1.ToString("d"));
19+
Console.WriteLine();
20+
21+
// Change the calendar to Hijri.
22+
Calendar hijri = new HijriCalendar();
23+
if (CalendarExists(arSA, hijri))
24+
{
25+
arSA.DateTimeFormat.Calendar = hijri;
26+
// Display date and information about the current culture.
27+
DisplayCurrentInfo();
28+
Console.WriteLine(date1.ToString("d"));
29+
}
30+
31+
static void DisplayCurrentInfo()
32+
{
33+
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
34+
Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}");
35+
}
36+
37+
static bool CalendarExists(CultureInfo culture, Calendar cal) =>
38+
culture.OptionalCalendars.Any(optional => optional.ToString() == cal.ToString());
39+
// The example displays the following output:
40+
// Current Culture: en-US
41+
// Current Calendar: System.Globalization.GregorianCalendar
42+
// 6/20/2011
43+
//
44+
// Current Culture: ar-SA
45+
// Current Calendar: System.Globalization.UmAlQuraCalendar
46+
// 18‏‏/7‏‏/1432 بعد الهجرة
47+
//
48+
// Current Culture: ar-SA
49+
// Current Calendar: System.Globalization.HijriCalendar
50+
// 19‏‏/7‏‏/1432 بعد الهجرة
51+
// </ChangeCalendar>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<RootNamespace>ChangeCalendar</RootNamespace>
6+
<TargetFramework>net10.0</TargetFramework>
7+
</PropertyGroup>
8+
9+
</Project>

samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb renamed to docs/standard/datetime/snippets/working-with-calendars/vb/Program.vb

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
' Visual Basic .NET Document
2-
Option Strict On
3-
4-
' <Snippet2>
1+
' <ChangeCalendar>
52
Imports System.Globalization
6-
Imports System.Threading
73

84
Module Example
95
Public Sub Main()
106
Dim date1 As Date = #6/20/2011#
117

8+
Console.OutputEncoding = System.Text.Encoding.UTF8
129
DisplayCurrentInfo()
1310
' Display the date using the current culture and calendar.
1411
Console.WriteLine(date1.ToString("d"))
@@ -17,7 +14,7 @@ Module Example
1714
Dim arSA As CultureInfo = CultureInfo.CreateSpecificCulture("ar-SA")
1815

1916
' Change the current culture to Arabic (Saudi Arabia).
20-
Thread.CurrentThread.CurrentCulture = arSA
17+
CultureInfo.CurrentCulture = arSA
2118
' Display date and information about the current culture.
2219
DisplayCurrentInfo()
2320
Console.WriteLine(date1.ToString("d"))
@@ -34,18 +31,12 @@ Module Example
3431
End Sub
3532

3633
Private Sub DisplayCurrentInfo()
37-
Console.WriteLine("Current Culture: {0}",
38-
CultureInfo.CurrentCulture.Name)
39-
Console.WriteLine("Current Calendar: {0}",
40-
DateTimeFormatInfo.CurrentInfo.Calendar)
34+
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}")
35+
Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}")
4136
End Sub
4237

43-
Private Function CalendarExists(ByVal culture As CultureInfo,
44-
cal As Calendar) As Boolean
45-
For Each optionalCalendar As Calendar In culture.OptionalCalendars
46-
If cal.ToString().Equals(optionalCalendar.ToString()) Then Return True
47-
Next
48-
Return False
38+
Private Function CalendarExists(culture As CultureInfo, cal As Calendar) As Boolean
39+
Return culture.OptionalCalendars.Any(Function(optional1) optional1.ToString() = cal.ToString())
4940
End Function
5041
End Module
5142
' The example displays the following output:
@@ -55,9 +46,9 @@ End Module
5546
'
5647
' Current Culture: ar-SA
5748
' Current Calendar: System.Globalization.UmAlQuraCalendar
58-
' 18/07/32
49+
' 18‏‏/7‏‏/1432 بعد الهجرة
5950
'
6051
' Current Culture: ar-SA
6152
' Current Calendar: System.Globalization.HijriCalendar
62-
' 19/07/32
63-
' </Snippet2>
53+
' 19‏‏/7‏‏/1432 بعد الهجرة
54+
' </ChangeCalendar>

docs/standard/datetime/working-with-calendars.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ The calendar currently in use by a particular <xref:System.Globalization.Culture
7171

7272
The following example changes the calendar used by the Arabic (Saudi Arabia) culture. It first instantiates a <xref:System.DateTime> value and displays it using the current culture - which, in this case, is English (United States) - and the current culture's calendar (which, in this case, is the Gregorian calendar). Next, it changes the current culture to Arabic (Saudi Arabia) and displays the date using its default Um Al-Qura calendar. It then calls the `CalendarExists` method to determine whether the Hijri calendar is supported by the Arabic (Saudi Arabia) culture. Because the calendar is supported, it changes the current calendar to Hijri and again displays the date. Note that in each case, the date is displayed using the current culture's current calendar.
7373

74-
[!code-csharp[Conceptual.Calendars#2](../../../samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs#2)]
75-
[!code-vb[Conceptual.Calendars#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR/conceptual.calendars/vb/changecalendar2.vb#2)]
74+
:::code language="csharp" source="./snippets/working-with-calendars/csharp/Program.cs" id="ChangeCalendar":::
75+
:::code language="vb" source="./snippets/working-with-calendars/vb/Program.vb" id="ChangeCalendar":::
76+
77+
Before the example writes to the console, it changes the console's output encoding to UTF-8 so that Arabic letters display correctly.
7678

7779
## Dates and calendars
7880

samples/snippets/csharp/VS_Snippets_CLR/conceptual.calendars/cs/changecalendar2.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)