Skip to content

Commit 3e7d0ac

Browse files
authored
TimeZoneInfo.AdjustmentRule F# snippets (#7915)
1 parent dd55b06 commit 3e7d0ac

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// <Snippet1>
2+
open System
3+
open System.Globalization
4+
5+
let timeZones = TimeZoneInfo.GetSystemTimeZones()
6+
let dateInfo = CultureInfo.CurrentCulture.DateTimeFormat
7+
8+
type WeekOfMonth =
9+
| First = 1
10+
| Second = 2
11+
| Third = 3
12+
| Fourth = 4
13+
| Last = 5
14+
15+
for zone in timeZones do
16+
printfn $"{zone.StandardName} transition time information:"
17+
printfn " Time zone information: "
18+
printfn $" Base UTC Offset: {zone.BaseUtcOffset}"
19+
printfn $" Supports DST: {zone.SupportsDaylightSavingTime}"
20+
21+
let adjustmentRules= zone.GetAdjustmentRules()
22+
23+
// Indicate that time zone has no adjustment rules
24+
if adjustmentRules.Length = 0 then
25+
printfn " No adjustment rules defined."
26+
else
27+
printfn $" Adjustment Rules: {adjustmentRules.Length}"
28+
// Iterate adjustment rules
29+
for adjustmentRule in adjustmentRules do
30+
printfn $" Adjustment rule from {adjustmentRule.DateStart:d} to {adjustmentRule.DateEnd:d}:"
31+
printfn $" Delta: {adjustmentRule.DaylightDelta}"
32+
// Get start of transition
33+
let daylightStart = adjustmentRule.DaylightTransitionStart
34+
// Display information on floating date rule
35+
if not daylightStart.IsFixedDateRule then
36+
printfn $" Begins at {daylightStart.TimeOfDay:t} on the {enum<WeekOfMonth> daylightStart.Week} {daylightStart.DayOfWeek} of {dateInfo.GetMonthName daylightStart.Month}"
37+
// Display information on fixed date rule
38+
else
39+
printfn $" Begins at {daylightStart.TimeOfDay:t} on {dateInfo.GetMonthName daylightStart.Month} {daylightStart.Day}"
40+
41+
// Get end of transition.
42+
let daylightEnd = adjustmentRule.DaylightTransitionEnd
43+
// Display information on floating date rule.
44+
if not daylightEnd.IsFixedDateRule then
45+
printfn $" Ends at {daylightEnd.TimeOfDay:t} on the {enum<WeekOfMonth> daylightEnd.Week} {daylightEnd.DayOfWeek} of {dateInfo.GetMonthName daylightEnd.Month}"
46+
47+
// Display information on fixed date rule.
48+
else
49+
printfn $" Ends at {daylightEnd.TimeOfDay:t} on {dateInfo.GetMonthName daylightEnd.Month} {daylightEnd.Day}"
50+
51+
// A portion of the output from the example might appear as follows:
52+
// Tonga Standard Time transition time information:
53+
// Time zone information:
54+
// Base UTC Offset: 13:00:00
55+
// Supports DST: False
56+
// No adjustment rules defined.
57+
// Samoa Standard Time transition time information:
58+
// Time zone information:
59+
// Base UTC Offset: 13:00:00
60+
// Supports DST: True
61+
// Adjustment Rules: 4
62+
// Adjustment rule from 1/1/0001 to 12/31/2009:
63+
// Delta: 00:00:00
64+
// Begins at 12:00 AM on January 1
65+
// Ends at 12:00 AM on January 1
66+
// Adjustment rule from 1/1/2010 to 12/31/2010:
67+
// Delta: 01:00:00
68+
// Begins at 11:59 PM on the Last Saturday of September
69+
// Ends at 12:00 AM on the First Friday of January
70+
// Adjustment rule from 1/1/2011 to 12/31/2011:
71+
// Delta: 01:00:00
72+
// Begins at 3:00 AM on the Fourth Saturday of September
73+
// Ends at 4:00 AM on the First Saturday of April
74+
// Adjustment rule from 1/1/2012 to 12/31/9999:
75+
// Delta: 01:00:00
76+
// Begins at 12:00 AM on the Last Sunday of September
77+
// Ends at 1:00 AM on the First Sunday of April
78+
// Line Islands Standard Time transition time information:
79+
// Time zone information:
80+
// Base UTC Offset: 14:00:00
81+
// Supports DST: False
82+
// No adjustment rules defined.
83+
// </Snippet1>
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+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="DateStart1.fs" />
9+
</ItemGroup>
10+
</Project>

