如何产生带有时间偏移的循环调度持续时间 [英] How to produce recurrence rschedule durations with timeshifts

查看:116
本文介绍了如何产生带有时间偏移的循环调度持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 rSchedule 来产生工作时间间隔.但是时区被忽略了.我该如何实现?

I am using rSchedule to produce working hour intervals.But timezone is ignored. How can I achieve it?

输入的是一天中的工作时间间隔,时区如下.

The input is working hour intervals in a day, with timezone as below.

const input = {
  workingHours:[{start:9, end:12}, {start:13, end:18}], 
  timeZone:'Europe/Berlin'
};

使用下面的代码,我期望产生一年中白天光线变化的间隔.但是,生成的输出将忽略我提供的timeZone.

With the below code, I am expecting to produce intervals with respect to day light shiftings in a year. However, the produced output ignores the timeZone I provided.

import { Schedule } from "@rschedule/core/generators";
import '@rschedule/moment-tz-date-adapter/setup';
import moment from 'moment-timezone';

const schedule = new Schedule({
      rrules: [
        {
          frequency: "MONTHLY",//frequency: "WEEKLY",
          //byDayOfWeek: ["MO", 'TU', 'WE', 'TH', 'FR'],
          duration:1000 * 60 * 60 * 3,//input.workingHours[0].end-input.workingHours[0].start,
          byHourOfDay:[12],//input.workingHours[0].end
          timezone:'Europe/Berlin',//input.timeZone,

          start: moment(Date.UTC(2019, 0, 1)),
          end: moment(Date.UTC(2020, 0, 0))
        }
      ]
    });

    schedule.occurrences().toArray().forEach(adapter => {
      console.log(
          {
            start: adapter.date.toISOString(),
            end: adapter.end.toISOString(),
          }
        )
    })

忽略时区的输出:

 { start: '2019-01-01T09:00:00.000Z', end: '2019-01-01T12:00:00.000Z' }
 { start: '2019-02-01T09:00:00.000Z', end: '2019-02-01T12:00:00.000Z' }
 { start: '2019-03-01T09:00:00.000Z', end: '2019-03-01T12:00:00.000Z' }
 { start: '2019-04-01T09:00:00.000Z', end: '2019-04-01T12:00:00.000Z' }
 { start: '2019-05-01T09:00:00.000Z', end: '2019-05-01T12:00:00.000Z' }
 { start: '2019-06-01T09:00:00.000Z', end: '2019-06-01T12:00:00.000Z' }
 { start: '2019-07-01T09:00:00.000Z', end: '2019-07-01T12:00:00.000Z' }
 { start: '2019-08-01T09:00:00.000Z', end: '2019-08-01T12:00:00.000Z' }
 { start: '2019-09-01T09:00:00.000Z', end: '2019-09-01T12:00:00.000Z' }
 { start: '2019-10-01T09:00:00.000Z', end: '2019-10-01T12:00:00.000Z' }
 { start: '2019-11-01T09:00:00.000Z', end: '2019-11-01T12:00:00.000Z' }
 { start: '2019-12-01T09:00:00.000Z', end: '2019-12-01T12:00:00.000Z' }

预期输出:

{ start: '2019-01-01T11:00:00.000Z', end: '2019-01-01T14:00:00.000Z' },
{ start: '2019-02-01T11:00:00.000Z', end: '2019-02-01T14:00:00.000Z' },
{ start: '2019-03-01T11:00:00.000Z', end: '2019-03-01T14:00:00.000Z' },
{ start: '2019-04-01T10:00:00.000Z', end: '2019-04-01T13:00:00.000Z' },
{ start: '2019-05-01T10:00:00.000Z', end: '2019-05-01T13:00:00.000Z' },
{ start: '2019-06-01T10:00:00.000Z', end: '2019-06-01T13:00:00.000Z' },
{ start: '2019-07-01T10:00:00.000Z', end: '2019-07-01T13:00:00.000Z' },
{ start: '2019-08-01T10:00:00.000Z', end: '2019-08-01T13:00:00.000Z' },
{ start: '2019-09-01T10:00:00.000Z', end: '2019-09-01T13:00:00.000Z' },
{ start: '2019-10-01T10:00:00.000Z', end: '2019-10-01T13:00:00.000Z' },
{ start: '2019-11-01T11:00:00.000Z', end: '2019-11-01T14:00:00.000Z' },
{ start: '2019-12-01T11:00:00.000Z', end: '2019-12-01T14:00:00.000Z' }

