Skip to content

Commit de0b1d8

Browse files
authored
System.DateTime F# snippets (#7548)
* System.DateTime F# snippets * Add missing module declaration
1 parent 8cffa20 commit de0b1d8

File tree

137 files changed

+3995
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+3995
-5
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
module Calender
2+
3+
open System
4+
5+
let thaiBuddistEra () =
6+
// <Snippet1>
7+
let thTH = System.Globalization.CultureInfo "th-TH"
8+
let value = DateTime(2016, 5, 28)
9+
10+
printfn $"{value.ToString thTH}"
11+
12+
thTH.DateTimeFormat.Calendar <- System.Globalization.GregorianCalendar()
13+
14+
printfn $"{value.ToString thTH}"
15+
16+
// The example displays the following output:
17+
// 28/5/2559 0:00:00
18+
// 28/5/2016 0:00:00
19+
// </Snippet1>
20+
21+
let thaiBuddhistEraParse () =
22+
// <Snippet2>
23+
let thTH = System.Globalization.CultureInfo "th-TH"
24+
let value = DateTime.Parse("28/05/2559", thTH)
25+
printfn $"{value.ToString thTH}"
26+
27+
thTH.DateTimeFormat.Calendar <- System.Globalization.GregorianCalendar()
28+
printfn $"{value.ToString thTH}"
29+
30+
// The example displays the following output:
31+
// 28/5/2559 0:00:00
32+
// 28/5/2016 0:00:00
33+
// </Snippet2>
34+
35+
let instantiateCalendar () =
36+
// <Snippet3>
37+
let thTH = System.Globalization.CultureInfo "th-TH"
38+
let dat = DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar)
39+
40+
printfn $"""Thai Buddhist era date: {dat.ToString("d", thTH)}"""
41+
printfn $"Gregorian date: {dat:d}"
42+
43+
// The example displays the following output:
44+
// Thai Buddhist Era Date: 28/5/2559
45+
// Gregorian Date: 28/05/2016
46+
// </Snippet3>
47+
48+
let calendarFields () =
49+
// <Snippet4>
50+
let thTH = System.Globalization.CultureInfo "th-TH"
51+
let cal = thTH.DateTimeFormat.Calendar
52+
let dat = DateTime(2559, 5, 28, cal)
53+
printfn "Using the Thai Buddhist Era calendar:"
54+
printfn $"""Date: {dat.ToString("d", thTH)}"""
55+
printfn $"Year: {cal.GetYear dat}"
56+
printfn $"Leap year: {cal.IsLeapYear(cal.GetYear dat)}\n"
57+
58+
printfn "Using the Gregorian calendar:"
59+
printfn $"Date: {dat:d}"
60+
printfn $"Year: {dat.Year}"
61+
printfn $"Leap year: {DateTime.IsLeapYear dat.Year}"
62+
63+
// The example displays the following output:
64+
// Using the Thai Buddhist Era calendar
65+
// Date : 28/5/2559
66+
// Year: 2559
67+
// Leap year : True
68+
//
69+
// Using the Gregorian calendar
70+
// Date : 28/05/2016
71+
// Year: 2016
72+
// Leap year : True
73+
// </Snippet4>
74+
75+
let calculateWeeks () =
76+
// <Snippet5>
77+
let thTH = System.Globalization.CultureInfo "th-TH"
78+
let thCalendar = thTH.DateTimeFormat.Calendar
79+
let dat = DateTime(1395, 8, 18, thCalendar)
80+
printfn "Using the Thai Buddhist Era calendar:"
81+
printfn $"""Date: {dat.ToString("d", thTH)}"""
82+
printfn $"Day of Week: {thCalendar.GetDayOfWeek dat}"
83+
printfn $"Week of year: {thCalendar.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}\n"
84+
85+
let greg = System.Globalization.GregorianCalendar()
86+
printfn "Using the Gregorian calendar:"
87+
printfn $"Date: {dat:d}"
88+
printfn $"Day of Week: {dat.DayOfWeek}"
89+
printfn $"Week of year: {greg.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}"
90+
91+
// The example displays the following output:
92+
// Using the Thai Buddhist Era calendar
93+
// Date : 18/8/1395
94+
// Day of Week: Sunday
95+
// Week of year: 34
96+
//
97+
// Using the Gregorian calendar
98+
// Date : 18/08/0852
99+
// Day of Week: Sunday
100+
// Week of year: 34
101+
// </Snippet5>
102+
103+
104+
thaiBuddistEra ()
105+
thaiBuddhistEraParse ()
106+
instantiateCalendar ()
107+
calendarFields ()
108+
calculateWeeks ()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module DateTimeComparisons
2+
3+
open System
4+
open System.Text
5+
6+
// <Snippet1>
7+
let roughlyEquals (time: DateTime) (timeWithWindow: DateTime) windowInSeconds frequencyInSeconds =
8+
let delta =
9+
int64 (timeWithWindow - time).TotalSeconds % frequencyInSeconds
10+
11+
let delta = if delta > windowInSeconds then frequencyInSeconds - delta else delta
12+
abs delta < windowInSeconds
13+
14+
let testRoughlyEquals () =
15+
let window = 10
16+
let window' = 10.
17+
let freq = 60 * 60 * 2 // 2 hours
18+
19+
let d1 = DateTime.Now
20+
21+
let d2 = d1.AddSeconds(2. * window')
22+
let d3 = d1.AddSeconds(-2. * window')
23+
let d4 = d1.AddSeconds(window' / 2.)
24+
let d5 = d1.AddSeconds(-window' / 2.)
25+
26+
let d6 = (d1.AddHours 2).AddSeconds(2. * window')
27+
let d7 = (d1.AddHours 2).AddSeconds(-2. * window')
28+
let d8 = (d1.AddHours 2).AddSeconds(window' / 2.)
29+
let d9 = (d1.AddHours 2).AddSeconds(-window' / 2.)
30+
31+
printfn $"d1 ({d1}) ~= d1 ({d1}): {roughlyEquals d1 d1 window freq}"
32+
printfn $"d1 ({d1}) ~= d2 ({d2}): {roughlyEquals d1 d2 window freq}"
33+
printfn $"d1 ({d1}) ~= d3 ({d3}): {roughlyEquals d1 d3 window freq}"
34+
printfn $"d1 ({d1}) ~= d4 ({d4}): {roughlyEquals d1 d4 window freq}"
35+
printfn $"d1 ({d1}) ~= d5 ({d5}): {roughlyEquals d1 d5 window freq}"
36+
37+
printfn $"d1 ({d1}) ~= d6 ({d6}): {roughlyEquals d1 d6 window freq}"
38+
printfn $"d1 ({d1}) ~= d7 ({d7}): {roughlyEquals d1 d7 window freq}"
39+
printfn $"d1 ({d1}) ~= d8 ({d8}): {roughlyEquals d1 d8 window freq}"
40+
printfn $"d1 ({d1}) ~= d9 ({d9}): {roughlyEquals d1 d9 window freq}"
41+
42+
// The example displays output similar to the following:
43+
// d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
44+
// d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
45+
// d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
46+
// d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
47+
// d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
48+
// d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
49+
// d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
50+
// d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
51+
// d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True
52+
// </Snippet1>
53+
testRoughlyEquals ()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// <Snippet6>
2+
namespace DateTimeExtensions
3+
4+
open System
5+
6+
[<Serializable>]
7+
type DateWithTimeZone =
8+
struct
9+
val TimeZone: TimeZoneInfo
10+
val DateTime: DateTime
11+
new (dateValue, timeZone) =
12+
{ DateTime = dateValue;
13+
TimeZone =
14+
if isNull timeZone then TimeZoneInfo.Local
15+
else timeZone }
16+
end
17+
18+
// </Snippet6>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Instantiation
2+
3+
open System
4+
5+
let instantiateWithConstructor () =
6+
// <Snippet1>
7+
let date1 = DateTime(2008, 5, 1, 8, 30, 52)
8+
printfn $"{date1}"
9+
// </Snippet1>
10+
11+
let instantiateWithReturnValue () =
12+
// <Snippet3>
13+
let date1 = DateTime.Now
14+
let date2 = DateTime.UtcNow
15+
let date3 = DateTime.Today
16+
// </Snippet3>
17+
()
18+
19+
let instantiateFromString () =
20+
// <Snippet4>
21+
let dateString = "5/1/2008 8:30:52 AM"
22+
let date1 = DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture)
23+
let iso8601String = "20080501T08:30:52Z"
24+
let dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
25+
// </Snippet4>
26+
()
27+
28+
let instantiateUsingDftCtor () =
29+
// <Snippet5>
30+
let dat1 = DateTime()
31+
32+
// The following method call displays 1/1/0001 12:00:00 AM.
33+
printfn $"{dat1.ToString System.Globalization.CultureInfo.InvariantCulture}"
34+
35+
// The following method call displays True.
36+
printfn $"{dat1.Equals DateTime.MinValue}"
37+
// </Snippet5>
38+
39+
instantiateWithConstructor ()
40+
instantiateWithReturnValue ()
41+
instantiateFromString ()
42+
instantiateUsingDftCtor ()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
module Parsing
2+
3+
open System
4+
5+
let parseStandardFormats () =
6+
// <Snippet1>
7+
System.Threading.Thread.CurrentThread.CurrentCulture <-
8+
System.Globalization.CultureInfo.CreateSpecificCulture "en-GB"
9+
10+
let date1 = DateTime(2013, 6, 1, 12, 32, 30)
11+
let badFormats = ResizeArray<String>()
12+
13+
printfn "%-37s %-19s\n" "Date String" "Date"
14+
for dateString in date1.GetDateTimeFormats() do
15+
match DateTime.TryParse dateString with
16+
| true, parsedDate ->
17+
printfn $"%-37s{dateString} %-19O{parsedDate}\n"
18+
| _ ->
19+
badFormats.Add dateString
20+
21+
// Display strings that could not be parsed.
22+
if badFormats.Count > 0 then
23+
printfn "\nStrings that could not be parsed: "
24+
for badFormat in badFormats do
25+
printfn $" {badFormat}"
26+
// Press "Run" to see the output.
27+
// </Snippet1>
28+
29+
let parseCustomFormats () =
30+
// <Snippet2>
31+
let formats = [| "yyyyMMdd"; "HHmmss" |]
32+
let dateStrings =
33+
[ "20130816"; "20131608"; " 20130816 "
34+
"115216"; "521116"; " 115216 " ]
35+
36+
for dateString in dateStrings do
37+
match DateTime.TryParseExact(dateString, formats, null,
38+
System.Globalization.DateTimeStyles.AllowWhiteSpaces |||
39+
System.Globalization.DateTimeStyles.AdjustToUniversal) with
40+
| true, parsedDate ->
41+
printfn $"{dateString} --> {parsedDate:g}"
42+
| _ ->
43+
printfn $"Cannot convert {dateString}"
44+
45+
// The example displays the following output:
46+
// 20130816 --> 8/16/2013 12:00 AM
47+
// Cannot convert 20131608
48+
// 20130816 --> 8/16/2013 12:00 AM
49+
// 115216 --> 4/22/2013 11:52 AM
50+
// Cannot convert 521116
51+
// 115216 --> 4/22/2013 11:52 AM
52+
// </Snippet2>
53+
54+
let parseISO8601 () =
55+
// <Snippet3>
56+
let iso8601String = "20080501T08:30:52Z"
57+
let dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
58+
59+
printfn $"{iso8601String} --> {dateISO8602:g}"
60+
// </Snippet3>
61+
62+
parseStandardFormats ()
63+
parseCustomFormats ()
64+
parseISO8601 ()

0 commit comments

Comments
 (0)