Skip to content
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

Refactor code to use TimeProvider.System #2286

Merged
merged 3 commits into from
Feb 25, 2024

Conversation

jafin
Copy link
Contributor

@jafin jafin commented Feb 24, 2024

Summary

Replace DateTime usage with TimeProvider.System
Replace implicit DateTime to DateTimeOffset conversions with explicit. Add in Analyzer to ensure DateTime isn't used.
Remove a check for IsRUnningOnMono. As it is no longer one of the supported deployment frameworks. Registers a TimeProvider in DI (but is not using it)

Bcl.TimeProvider supports Net8, net 4.6.2 ; Netstandard 2.0

❌ This does NOT implement DI for TimeProvider. A few usage of DateTime are in static variables and private constructors. I believe this would possibly need a servicelocator to pull in as a dependency. And would rather see the DI work handle this if possible.

Relates to #2079

Replace DateTime usage with TimeProvider.System
Replace implicit DateTime to DateTimeOffset conversions with explicit.
Add in Analyzer to ensure DateTime isn't used.
Remove a check for IsRUnningOnMono. As it is no longer one of the supported deployment frameworks.
Registers a TimeProvider in DI (but is not using it)
src/Quartz/CronExpression.cs Outdated Show resolved Hide resolved
src/Quartz/DateBuilder.cs Outdated Show resolved Hide resolved
@@ -114,7 +102,7 @@ public static DateBuilder NewDate()
/// <returns></returns>
public static DateBuilder NewDateInTimeZone(TimeZoneInfo tz)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe needs parameter TimeProvider? timeProvider = null and then we default to system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the DateBuilder constructors are private, I've reduced the constructor to one, made TimeZoneInfo? optional, and as per your comment, in the public method NewDate and NewDateInTimeZone default to TimeProvider.System when not provided. Take a look at next PR let me know.

@@ -259,7 +247,7 @@ public static DateTimeOffset TomorrowAt(int hour, int minute, int second)
ValidateMinute(minute);
ValidateHour(hour);

DateTimeOffset now = DateTimeOffset.Now;
DateTimeOffset now = TimeProvider.System.GetLocalNow();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe needs parameter TimeProvider? timeProvider = null and then we default to system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted in next PR, let me know if suitable.

src/Quartz/Impl/Triggers/CalendarIntervalTriggerImpl.cs Outdated Show resolved Hide resolved
@@ -386,6 +392,7 @@ public CronTriggerImpl(string name, string group, string cronExpression) : base(
string cronExpression,
TimeZoneInfo timeZone) : base(name, group, jobName, jobGroup)
{
timeProvider = TimeProvider.System;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the widest constructor take TimeProvider? timeProvider = null and then we default to system?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok reworked, also trying to constructor chain where possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more general question overall, as perhaps I am going about things wrong. We can inject/pass in a TimeProvider, but there is also this SystemTime with Funcs for Now/UtcNow. I see these as both conflicting.

SystemTime being static can be set at a global level, good for objects that sit outside of DI etc. But also being static, are there situations where there is a need for separate TimeProviders ? In which case I would assume the goal is to reduce dependency on SystemTime class. I can't think of a scenario where different TimeProviders would be required?

So just wondering if taking parameters for a TimeProvider actually makes sense and should methods to leaning on SystemTime more?

src/Quartz/Impl/Triggers/DailyTimeIntervalTriggerImpl.cs Outdated Show resolved Hide resolved
…e with the timeProvider

Refactor Datebuilder to take optional TimeProvider. Default to TimeProvider.System on null
Copy link

sonarcloud bot commented Feb 25, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

@@ -321,14 +328,15 @@ public CronTriggerImpl(string name, string group, string cronExpression) : base(
/// <exception cref="ArgumentNullException"><paramref name="name"/>, <paramref name="group"/>, <paramref name="jobName"/> or <paramref name="jobGroup"/> are <see langword="null"/>.</exception>
public CronTriggerImpl(string name, string group, string jobName,
Copy link
Contributor Author

@jafin jafin Feb 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be removed as it matches line 379 due to optional parameters.

@@ -345,21 +353,8 @@ public CronTriggerImpl(string name, string group, string cronExpression) : base(
public CronTriggerImpl(string name, string group, string jobName,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be removed as it matches line 379 due to optional parameters.

Copy link
Member

@lahma lahma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great step towards the future, thank you!

@lahma lahma merged commit c4d3a0a into quartznet:main Feb 25, 2024
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants