-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add TimeProvider overview #43881
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
Merged
Add TimeProvider overview #43881
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1165f59
Add TimeProvider overview
adegeo b868a73
Minor
adegeo fcec8a3
Add to toc
adegeo 1f53147
minor
adegeo c8812b6
Various tweaks
adegeo 10ac470
Apply suggestions from code review
adegeo 113ebae
Adjust namespaces
adegeo 3cfb62b
Feedback
adegeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
docs/standard/datetime/snippets/timeprovider-overview/csharp/CalendarHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| namespace ExampleProject; | ||
|
|
||
| //<CalendarHelper> | ||
| public static class CalendarHelper | ||
| { | ||
| static readonly DateTimeOffset MoonLandingDateTime = new(1969, 7, 20, 20, 17, 40, TimeZoneInfo.Utc.BaseUtcOffset); | ||
|
|
||
| public static void SendGreeting(TimeProvider currentTime, string name) | ||
| { | ||
| DateTimeOffset localTime = currentTime.GetLocalNow(); | ||
|
|
||
| Console.WriteLine($"Good morning, {name}!"); | ||
| Console.WriteLine($"The date is {localTime.Date:d} and the day is {localTime.Date.DayOfWeek}."); | ||
|
|
||
| if (localTime.Date.Month == MoonLandingDateTime.Date.Month | ||
| && localTime.Date.Day == MoonLandingDateTime.Date.Day) | ||
|
|
||
| Console.WriteLine("Did you know that on this day in 1969 humans landed on the Moon?"); | ||
adegeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Console.WriteLine($"I hope you enjoy your day!"); | ||
| } | ||
| } | ||
| //</CalendarHelper> | ||
13 changes: 13 additions & 0 deletions
13
docs/standard/datetime/snippets/timeprovider-overview/csharp/MoonLandingTimeProviderPST.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace ExampleProject; | ||
|
|
||
| //<CustomProvider> | ||
| public class MoonLandingTimeProviderPST: TimeProvider | ||
| { | ||
| // July 20, 1969, at 20:17:40 UTC | ||
| private readonly DateTimeOffset _specificDateTime = new(1969, 7, 20, 20, 17, 40, TimeZoneInfo.Utc.BaseUtcOffset); | ||
|
|
||
| public override DateTimeOffset GetUtcNow() => _specificDateTime; | ||
|
|
||
| public override TimeZoneInfo LocalTimeZone => TimeZoneInfo.FindSystemTimeZoneById("PST"); | ||
| } | ||
| //</CustomProvider> |
62 changes: 62 additions & 0 deletions
62
docs/standard/datetime/snippets/timeprovider-overview/csharp/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using ExampleProject; | ||
|
|
||
| Console.WriteLine(@"_./-==--======- getlocal -======--==-\._"); | ||
|
|
||
| //<GetLocal> | ||
| Console.WriteLine($"Local: {TimeProvider.System.GetLocalNow()}"); | ||
| Console.WriteLine($"Utc: {TimeProvider.System.GetUtcNow()}"); | ||
|
|
||
| /* This example produces output similar to the following: | ||
| * | ||
| * Local: 12/5/2024 10:41:14 AM -08:00 | ||
| * Utc: 12/5/2024 6:41:14 PM +00:00 | ||
| */ | ||
| //</GetLocal> | ||
|
|
||
| Console.WriteLine(@"_./-==--======- timestamp -======--==-\._"); | ||
|
|
||
| //<Timestamp> | ||
| long stampStart = TimeProvider.System.GetTimestamp(); | ||
| Console.WriteLine($"Starting timestamp: {stampStart}"); | ||
|
|
||
| long stampEnd = TimeProvider.System.GetTimestamp(); | ||
adegeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Ending timestamp: {stampEnd}"); | ||
|
|
||
| Console.WriteLine($"Elapsed time: {TimeProvider.System.GetElapsedTime(stampStart, stampEnd)}"); | ||
| Console.WriteLine($"Nanoseconds: {TimeProvider.System.GetElapsedTime(stampStart, stampEnd).TotalNanoseconds}"); | ||
|
|
||
| /* This example produces output similar to the following: | ||
| * | ||
| * Starting timestamp: 55185546133 | ||
| * Ending timestamp: 55185549929 | ||
| * Elapsed time: 00:00:00.0003796 | ||
| * Nanoseconds: 379600 | ||
| */ | ||
| //</Timestamp> | ||
|
|
||
| Console.WriteLine(@"_./-==--======- greeting-normal -======--==-\._"); | ||
|
|
||
| //<GreetingNormal> | ||
| CalendarHelper.SendGreeting(TimeProvider.System, "Eric Solomon"); | ||
|
|
||
| /* This example produces output similar to the following: | ||
| * | ||
| * Good morning, Eric Solomon! | ||
| * The date is 12/5/2024 and the day is Thursday. | ||
| * I hope you enjoy your day! | ||
| */ | ||
| //</GreetingNormal> | ||
|
|
||
| Console.WriteLine(@"_./-==--======- greeting-moon -======--==-\._"); | ||
|
|
||
| //<GreetingMoon> | ||
| CalendarHelper.SendGreeting(new MoonLandingTimeProviderPST(), "Eric Solomon"); | ||
|
|
||
| /* This example produces output similar to the following: | ||
| * | ||
| * Good morning, Eric Solomon! | ||
| * The date is 7/20/1969 and the day is Sunday. | ||
| * Did you know that on this day in 1969 humans landed on the Moon? | ||
| * I hope you enjoy your day! | ||
| */ | ||
| //</GreetingMoon> | ||
10 changes: 10 additions & 0 deletions
10
docs/standard/datetime/snippets/timeprovider-overview/csharp/myproject.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
24 changes: 24 additions & 0 deletions
24
docs/standard/datetime/snippets/timeprovider-overview/vb/CalendarHelper.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| '<CalendarHelper> | ||
| Public Module CalendarHelper | ||
|
|
||
| ReadOnly MoonLandingDateTime As DateTimeOffset = #7/20/1969 20:17:40# | ||
|
|
||
| Public Sub SendGreeting(currentTime As TimeProvider, name As String) | ||
|
|
||
| Dim localTime As DateTimeOffset = currentTime.GetLocalNow() | ||
|
|
||
| Console.WriteLine($"Good morning, {name}!") | ||
| Console.WriteLine($"The date is {localTime.Date:d} and the day is {localTime.Date.DayOfWeek}.") | ||
|
|
||
| If (localTime.Date.Month = MoonLandingDateTime.Date.Month _ | ||
| And localTime.Date.Day = MoonLandingDateTime.Date.Day) Then | ||
|
|
||
| Console.WriteLine("Did you know that on this day in 1969 humans landed on the Moon?") | ||
| End If | ||
|
|
||
| Console.WriteLine($"I hope you enjoy your day!") | ||
|
|
||
| End Sub | ||
|
|
||
| End Module | ||
| '</CalendarHelper> |
19 changes: 19 additions & 0 deletions
19
docs/standard/datetime/snippets/timeprovider-overview/vb/MoonLandingTimeProviderPST.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| '<CustomProvider> | ||
| Public Class MoonLandingTimeProviderPST | ||
| Inherits TimeProvider | ||
|
|
||
| 'July 20, 1969, at 20:17:40 UTC | ||
| Private ReadOnly _specificDateTime As New DateTimeOffset(1969, 7, 20, 20, 17, 40, TimeZoneInfo.Utc.BaseUtcOffset) | ||
|
|
||
| Public Overrides Function GetUtcNow() As DateTimeOffset | ||
| Return _specificDateTime | ||
| End Function | ||
|
|
||
| Public Overrides ReadOnly Property LocalTimeZone As TimeZoneInfo | ||
| Get | ||
| Return TimeZoneInfo.FindSystemTimeZoneById("PST") | ||
| End Get | ||
| End Property | ||
|
|
||
| End Class | ||
| '</CustomProvider> |
70 changes: 70 additions & 0 deletions
70
docs/standard/datetime/snippets/timeprovider-overview/vb/Program.vb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| Imports System | ||
| Imports System.Globalization | ||
| Imports System.Text.Json | ||
|
|
||
| Module Program | ||
| Sub Main(args As String()) | ||
| Console.WriteLine("_./-==--======- getlocal -======--==-\._") | ||
|
|
||
| '<GetLocal> | ||
| Console.WriteLine($"Local: {TimeProvider.System.GetLocalNow()}") | ||
| Console.WriteLine($"Utc: {TimeProvider.System.GetUtcNow()}") | ||
|
|
||
| ' This example produces output similar to the following | ||
| ' | ||
| ' Local: 12/5/2024 10:41:14 AM -08:00 | ||
| ' Utc: 12/5/2024 6:41:14 PM +00:00 | ||
|
|
||
| '</GetLocal> | ||
|
|
||
| Console.WriteLine("_./-==--======- timestamp -======--==-\._") | ||
|
|
||
| '<Timestamp> | ||
| Dim stampStart As Long = TimeProvider.System.GetTimestamp() | ||
| Console.WriteLine($"Starting timestamp: {stampStart}") | ||
|
|
||
| Dim stampEnd As Long = TimeProvider.System.GetTimestamp() | ||
| Console.WriteLine($"Ending timestamp: {stampEnd}") | ||
|
|
||
| Console.WriteLine($"Elapsed time: {TimeProvider.System.GetElapsedTime(stampStart, stampEnd)}") | ||
| Console.WriteLine($"Nanoseconds: {TimeProvider.System.GetElapsedTime(stampStart, stampEnd).TotalNanoseconds}") | ||
|
|
||
| ' This example produces output similar to the following: | ||
| ' | ||
| ' Starting timestamp: 55185546133 | ||
| ' Ending timestamp: 55185549929 | ||
| ' Elapsed time: 00:00:00.0003796 | ||
| ' Nanoseconds: 379600 | ||
|
|
||
| '</Timestamp> | ||
|
|
||
| Console.WriteLine("_./-==--======- greeting-normal -======--==-\._") | ||
|
|
||
| '<GreetingNormal> | ||
| CalendarHelper.SendGreeting(TimeProvider.System, "Eric Solomon") | ||
|
|
||
| ' This example produces output similar to the following: | ||
| ' | ||
| ' Good morning, Eric Solomon! | ||
| ' The date is 12/5/2024 and the day is Thursday. | ||
| ' I hope you enjoy your day! | ||
|
|
||
| '</GreetingNormal> | ||
|
|
||
| Console.WriteLine("_./-==--======- greeting-moon -======--==-\._") | ||
|
|
||
| '<GreetingMoon> | ||
| CalendarHelper.SendGreeting(New MoonLandingTimeProviderPST(), "Eric Solomon") | ||
|
|
||
| ' This example produces output similar to the following: | ||
| ' | ||
| ' Good morning, Eric Solomon! | ||
| ' The date is 7/20/1969 and the day is Sunday. | ||
| ' Did you know that on this day in 1969 humans landed on the Moon? | ||
| ' I hope you enjoy your day! | ||
|
|
||
| '</GreetingMoon> | ||
|
|
||
| End Sub | ||
|
|
||
| End Module |
9 changes: 9 additions & 0 deletions
9
docs/standard/datetime/snippets/timeprovider-overview/vb/myproject.vbproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <RootNamespace>ExampleProject</RootNamespace> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.