xml/System/TimeZoneInfo+AdjustmentRule.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
The following example retrieves all time zones defined on the local system and displays complete information about their adjustment rules.
8686
8787
:::code language="csharp" source="~/snippets/csharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.cs" id="Snippet3":::
88+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.fs" id="Snippet3":::
8889
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.TimeZone2.AdjustmentRule.Class/vb/System.TimeZone2.AdjustmentRule.Class.vb" id="Snippet3":::
8990
9091
The following is a small portion of the output that is generated by the example. The exact output will vary depending on the operating system and the date on which the example is run.
@@ -253,6 +254,7 @@ dateVariable.Date
253254
The following example creates an alternate Central Standard Time zone and defines three adjustment rules for the periods 1976-1986, 1987-2006, and 2007 and beyond. These rules are added to a generic <xref:System.Collections.Generic.List%601> object whose elements are then copied to a <xref:System.TimeZoneInfo.AdjustmentRule> array. This array is then used in the call to the <xref:System.TimeZoneInfo.CreateCustomTimeZone%28System.String%2CSystem.TimeSpan%2CSystem.String%2CSystem.String%2CSystem.String%2CSystem.TimeZoneInfo.AdjustmentRule%5B%5D%29?displayProperty=nameWithType> method.
254255
255256
:::code language="csharp" source="~/snippets/csharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.cs" id="Snippet1":::
257+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.fs" id="Snippet1":::
256258
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.TimeZone2.AdjustmentRule.Class/vb/System.TimeZone2.AdjustmentRule.Class.vb" id="Snippet1":::
257259
258260
]]></format>
@@ -380,6 +382,7 @@ dateVariable.Date
380382
The following example displays information about all of the time zones defined in the local computer's system registry, including the starting and ending dates of their adjustment rules.
381383
382384
:::code language="csharp" source="~/snippets/csharp/System/TimeZoneInfo+AdjustmentRule/DateEnd/DateStart1.cs" id="Snippet1":::
385+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeZoneInfo+AdjustmentRule/DateEnd/DateStart1.fs" id="Snippet1":::
383386
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TimeZoneInfo.AdjustmentRule.DateStart/vb/DateStart1.vb" id="Snippet1":::
384387
385388
]]></format>
@@ -448,6 +451,7 @@ dateVariable.Date
448451
The following example displays information about all of the time zones defined in the local computer's system registry, including the starting and ending dates of their adjustment rules.
449452
450453
:::code language="csharp" source="~/snippets/csharp/System/TimeZoneInfo+AdjustmentRule/DateEnd/DateStart1.cs" id="Snippet1":::
454+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeZoneInfo+AdjustmentRule/DateEnd/DateStart1.fs" id="Snippet1":::
451455
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/System.TimeZoneInfo.AdjustmentRule.DateStart/vb/DateStart1.vb" id="Snippet1":::
452456
453457
]]></format>
@@ -704,6 +708,7 @@ TimeZoneTime = BaseUtcOffset + DaylightDelta + UtcTime
704708
The following example calls the <xref:System.TimeZoneInfo.AdjustmentRule.Equals%28System.TimeZoneInfo.AdjustmentRule%29?displayProperty=nameWithType> method to compare the adjustment rules for Central Standard Time with those for Canada Central Standard Time and Mexico Standard Time.
705709
706710
:::code language="csharp" source="~/snippets/csharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.cs" id="Snippet2":::
711+
:::code language="fsharp" source="~/snippets/fsharp/System/TimeZoneInfo+AdjustmentRule/Overview/System.TimeZone2.AdjustmentRule.Class.fs" id="Snippet2":::
707712
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.TimeZone2.AdjustmentRule.Class/vb/System.TimeZone2.AdjustmentRule.Class.vb" id="Snippet2":::
708713
709714
This code displays the following output to the console:

0 commit comments

Comments
 (0)