推荐答案

因为我不熟悉这些时区(并且不知道预期输出"应该位于哪个时区),所以还不清楚您期望发生什么.但是,正如@JorgeFuentesGonzález在评论中指出的那样,问题是可能是您没有在正确的时区中提供start日期时间.

Because I'm not familiar with these timezone's (and don't know what timezone the "expected output" is suppose to be in), it's a little unclear what you expect to happen. But as @JorgeFuentesGonzález pointed out in a comment, an issue is probably that you aren't providing the start datetime in the proper timezone.

rSchedule源代码(应该在VSCode之类的编辑器中方便地显示为工具提示- edit ),我已经在rSchedule中进行了澄清以及文档),Rule对象的timezone config选项不会更改规则所在的时区,它会更改规则显示 所在的时区.这种区别是对于rSchedule的内部功能很重要,但是我可以看到在这种情况下还不清楚.

As pointed out in the rSchedule source code (which should conveniently show up as a tooltip in editors like VSCode--edit I've gone ahead and clarified this in the rSchedule docs as well), the timezone config option for Rule objects does not change the timezone the rule is in, it changes the timezone the rule is displayed in. This distinction is important for the internal functioning of rSchedule, but I can see in this case it is unclear.

因此您的规则正在本地时区中生成事件(因为start: moment(Date.UTC(2019, 0, 1))会生成本地moment),然后将这些事件转换为'Europe/Berlin'时区,然后再输出.除了等待!那不是正在发生的事情.此RuleSchedule的一部分,因此,调度的时区决定了事件发生的输出时区.该时间表似乎没有时区(因此它在本地时区中).因此,我认为规则位于本地时区,而输出日期位于本地时区.

So your rule is generating occurrences in your local timezone (because start: moment(Date.UTC(2019, 0, 1)) generates a local moment), and then those occurrences are being transformed into the 'Europe/Berlin' timezone before being output. Except wait! That's not what's happening. This Rule is part of a Schedule, so it's the schedule's timezone which determines the output timezone of occurrences. The schedule appears to have no timezone (so it is in the local timezone). So I think the rule is in the local timezone, and the output dates are in the local timezone.

  • 注意:使用toISOString()进行控制台日志记录可能会混淆以下事实,即您的输出日期位于本地时区,而不是您期望的时区.
  • Note: console logging using toISOString() may be obfuscating the fact that your output dates are in your local timezone, rather than whatever timezone you expect.

根据您要完成的工作,我建议类似以下内容:

Depending on what you are trying to accomplish, I'd suggest something like the following:

import { Schedule } from "@rschedule/core/generators";
import '@rschedule/moment-tz-date-adapter/setup';
import moment from 'moment-timezone';

const schedule = new Schedule({
  rrules: [
    {
      frequency: "MONTHLY",//frequency: "WEEKLY",
      duration:1000 * 60 * 60 * 3,//input.workingHours[0].end-input.workingHours[0].start,
      byHourOfDay:[12],//input.workingHours[0].end
      start: moment.tz(Date.UTC(2019, 0, 1), 'Europe/Berlin'),
      end: moment.tz(Date.UTC(2020, 0, 0), 'Europe/Berlin')
    }
  ],
  timezone:'Europe/Berlin'
});

此处的Codesandbox演示.

让我知道您是否仍然没有看到期望的结果.

Let me know if you're still not seeing what you expect.

这篇关于如何产生带有时间偏移的循环调度持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