为重复活动的开始时间添加持续时间,以便结束时间始终是同一时间(即下午2点至下午4点) [英] Add a duration to a repeating event's start time so that it's end is always the same time (i.e 2pm to 4 pm)

查看:225
本文介绍了为重复活动的开始时间添加持续时间,以便结束时间始终是同一时间(即下午2点至下午4点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆规则(在 rrule.js 中实现)事件开始时间(请参见演示)。 rrule.js 不会 t实际上提供了事件持续时间的概念结束时间 ...因此它可以告诉我确切的日期,即重复事件的百万次发生将开始的时间,不是何时结束。原来,我实际上是想知道活动何时结束,因此我必须发挥创造力。据我所知,我有两个选择

I have a bunch of rrules (implemented in rrule.js) that gives me an array of event start times (see the demo). rrule.js doesn't actually provide the concept of an event duration or endtime... So it can tell me the precise date when the millionth occurrence of a repeating event will start but not when it will end. Turns out I actually want to know when an event ends so I'll have to get creative. As far as I see it I've got two options


  1. 数据库侧:存储规则字符串+事件持续时间

客户端:重新构造事件是从规则字符串开始的日期数组。只有开始时间是已知的,而结束时间通过将持续时间作为偏移量添加到每个开始时间中来计算数组。

CLIENT SIDE: Reconstitute events start date array from rrule string. Only start times would be known and end times would be calculated by adding the duration as an offset to each start time in the array.

数据库侧:存储修改后的规则字符串,该字符串编码结束时间

DB SIDE: Store a modified rrule string which encodes an endtime.

客户端:特殊的包装函数读取修改后的rrule字符串,并将其重构为两个日期数组;一种表示事件的开始时间,另一种表示事件的结束时间。

CLIENT SIDE: A special wrapper function reads the modified rrule string and reconstitutes it as two date arrays; one representing event start times and the other end times.

选项1似乎更容易,但我怀疑它将遇到问题与夏时制。例如,假设我有一个活动,该活动在每个星期二的晚上6点至星期三的凌晨2点。在这种情况下,我将在数据库中存储8个小时的时间以及该字符串规则。现在,让我们前进到将来的星期二下午6点。我的活动是否总是在星期三凌晨2点结束(或者持续8个小时有时使我的活动在凌晨1点或凌晨3点结束)? 我如何让它总是在凌晨2点结束?

Option 1 seems easier but I suspect it will run into problems with daylight savings. For example, say I've an event that is every Tuesday from 6pm to 2 am Wednesday. In that case I'd store a duration of 8 hours in my database alongside that stringified rrule. Now let's fast forward to any 6pm Tuesday in the future. Does my event always end on Wednesday at 2am (or does that 8 hour duration sometimes make my event end at 1am or 3am)? How do I get it to always end at 2am?

...如果您知道答案,那就在这里停止阅读。

... If you know the answer then just stop reading here.

根据如何向JavaScript Date对象添加30分钟? >智能 偏移日期时间的方法是使用诸如moment.js之类的精美库。

According to Kip in How to add 30 minutes to a JavaScript Date object? the smart way to offset a date time is to use a fancy library like moment.js.

他强调了这一点,它展示了使用容易出错的地方非理想的日期时间库(显示天真的分钟偏移功能由于夏时制而失败)

He emphasizes that point by showing how easily things go wrong using non fancy date time libraries (showing how a naive minute offset function fails due to daylight savings)

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}
addMinutes(new Date('2014-11-02'), 60*24) //In USA, prints 11pm on Nov 2, not 12am Nov 3!

但是对我来说有些奇怪的事情发生了。上面的函数应该在11月2日输出 11pm -这是错误的答案,即由于夏令时而失败。当我运行它时,它实际上输出的是正确的时间11月3日 12am (注意:我在芝加哥/中部时间)。

But something weird happens for me. The function above was supposed to output 11pm on Nov 2 - which is the wrong answer i.e. it was supposed to fail because of daylight savings. When I run it, it actually outputs the right time 12am on Nov 3 (note: I'm in Chicago/Central time).

当我将其朴素函数的输出与moment.js和luxon.js的输出进行比较时,我得到的答案与您在可观察的笔记本中添加一个持续时间到一个这样的日期,以便该24小时。。

When I compare the output of his naive function to the output of moment.js and luxon.js, I get the same answer as you can see in this observable notebook.

此外,如果使用luxon或矩,当您在 2014-11-02 您会得到 2014-11-03T00:00:00.000Z ,但是如果您直接添加一天到 2014-11-02 ,您会得到 2014-11-03T01:00:00.000Z -一个小时的休假。

What's more, if using luxon or moment, when you add a days worth of minutes to 2014-11-02 you get2014-11-03T00:00:00.000Z but if you just directly add a day to 2014-11-02 you get 2014-11-03T01:00:00.000Z - it's an hour off.

所以我最好还是选择选项2?

So am I better off pursuing option 2?

推荐答案


现在让我们快进到将来的星期二下午6点。我的活动是否总是在星期三凌晨2点结束(或者持续8个小时有时使我的活动在凌晨1点或凌晨3点结束)?如何使它始终在凌晨2点结束?

Now let's fast forward to any 6pm Tuesday in the future. Does my event always end on Wednesday at 2am (or does that 8 hour duration sometimes make my event end at 1am or 3am)? How do I get it to always end at 2am?

标准的Javascript Date对象会自动为您处理夏令时。即使您在夏令时的前一天下午6点增加8小时的日期,新日期仍将在第二天凌晨2点结束。

The standard Javascript Date object automatically handles the daylight savings shift for you. Even if you add 8 hours to a date at 6pm the day before daylight savings, the new date will still end at 2am the next day.

顺便说一句,我实现了持续时间支持在 rSchedule 中,由于它同时支持标准javascript Date 以及时刻 / luxon 日期,您可以使用以下方式测试持续时间的重复事件

Incidently, I implemented duration support in rSchedule and since it supports both the standard javascript Date as well as moment/luxon dates, you can test a recurring event with a duration using either library and see that they both produce the same result.

此示例可以为在堆栈闪电中看到

import { Schedule } from '@rschedule/rschedule';
import { StandardDateAdapter } from '@rschedule/standard-date-adapter';

// This example will also work with `moment`, `moment-timezone`, and `luxon`
// (assuming you import the proper date adapter -- see rSchedule docs)

const schedule = new Schedule({
  rrules: [
    {
      start: new Date(2019,9,10,18),
      frequency: "DAILY",
      duration: 1000 * 60 * 60 * 8,
      count: 30
    }
  ],
  dateAdapter: StandardDateAdapter,
});

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




结果我实际上想知道事件何时结束

Turns out I actually want to know when an event ends

要找出此事件何时结束,您可以执行以下操作:

To find out when this event ends, you could do:

const iterator = schedule.occurrences({ reverse: true })

const { end } = iterator.next().value

此技巧仅适用于实际上具有结束日期的事件(因此不适用于无限次发生的事件)。

This trick would only work with an event that actually has an end date (so not an event with infinite occurrences).

这篇关于为重复活动的开始时间添加持续时间,以便结束时间始终是同一时间(即下午2点至下午4点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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