【问题标题】:Ignore time zone settings with vCalendar使用 vCalendar 忽略时区设置
【发布时间】:2015-09-01 01:15:11
【问题描述】:

我正在尝试创建一个 vCalendar 约会以发送给我们的客户。日期和时间是诊所预约的。生成 vCal 的服务器在东部时间,但我们的客户端遍布全国。时间和日期与时区无关,因为它是由他们所见的诊所设置的。我需要使用特定的日期和时间发送 vCal,而不是根据时区调整的日期和时间。这是我现在正在使用的代码,但无论我做什么,它都会根据客户端的时区进行调整,这会导致他们的约会时间不正确。

Private Function CreateCalendarItem(ByVal Mail As System.Net.Mail.MailMessage, ByVal PlainText As String, ByVal ApptDateTime As String) As System.Net.Mail.AlternateView

    Dim ApptDate As DateTime = CDate(ApptDateTime)

    Dim calendarEventText As StringBuilder = New StringBuilder
    calendarEventText.AppendLine("BEGIN:VCALENDAR")
    calendarEventText.AppendLine("METHOD:REQUEST")
    calendarEventText.AppendLine("PRODID:-//MyCompanyName")
    calendarEventText.AppendLine("VERSION:2.0")
    calendarEventText.AppendLine("BEGIN:VEVENT")
    calendarEventText.AppendLine("DTSTART:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("DTSTAMP:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("DTEND:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmss"))
    calendarEventText.AppendLine("UID:" & Guid.NewGuid().ToString)
    calendarEventText.AppendLine("DESCRIPTION:" & PlainText)
    calendarEventText.AppendLine("X-ALT-DESC;FMTTYPE=text/html:" & PlainText)
    calendarEventText.AppendLine("SUMMARY:" & Mail.Subject)
    calendarEventText.AppendLine("ORGANIZER:MAILTO:" & Mail.From.Address)
    If Mail.To.Count > 0 Then
        calendarEventText.AppendLine("ATTENDEE;CN=\" & Mail.To(0).DisplayName & "\;RSVP=FALSE:mailto:" & Mail.To(0).Address)
    Else
        calendarEventText.AppendLine("ATTENDEE;CN=\\;RSVP=FALSE:mailto" & "")
    End If
    calendarEventText.AppendLine("BEGIN:VALARM")
    calendarEventText.AppendLine("TRIGGER:-PT15M")
    calendarEventText.AppendLine("ACTION:DISPLAY")
    calendarEventText.AppendLine("DESCRIPTION:Reminder")
    calendarEventText.AppendLine("END:VALARM")
    calendarEventText.AppendLine("END:VEVENT")
    calendarEventText.AppendLine("END:VCALENDAR")
    Dim ct As System.Net.Mime.ContentType = New System.Net.Mime.ContentType("text/calendar")
    ct.Parameters.Add("method", "REQUEST")
    Dim avCal As System.Net.Mail.AlternateView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(calendarEventText.ToString(), ct)

    Return avCal

End Function

任何帮助将不胜感激。感谢您的意见。

【问题讨论】:

  • 看看at this thread 时间在 .NET 中的工作方式,具体取决于您运行的版本。显然TimeZoneInfo 是前进的方向。
  • 非常感谢您的回复。我正在尝试实施 TimeZoneInfo,但该结果没有变化。我仍然看到我的提醒已针对时区进行了调整。 Dim ApptDate As String = TimeZoneInfo.ConvertTimeToUtc(CType(ApptDateTime, DateTime)).ToString("yyyyMMddTHHmmss")
  • 那么您是如何存储 ApptDateTime 的呢?是 UTC 还是当地时间还是其他什么?
  • 这一定是问题所在。我将约会日期作为当地时间存储在我们的数据库中。那么有没有办法事后转换呢?再次感谢您的洞察力。
  • 在设置 vCalendar 日期之前,您将知道约会所在的时区并调整与东部时间的差异。见TimeZoneInfo.ConvertTime

标签: vb.net vcalendar


【解决方案1】:

使用以下内容:

calendarEventText.AppendLine("DTSTART:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))
calendarEventText.AppendLine("DTSTAMP:" & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))
calendarEventText.AppendLine("DTEND:"   & ApptDate.ToUniversalTime.ToString("yyyyMMddTHHmmssZ"))

请注意,我在 DATE-TIME 字符串格式的末尾添加了“Z”。 “Z”是 vCalendar 文件解析器知道日期时间值是 UTC 时间的方式。

See the RFC 5545 Date-Time spec info.

【讨论】:

  • 非常感谢,这个特殊的代码被搁置了,所以我很长时间没有做任何事情了。我确实相信我最初使用的是 Z 格式,但仍然存在反映 vCalendar 中正确时间的问题。我肯定会选择这个备份并用 Z 重新测试,如果它有助于解决我的问题,请告诉您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2012-12-18
  • 2011-09-26
  • 2018-12-08
  • 2020-08-28
  • 2020-06-10
相关资源
最近更新 更多