全天活动日历宝石

发布于 2024-10-05 16:47:22 字数 687 浏览 4 评论 0原文

我正在使用下面的内容设置一个事件,以使用icalendar gem 导出到ical。

@calendar = Icalendar::Calendar.new

event = Icalendar::Event.new
event.dtstart = ev.start_at.strftime("%Y%m%d")
event.dtend = ev.end_at.strftime("%Y%m%d")
event.summary = ev.summary

@calendar.add

为了全天举办一个活动,它需要看起来像这样:

DTSTART;VALUE=DATE:20101117
DTEND;VALUE=DATE:20101119

现在我正在使用

event.dtstart = "$VALUE=DATE:"+ev.start_at.strftime("%Y%m%d")"

This will 输出

DTSTART:$VALUE=DATE:20101117

,然后我将所有“:$”替换为“;”有

@allday = @calendar.to_ical.gsub(":$", ";")

没有更直接的方法将日期保存为全天?

I am using the below to setup an event to export to ical with the icalendar gem.

@calendar = Icalendar::Calendar.new

event = Icalendar::Event.new
event.dtstart = ev.start_at.strftime("%Y%m%d")
event.dtend = ev.end_at.strftime("%Y%m%d")
event.summary = ev.summary

@calendar.add

In order to make an event all day it needs to look like this:

DTSTART;VALUE=DATE:20101117
DTEND;VALUE=DATE:20101119

Right now I am using

event.dtstart = "$VALUE=DATE:"+ev.start_at.strftime("%Y%m%d")"

This will output

DTSTART:$VALUE=DATE:20101117

and then I replace all ":$" with ";" with

@allday = @calendar.to_ical.gsub(":$", ";")

Is there a more direct way to save dates as all day?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-10-12 16:47:22

我对此进行了研究并找到了一种方法。您可以以键值对的形式为事件日期分配属性。所以你可以像这样分配 VALUE 属性:

event = Icalendar::Event.new
event.dtstart = Date.new(2010,12,1)
event.dtstart.ical_params = { "VALUE" => "DATE" }
puts event.to_ical

# output
BEGIN:VEVENT
DTSTAMP:20101201T230134
DTSTART;VALUE=DATE:20101201
SEQUENCE:0
UID:2010-12-01T23:01:34-08:00_923426206@ubuntu
END:VEVENT

现在是有趣的部分。给定一个日历,您可以创建一个事件并传入一个块,该块用其属性初始化日期:

calendar.event do
  dtstart Date.new(2010,11,17), ical_params = {"VALUE"=>"DATE"}
  dtend Date.new(2010,11,19), ical_params = {"VALUE"=>"DATE"}
end

I played around with this and figured out one way. You can assign properties to the event dates, in the form of key-value pairs. so you could assign the VALUE property like so:

event = Icalendar::Event.new
event.dtstart = Date.new(2010,12,1)
event.dtstart.ical_params = { "VALUE" => "DATE" }
puts event.to_ical

# output
BEGIN:VEVENT
DTSTAMP:20101201T230134
DTSTART;VALUE=DATE:20101201
SEQUENCE:0
UID:2010-12-01T23:01:34-08:00_923426206@ubuntu
END:VEVENT

Now the fun part. Given a calendar you can create an event and pass in a block which initializes the date with its properties:

calendar.event do
  dtstart Date.new(2010,11,17), ical_params = {"VALUE"=>"DATE"}
  dtend Date.new(2010,11,19), ical_params = {"VALUE"=>"DATE"}
end
羅雙樹 2024-10-12 16:47:22

所以这个线程看起来很旧(并且没有解决最新版本的icalendar gem - 2.3.0 的问题)。我最近不得不以 ics 格式创建“全天”日历事件。我发现这是一个更好的解决方案(并且似乎按照您期望日历处理它的方式工作) - 请参阅下面的代码片段 上面的

date = Date.new(2010,11,17)
event = Icalendar::Event.new
event.dtstart = Icalendar::Values::Date.new date
event.dtstart.ical_param "VALUE", "DATE"
event.dtend = Icalendar::Values::Date.new (date + 1.day)
event.dtend.ical_param "VALUE", "DATE"
puts event.to_ical

代码产生以下输出:

BEGIN:VEVENT
DTSTAMP:20150521T162712Z
UID:4c239930-15ba-44b4-a045-c6fae3d858d2
DTSTART;VALUE=DATE:20101117
DTEND;VALUE=DATE:20101118
END:VEVENT

请注意,日期没有关联的时间与它。先前回复中的代码当前生成时间。我必须深入研究icalendar的源代码才能找到这个解决方案。

我希望这对其他人有帮助。

干杯!

So this thread seems quite old (and did not solve the problem with the most recent version of the icalendar gem - 2.3.0). I've recently had to create "all day" calendar events in ics format. I've found this to be a much better solution (and seems to work the way you'd expect calendars to handle it) - see the snippet below

date = Date.new(2010,11,17)
event = Icalendar::Event.new
event.dtstart = Icalendar::Values::Date.new date
event.dtstart.ical_param "VALUE", "DATE"
event.dtend = Icalendar::Values::Date.new (date + 1.day)
event.dtend.ical_param "VALUE", "DATE"
puts event.to_ical

The above code produces the following output:

BEGIN:VEVENT
DTSTAMP:20150521T162712Z
UID:4c239930-15ba-44b4-a045-c6fae3d858d2
DTSTART;VALUE=DATE:20101117
DTEND;VALUE=DATE:20101118
END:VEVENT

Note that the Date does not have a time associated with it. The code in the prior reply currently produces the time. I had to dig into the source code for icalendar to figure out this solution.

I hope this helps someone else.

Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文