Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.TimeZoneInfo.TransitionTime F# snippets #7916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
module Program

open System
open System.Globalization

let compareForEquality () =
// <Snippet1>
let tt1 = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 02, 00, 00), 11, 03)
let tt2 = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 02, 00, 00), 11, 03)
let tt3 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 02, 00, 00), 10, 05, DayOfWeek.Sunday)
let tz = TimeZoneInfo.Local
printfn $"{tt1.Equals tz}" // Returns False (overload with argument of type Object)
printfn $"{tt1.Equals tt1}" // Returns True (an object always equals itself)
printfn $"{tt1.Equals tt2}" // Returns True (identical property values)
printfn $"{tt1.Equals tt3}" // Returns False (different property values)
// </Snippet1>

let compareTransitionTimesForEquality () =
// <Snippet7>
let tt1 = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 02, 00, 00), 11, 03)
let tt2 = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 02, 00, 00), 11, 03)
let tt3 = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 02, 00, 00), 10, 05, DayOfWeek.Sunday)
printfn $"{tt1.Equals tt1}" // Returns True (an object always equals itself)
printfn $"{tt1.Equals tt2}" // Returns True (identical property values)
printfn $"{tt1.Equals tt3}" // Returns False (different property values)
// </Snippet7>

let createTransitionRules () =
// <Snippet2>
let delta = TimeSpan(1, 0, 0)
let adjustmentList = ResizeArray()

// Define a fictitious new time zone consisting of fixed and floating adjustment rules
// Define fixed rule (for 1900-1955)
let transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 2, 0, 0), 3, 15)
let transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFixedDateRule(DateTime(1, 1, 1, 3, 0, 0), 11, 15)
let adjustment =
TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(1900, 1, 1), DateTime(1955, 12, 31), delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add adjustment
// Define floating rule (for 1956- )
let transitionRuleStart = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 2, 0, 0), 3, 5, DayOfWeek.Sunday)
let transitionRuleEnd = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(DateTime(1, 1, 1, 3, 0, 0), 10, 4, DayOfWeek.Sunday)
let adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(DateTime(1956, 1, 1), DateTime.MaxValue.Date, delta, transitionRuleStart, transitionRuleEnd)
adjustmentList.Add adjustment

// Create fictitious time zone
let imaginaryTZ =
TimeZoneInfo.CreateCustomTimeZone("Fictitious Standard Time", TimeSpan(-9, 0, 0),
"(GMT-09:00) Fictitious Time", "Fictitious Standard Time",
"Fictitious Daylight Time", adjustmentList.ToArray())
// </Snippet2>
()

// <Snippet3>
let getFixedTransitionTimes () =
let timeZones = TimeZoneInfo.GetSystemTimeZones()
let dateInfo = CultureInfo.CurrentCulture.DateTimeFormat
for zone in timeZones do
let adjustmentRules = zone.GetAdjustmentRules()
for adjustmentRule in adjustmentRules do
let daylightStart = adjustmentRule.DaylightTransitionStart
if daylightStart.IsFixedDateRule then
printfn $"For {zone.StandardName}, daylight savings time begins at {daylightStart.TimeOfDay:t} on {dateInfo.GetMonthName daylightStart.Month} {daylightStart.Day} from {adjustmentRule.DateStart:d} to {adjustmentRule.DateEnd:d}."
let daylightEnd = adjustmentRule.DaylightTransitionEnd
if daylightEnd.IsFixedDateRule then
printfn $"For {zone.StandardName}, daylight savings time ends at {daylightEnd.TimeOfDay:t} on {dateInfo.GetMonthName daylightEnd.Month} {daylightEnd.Day} from {adjustmentRule.DateStart:d} to {adjustmentRule.DateEnd:d}."
// </Snippet3>

// <Snippet4>
type WeekOfMonth =
| First = 1
| Second = 2
| Third = 3
| Fourth = 4
| Last = 5

let getFloatingTransitionTimes () =
let timeZones = TimeZoneInfo.GetSystemTimeZones()
for zone in timeZones do
let adjustmentRules = zone.GetAdjustmentRules()
let dateInfo = CultureInfo.CurrentCulture.DateTimeFormat
for adjustmentRule in adjustmentRules do
let daylightStart = adjustmentRule.DaylightTransitionStart
if not daylightStart.IsFixedDateRule then
printfn $"{zone.StandardName}, {adjustmentRule.DateStart:d}-{adjustmentRule.DateEnd:d}: Begins at {daylightStart.TimeOfDay:t} on the {enum<WeekOfMonth> daylightStart.Week} {daylightStart.DayOfWeek} of {dateInfo.GetMonthName daylightStart.Month}."

let daylightEnd = adjustmentRule.DaylightTransitionEnd
if not daylightEnd.IsFixedDateRule then
printfn $"{zone.StandardName}, {adjustmentRule.DateStart:d}-{adjustmentRule.DateEnd:d}: Ends at {daylightEnd.TimeOfDay:t} on the {enum<WeekOfMonth> daylightEnd.Week} {daylightEnd.DayOfWeek} of {dateInfo.GetMonthName daylightEnd.Month}."
// </Snippet4>

module AdditionalExamples =
// <Snippet6>
type WeekOfMonth =
| First = 1
| Second = 2
| Third = 3
| Fourth = 4
| Last = 5

let getAllTransitionTimes () =
let timeZones = TimeZoneInfo.GetSystemTimeZones()
let dateInfo = CultureInfo.CurrentCulture.DateTimeFormat

