Skip to content

Commit 8cffa20

Browse files
authored
DateTimeOffset F# snippets (#7596)
1 parent b291cea commit 8cffa20

File tree

43 files changed

+2419
-0
lines changed

Some content is hidden

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

43 files changed

+2419
-0
lines changed
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+
<ItemGroup>
7+
<Compile Include="parseexact-iso8601.fs" />
8+
<Compile Include="parseexact-iso8601-2.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module parseexact_iso8601_2
2+
3+
open System
4+
open System.Globalization
5+
6+
let dateStrings =
7+
[| "2018-08-18T12:45:16.0000000Z"
8+
"2018/08/18T12:45:16.0000000Z"
9+
"2018-18-08T12:45:16.0000000Z"
10+
"2018-08-18T12:45:16.0000000"
11+
" 2018-08-18T12:45:16.0000000Z "
12+
"2018-08-18T12:45:16.0000000+02:00"
13+
"2018-08-18T12:45:16.0000000-07:00" |]
14+
15+
let parseWithISO8601 dateStrings styles =
16+
printfn $"Parsing with {styles}:"
17+
for dateString in dateStrings do
18+
try
19+
let date = DateTimeOffset.ParseExact(dateString, "O", null, styles)
20+
printfn $""" {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
21+
with :? FormatException ->
22+
printfn $" FormatException: Unable to convert '{dateString}'"
23+
24+
parseWithISO8601 dateStrings DateTimeStyles.None
25+
printfn "\n-----\n"
26+
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
27+
printfn "\n-----\n"
28+
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
29+
printfn "\n-----\n"
30+
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
31+
printfn "\n-----\n"
32+
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal
33+
34+
35+
// The example displays the following output:
36+
// Parsing with None:
37+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
38+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
39+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
40+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
41+
// FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
42+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
43+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
44+
//
45+
// -----
46+
//
47+
// Parsing with AllowWhiteSpaces:
48+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
49+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
50+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
51+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
52+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
53+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
54+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
55+
//
56+
// -----
57+
//
58+
// Parsing with AdjustToUniversal:
59+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
60+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
61+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
62+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 19:45:16 +00:00
63+
// FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
64+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 10:45:16 +00:00
65+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 19:45:16 +00:00
66+
//
67+
// -----
68+
//
69+
// Parsing with AssumeLocal:
70+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
71+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
72+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
73+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
74+
// FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
75+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
76+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
77+
//
78+
// -----
79+
//
80+
// Parsing with AssumeUniversal:
81+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
82+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
83+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
84+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 +00:00
85+
// FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
86+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
87+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module parseexact_iso8601
2+
3+
open System
4+
5+
let dateStrings =
6+
[ "2018-08-18T12:45:16.0000000Z"
7+
"2018/08/18T12:45:16.0000000Z"
8+
"2018-18-08T12:45:16.0000000Z"
9+
" 2018-08-18T12:45:16.0000000Z "
10+
"2018-08-18T12:45:16.0000000+02:00"
11+
"2018-08-18T12:45:16.0000000-07:00" ]
12+
13+
for dateString in dateStrings do
14+
try
15+
let date =
16+
DateTimeOffset.ParseExact(dateString, "O", null)
17+
18+
printfn $"""{dateString, -35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
19+
with :? FormatException -> printfn $"FormatException: Unable to convert '{dateString}'"
20+
21+
22+
// The example displays the following output:
23+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
24+
// FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
25+
// FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
26+
// FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
27+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
28+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
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+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="tryparseexacto8601-2.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
open System
2+
open System.Globalization
3+
4+
let parseWithISO8601 dateStrings styles =
5+
printfn $"Parsing with {styles}:"
6+
for dateString in dateStrings do
7+
match DateTimeOffset.TryParseExact(dateString, "O", null, styles) with
8+
| true, date ->
9+
printfn $""" {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
10+
| _ ->
11+
printfn $" Unable to convert '{dateString}'"
12+
13+
let dateStrings =
14+
[ "2018-08-18T12:45:16.0000000Z"
15+
"2018/08/18T12:45:16.0000000Z"
16+
"2018-18-08T12:45:16.0000000Z"
17+
"2018-08-18T12:45:16.0000000"
18+
" 2018-08-18T12:45:16.0000000Z "
19+
"2018-08-18T12:45:16.0000000+02:00"
20+
"2018-08-18T12:45:16.0000000-07:00" ]
21+
22+
parseWithISO8601 dateStrings DateTimeStyles.None
23+
printfn "\n-----\n"
24+
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
25+
printfn "\n-----\n"
26+
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
27+
printfn "\n-----\n"
28+
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
29+
printfn "\n-----\n"
30+
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal
31+
32+
// The example displays the following output:
33+
// Parsing with None:
34+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
35+
// Unable to convert '2018/08/18T12:45:16.0000000Z'
36+
// Unable to convert '2018-18-08T12:45:16.0000000Z'
37+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
38+
// Unable to convert ' 2018-08-18T12:45:16.0000000Z '
39+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
40+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
41+
//
42+
// -----
43+
//
44+
// Parsing with AllowWhiteSpaces:
45+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
46+
// Unable to convert '2018/08/18T12:45:16.0000000Z'
47+
// Unable to convert '2018-18-08T12:45:16.0000000Z'
48+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
49+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
50+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
51+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
52+
//
53+
// -----
54+
//
55+
// Parsing with AdjustToUniversal:
56+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
57+
// Unable to convert '2018/08/18T12:45:16.0000000Z'
58+
// Unable to convert '2018-18-08T12:45:16.0000000Z'
59+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 19:45:16 +00:00
60+
// Unable to convert ' 2018-08-18T12:45:16.0000000Z '
61+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 10:45:16 +00:00
62+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 19:45:16 +00:00
63+
//
64+
// -----
65+
//
66+
// Parsing with AssumeLocal:
67+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
68+
// Unable to convert '2018/08/18T12:45:16.0000000Z'
69+
// Unable to convert '2018-18-08T12:45:16.0000000Z'
70+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 -07:00
71+
// Unable to convert ' 2018-08-18T12:45:16.0000000Z '
72+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
73+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
74+
//
75+
// -----
76+
//
77+
// Parsing with AssumeUniversal:
78+
// 2018-08-18T12:45:16.0000000Z --> 2018-08-18 12:45:16 +00:00
79+
// Unable to convert '2018/08/18T12:45:16.0000000Z'
80+
// Unable to convert '2018-18-08T12:45:16.0000000Z'
81+
// 2018-08-18T12:45:16.0000000 --> 2018-08-18 12:45:16 +00:00
82+
// Unable to convert ' 2018-08-18T12:45:16.0000000Z '
83+
// 2018-08-18T12:45:16.0000000+02:00 --> 2018-08-18 12:45:16 +02:00
84+
// 2018-08-18T12:45:16.0000000-07:00 --> 2018-08-18 12:45:16 -07:00
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
open System
2+
open System.Globalization
3+
4+
let constructWithDateTime () =
5+
// <Snippet1>
6+
let localNow = DateTime.Now
7+
let localOffset = DateTimeOffset localNow
8+
printfn $"{localOffset}"
9+
10+
let utcNow = DateTime.UtcNow
11+
let utcOffset = DateTimeOffset utcNow
12+
printfn "{utcOffset}"
13+
14+
let unspecifiedNow = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified)
15+
let unspecifiedOffset = DateTimeOffset unspecifiedNow
16+
printfn $"{unspecifiedOffset}"
17+
18+
// The code produces the following output if run on Feb. 23, 2007, on
19+
// a system 8 hours earlier than UTC:
20+
// 2/23/2007 4:21:58 PM -08:00
21+
// 2/24/2007 12:21:58 AM +00:00
22+
// 2/23/2007 4:21:58 PM -08:00
23+
// </Snippet1>
24+
25+
let constructWithTicks () =
26+
// <Snippet2>
27+
let dateWithoutOffset = DateTime(2007, 7, 16, 13, 32, 00)
28+
let timeFromTicks = DateTimeOffset(dateWithoutOffset.Ticks, TimeSpan(-5, 0, 0))
29+
printfn $"{timeFromTicks}"
30+
// The code produces the following output:
31+
// 7/16/2007 1:32:00 PM -05:00
32+
// </Snippet2>
33+
34+
let constructWithDateAndOffset () =
35+
// <Snippet3>
36+
let localTime = DateTime(2007, 07, 12, 06, 32, 00)
37+
let dateAndOffset = DateTimeOffset(localTime, TimeZoneInfo.Local.GetUtcOffset localTime)
38+
printfn $"{dateAndOffset}"
39+
// The code produces the following output:
40+
// 7/12/2007 6:32:00 AM -07:00
41+
// </Snippet3>
42+
43+
let constructNonLocalWithLocalTicks () =
44+
// <Snippet4>
45+
let localTime = DateTime.Now
46+
let nonLocalDateWithOffset = DateTimeOffset(localTime.Ticks, TimeSpan(2, 0, 0))
47+
printfn $"{nonLocalDateWithOffset}"
48+
// The code produces the following output if run on Feb. 23, 2007:
49+
// 2/23/2007 4:37:50 PM +02:00
50+
// </Snippet4>
51+
52+
let constructWithDateElements () =
53+
// <Snippet5>
54+
let specificDate = DateTime(2008, 5, 1, 06, 32, 00)
55+
let offsetDate = DateTimeOffset(specificDate.Year,
56+
specificDate.Month,
57+
specificDate.Day,
58+
specificDate.Hour,
59+
specificDate.Minute,
60+
specificDate.Second,
61+
TimeSpan(-5, 0, 0))
62+
printfn $"Current time: {offsetDate}"
63+
printfn $"Corresponding UTC time: {offsetDate.UtcDateTime}"
64+
// The code produces the following output:
65+
// Current time: 5/1/2008 6:32:00 AM -05:00
66+
// Corresponding UTC time: 5/1/2008 11:32:00 AM
67+
// </Snippet5>
68+
69+
let constructWithDateElements2 () =
70+
// <Snippet6>
71+
let specificDate = DateTime(2008, 5, 1, 6, 32, 05)
72+
let offsetDate = DateTimeOffset(specificDate.Year - 1,
73+
specificDate.Month,
74+
specificDate.Day,
75+
specificDate.Hour,
76+
specificDate.Minute,
77+
specificDate.Second,
78+
0,
79+
TimeSpan(-5, 0, 0))
80+
printfn $"Current time: {offsetDate}"
81+
printfn $"Corresponding UTC time: {offsetDate.UtcDateTime}"
82+
// The code produces the following output:
83+
// Current time: 5/1/2007 6:32:05 AM -05:00
84+
// Corresponding UTC time: 5/1/2007 11:32:05 AM
85+
// </Snippet6>
86+
87+
let constructWithDateElements3 () =
88+
// <Snippet7>
89+
let fmt = "dd MMM yyyy HH:mm:ss"
90+
let thisDate = DateTime(2007, 06, 12, 19, 00, 14, 16)
91+
let offsetDate = DateTimeOffset(thisDate.Year,
92+
thisDate.Month,
93+
thisDate.Day,
94+
thisDate.Hour,
95+
thisDate.Minute,
96+
thisDate.Second,
97+
thisDate.Millisecond,
98+
TimeSpan(2, 0, 0))
99+
printfn $"Current time: {offsetDate.ToString fmt}:{offsetDate.Millisecond}"
100+
// The code produces the following output:
101+
// Current time: 12 Jun 2007 19:00:14:16
102+
// </Snippet7>
103+
104+
let constructWithCalendar () =
105+
// <Snippet8>
106+
// Instantiate DateTimeOffset with Hebrew calendar
107+
let year = 5770
108+
let cal = HebrewCalendar()
109+
let fmt = CultureInfo "he-IL"
110+
fmt.DateTimeFormat.Calendar <- cal
111+
let dateInCal = DateTimeOffset(year, 7, 12,
112+
15, 30, 0, 0,
113+
cal,
114+
TimeSpan(2, 0, 0))
115+
// Display the date in the Hebrew calendar
116+
printfn $"Date in Hebrew Calendar: {dateInCal.ToString fmt:g}"
117+
// Display the date in the Gregorian calendar
118+
printfn $"Date in Gregorian Calendar: {dateInCal:g}\n"
119+
120+
// Instantiate DateTimeOffset with Hijri calendar
121+
let year = 1431
122+
let cal = HijriCalendar()
123+
let fmt = CultureInfo "ar-SA"
124+
fmt.DateTimeFormat.Calendar <- cal
125+
let dateInCal = DateTimeOffset(year, 7, 12,
126+
15, 30, 0, 0,
127+
cal,
128+
TimeSpan(2, 0, 0))
129+
// Display the date in the Hijri calendar
130+
printfn $"Date in Hijri Calendar: {dateInCal.ToString fmt:g}"
131+
// Display the date in the Gregorian calendar
132+
printfn $"Date in Gregorian Calendar: {dateInCal:g}\n"
133+
// </Snippet8>
134+
135+
constructWithDateTime ()
136+
printfn ""
137+
constructWithTicks ()
138+
printfn ""
139+
constructWithDateAndOffset ()
140+
printfn ""
141+
constructNonLocalWithLocalTicks ()
142+
printfn ""
143+
constructWithDateElements ()
144+
printfn ""
145+
constructWithDateElements2 ()
146+
printfn ""
147+
constructWithDateElements3 ()
148+
printfn ""
149+
constructWithCalendar ()
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+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Constructors.fs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)