我遇到以下问题。我正在使用icalendar gem(https://github.com/icalendar/icalendar)创建.ics日历文件。例如,我及时这样写:
def make_ical(email)
cal = Icalendar::Calendar.new
cal.event do |e|
e.dtstart = Icalendar::Values::Datetime.new('20141230T193400')
e.dtend = Icalendar::Values::Datetime.new('20141230T213400')
..
end
end
但是当它构建 .ics 文件时,它显示的时间与我输入的时间不同。我怀疑它会将其转换为某种时区。我怎样才能阻止它这样做?或者也许有一种方法可以更改初始时间,因此如果必须转换,它会正确转换。请帮忙。
calendar.ics
文件:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:icalendar-ruby
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20141231T173138Z
UID:...
DTSTART:20141011T081000
DTEND:20141012T081000
CLASS:PRIVATE
DESCRIPTION:Blah blah blah
SUMMARY:Secrets of Alchemy
BEGIN:VALARM
ACTION:EMAIL
TRIGGER:-P1D
DESCRIPTION:Event reminder
SUMMARY:Alarm notification
ATTENDEE:
ATTENDEE:
END:VALARM
END:VEVENT
END:VCALENDAR
不要注意结束时间,但在任一情况下,日期时间的时间部分都会发生变化。我想让它以某种方式显示正确的时间。
最佳答案
在 Gemfile 中添加:-
gem 'tzinfo'
gem 'tzinfo-data'
然后进行更改:-
require 'icalendar/tzinfo'
require 'tzinfo'
cal = Icalendar::Calendar.new
event_start = DateTime.new 2014, 12, 30, 19, 34, 00
event_end = DateTime.new 2014, 12, 30, 21, 34, 00
tzid = "America/Chicago"
tz = TZInfo::Timezone.get tzid
timezone = tz.ical_timezone event_start
cal.add_timezone timezone
cal.event do |e|
e.dtstart = Icalendar::Values::DateTime.new event_start, 'tzid' => tzid
e.dtend = Icalendar::Values::DateTime.new event_end, 'tzid' => tzid
end
将此 ics 作为电子邮件附件发送:--
attachments["schedule.ics"] = {:mime_type => 'text/calendar',
:content => cal.to_ical}
注意:-
event_start
和event_end
应与tzid
在同一时区,然后它会自动根据时区转换正确的时间此 ics 文件将在其中打开。
关于ruby-on-rails - Ical 更改了我在制作 .ics 文件时输入的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27724497/