for zone in timeZones do
printfn $"{zone.StandardName} transition time information:"
let adjustmentRules= zone.GetAdjustmentRules()

// Indicate that time zone has no adjustment rules
if adjustmentRules.Length = 0 then
printfn " No adjustment rules defined."
else
// Iterate adjustment rules
for adjustmentRule in adjustmentRules do
printfn $" Adjustment rule from {adjustmentRule.DateStart:d} to {adjustmentRule.DateEnd:d}:"

// Get start of transition
let daylightStart = adjustmentRule.DaylightTransitionStart
// Display information on fixed date rule
if not daylightStart.IsFixedDateRule then
printfn $" Begins at {daylightStart.TimeOfDay:t} on the {enum<WeekOfMonth> daylightStart.Week} {daylightStart.DayOfWeek} of {dateInfo.GetMonthName daylightStart.Month}."
// Display information on floating date rule
else
printfn $" Begins at {daylightStart.TimeOfDay:t} on the {enum<WeekOfMonth> daylightStart.Week} {daylightStart.DayOfWeek} of {dateInfo.GetMonthName daylightStart.Month}."

// Get end of transition
let daylightEnd = adjustmentRule.DaylightTransitionEnd
// Display information on fixed date rule
if not daylightEnd.IsFixedDateRule then
printfn $" Ends at {daylightEnd.TimeOfDay:t} on the {enum<WeekOfMonth> daylightEnd.Week} {daylightEnd.DayOfWeek} of {dateInfo.GetMonthName daylightEnd.Month}."
// Display information on floating date rule
else
printfn $" Ends at {daylightStart.TimeOfDay:t} on the {enum<WeekOfMonth> daylightStart.Week} {daylightStart.DayOfWeek} of {dateInfo.GetMonthName daylightStart.Month}."
// </Snippet6>

compareForEquality ()
printfn ""
compareTransitionTimesForEquality()
printfn ""
createTransitionRules()
printfn ""
getFixedTransitionTimes ()
printfn ""
getFloatingTransitionTimes ()
printfn ""
AdditionalExamples.getAllTransitionTimes ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module example1

// <Snippet5>
open System
open System.Globalization

let displayTransitionInfo (transition: TimeZoneInfo.TransitionTime) year label =
// For non-fixed date rules, get local calendar
let cal = CultureInfo.CurrentCulture.Calendar
// Get first day of week for transition
// For example, the 3rd week starts no earlier than the 15th of the month
let startOfWeek = transition.Week * 7 - 6
// What day of the week does the month start on?
let firstDayOfWeek = cal.GetDayOfWeek(DateTime(year, transition.Month, 1)) |> int
// Determine how much start date has to be adjusted
let changeDayOfWeek = int transition.DayOfWeek

let transitionDay =
if firstDayOfWeek <= changeDayOfWeek then
startOfWeek + (changeDayOfWeek - firstDayOfWeek)
else
startOfWeek + (7 - firstDayOfWeek + changeDayOfWeek)

// Adjust for months with no fifth week
let transitionDay =
if transitionDay > cal.GetDaysInMonth(year, transition.Month) then
transitionDay - 7
else
transitionDay

printfn $" {label} {transition.DayOfWeek}, {DateTime(year, transition.Month, transitionDay):d} at {transition.TimeOfDay:t}"

let getAdjustment (adjustments: TimeZoneInfo.AdjustmentRule seq) year =
adjustments
// Iterate adjustment rules for time zone
// Determine if this adjustment rule covers year desired
|> Seq.tryFind (fun adjustment -> adjustment.DateStart.Year <= year && adjustment.DateEnd >= DateTime year)
|> Option.defaultValue null

let getTransitionTimes year =
// Instantiate DateTimeFormatInfo object for month names
let dateFormat = CultureInfo.CurrentCulture.DateTimeFormat

// Get and iterate time zones on local computer
let timeZones = TimeZoneInfo.GetSystemTimeZones()
for timeZone in timeZones do
printfn $"{timeZone.StandardName}:"
let adjustments = timeZone.GetAdjustmentRules()
let startYear = year
let mutable endYear = startYear

if adjustments.Length = 0 then
printfn " No adjustment rules."
else
let adjustment = getAdjustment adjustments year
if adjustment = null then
Console.WriteLine(" No adjustment rules available for this year.")
else
// Determine if starting transition is fixed
let startTransition = adjustment.DaylightTransitionStart
// Determine if starting transition is fixed and display transition info for year
if startTransition.IsFixedDateRule then
printfn $" Begins on {dateFormat.GetMonthName startTransition.Month} {startTransition.Day} at {startTransition.TimeOfDay:t}"
else
displayTransitionInfo startTransition startYear "Begins on"

// Determine if ending transition is fixed and display transition info for year
let mutable endTransition = adjustment.DaylightTransitionEnd

// Does the transition back occur in an earlier month (i.e.,
// the following year) than the transition to DST? If so, make
// sure we have the right adjustment rule.
if endTransition.Month < startTransition.Month then
endTransition <- (getAdjustment adjustments (year + 1)).DaylightTransitionEnd
endYear <- endYear + 1

if endTransition.IsFixedDateRule then
printfn $" Ends on {dateFormat.GetMonthName endTransition.Month} {endTransition.Day} at {endTransition.TimeOfDay:t}"
else
displayTransitionInfo endTransition endYear "Ends on"

// </Snippet5>
getTransitionTimes 2007
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="example1.fs" />
<Compile Include="System.TimeZone2.TransitionTime.Class.fs" />
</ItemGroup>
</Project>
Loading