Quatz.Net GetNextFireTime()返回"错误的时区"

时间:2018-01-09 10:32:12

标签: datetime quartz-scheduler quartz.net datetimeoffset

我目前正在开发一个使用Quartz Scheduler的项目。 我正在使用以下方法处理显示当前活动作业的不同详细信息的功能。

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;
    }

其中的方法自行工作得很好,我的问题在于GetNextFireTime()方法以UTC格式给出它,这比GMT + 1 / UTC + 1落后一小时。

显示时: 10-01-2018 07:00:00 +00:00

应显示: 10-01-2018 08:00:00 +01:00

我查看了以下链接:Working With DateTimeOffset

并尝试使用提到的Marko Lahma可以在转换DateTimeOffset时起作用。但是我遇到了无法转换它的墙,因为GetNextFireTime()会返回一个DataTimeOffset?我尝试过的东西,不能转换Nullable的东西。所以我很擅长做什么

1 个答案:

答案 0 :(得分:2)

您可以将DateTimeOffset转换为本地DateTimeOffset,将其转换为DateTime。

var nextFireDateTime = trigger.GetNextFireTimeUtc()?.ToLocalTime().DateTime;

请注意,nextFireDateTime可能为空。