-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Provide an abstraction for the system clock #29139
Comments
Side note: Both Java ( I feel like, instead of an interface or class, I want to have a delegate/func definition, but that's possibly just my fascination with functional languages talking. So something like: public class SomeClass
{
int SomeMethod(Func<DateTimeOffset> currentTime, int otherParameter);
// To abstract getting the "system" timezone, if relevant.
int SomeMethod(Func<(DateTimeOffset, TimeZoneInfo)> currentTime, int otherParameter);
}
// Used like:
var result = someClassInstance.SomeMethod(() => DateTimeOffset.UtcNow, 4);
var constant = someClassInstance.SomeMethod(() => new DateTimeOffset(2019, 4, 1, 15, 39, 0, TimeSpan.FromHours(0)), 4);
var result = someClassInstance.SomeMethod(() => (DateTimeOffset.UtcNow, TimeZoneInfo.Utc), 4); ...presumably, there'd be at least a "common" set of pre-defined clocks, for the system (probably provided by us?), as well as some other common use cases (fixed is trivial to create on your own if it's defined as a |
For what its worth this already exists in ASP.NET Core as |
Is it so common for unrelated components to want to share such an interface, that itis burdensome for it to be shared in some other common library? The fact that something might be interesting to have and possibly share is not enough by itself to add to the base libraries. There is a cost to defining and shipping a type and we may make a bad decision about its shape. Once we ship an |
Valid points. I am not here to push my case, only to gather viewpoints. A To avoid a function in a constructor or method signature, I think for many people it would mean writing a package that exports only a single public interface like But I'm open minded to people's thoughts, esp. contributors/maintainers. |
Let's close this in favor of #36617 (because it has more comments) |
Should we have a standard abstraction for the wall-clock that returns the local
DateTimeOffset
?The idea is to break the hard dependency on the system clock and be able to inject a different clock for system level testing of time and ageing impacts, especially useful in finance.
I've rarely seen anyone do this but often seen people that wish they had. I think its just a habit we haven't learned yet.
If we saw more types taking
IWallClock
or whatever we'd maybe think more about this. Sure its super easy to implement but likeILogFactory
life's better when we're all using a uniform interface.The text was updated successfully, but these errors were encountered: