I am currently working on a project in which I am using the Quartz Scheduler. I've been working on a feature that displays different details about jobs that are currently active, using the following method.
public IEnumerable<ActiveScheduleJob> GetAllActiveScheduls()
{
var activeScheduls = new List<ActiveScheduleJob>();
try
{
IList<string> jobGroups = scheduler.GetJobGroupNames();
// IList<string> triggerGroups = scheduler.GetTriggerGroupNames();
ActiveScheduleJob ASJ;
foreach (string group in jobGroups)
{
var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
var jobKeys = scheduler.GetJobKeys(groupMatcher);
foreach (var jobKey in jobKeys)
{
var detail = scheduler.GetJobDetail(jobKey);
var triggers = scheduler.GetTriggersOfJob(jobKey);
foreach (ITrigger trigger in triggers)
{
ASJ = new ActiveScheduleJob();
ASJ.Group = group;
ASJ.Name = jobKey.Name;
ASJ.Description = detail.Description;
ASJ.TriggerKeyName = trigger.Key.Name;
ASJ.TriggerKeyGroup = trigger.Key.Group;
ASJ.TriggerGetTypeName = trigger.GetType().Name;
ASJ.TriggerState = scheduler.GetTriggerState(trigger.Key);
ASJ.NextFireTime = trigger.GetNextFireTimeUtc();
if (ASJ.NextFireTime.HasValue)
{
ASJ.NextFireTimeString = ASJ.NextFireTime.Value.LocalDateTime.ToString();
}
ASJ.PreviousFireTime = trigger.GetPreviousFireTimeUtc();
if (ASJ.PreviousFireTime.HasValue)
{
ASJ.PreviousFireTimeString = ASJ.PreviousFireTime.Value.LocalDateTime.ToString();
}
ASJ.FullJobString = $"Trigger Name: {ASJ.TriggerKeyName} | Trigger Group: {ASJ.TriggerKeyGroup} | Trigger State: {ASJ.TriggerState} | Trigger Get Type: {ASJ.TriggerGetTypeName} | Job Name: {ASJ.Name} | Job Group: {ASJ.Group} | Next Fire Time: {ASJ.NextFireTimeString} | Previous Fire Time: {ASJ.PreviousFireTimeString} | Description: {ASJ.Description}";
activeScheduls.Add(ASJ);
}
}
}
}
catch (Exception ex)
{
logging.WriteLog(1, "JobScheduler", "GetAllActiveScheduls", "Hent alle aktive job+triggers", $"EXCEPTION MESSAGE: {ex.Message} | EXCEPTION INNER: {ex.InnerException}", LogType.Exception, "");
}
return activeScheduls;
}
The method in it self works just fine, my problems lies in the fact that the GetNextFireTime() method, gives it in UTC which is an hour behind my GMT+1 / UTC+1.
When it displays: 10-01-2018 07:00:00 +00:00
It should display: 10-01-2018 08:00:00 +01:00
I've looked at the following link: Working With DateTimeOffset
and tried to work with what Marko Lahma mentioned could work in converting the DateTimeOffset. But I've run into a wall where I can't convert it, because GetNextFireTime() returns a DataTimeOffset? and what I've tried, can't convert something thats Nullable. So I am abit stumped in what to do
You can convert the DateTimeOffset
to a local DateTimeOffset and that to a DateTime.
var nextFireDateTime = trigger.GetNextFireTimeUtc()?.ToLocalTime().DateTime;
Please note that nextFireDateTime
could be null.