美好的一天!
我正在尝试更新一些预先存在的代码,该代码使用 4.6.5 时区组件 VTIMEZONE 根据用户在 asp.net 网站上的表单中的输入创建会议请求。我所做的更新是删除作为枚举列出的时区静态列表,并用 TimeZoneInfo GetSystemTimeZones 方法替换它们。问题是VTIMEZONE的设置方式具有静态值。我想知道如何最好地解决这个问题,并欢迎任何建议。
下面是一段代码:
private const string vTimeZoneTemplate = @"
BEGIN:VTIMEZONE
TZID:Pacific
BEGIN:STANDARD
DTSTART:20071104T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:-0700
TZOFFSETTO:-0800
TZNAME:PST
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:20070311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:-0800
TZOFFSETTO:-0700
TZNAME:PDT
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VTIMEZONE
TZID:Eastern
BEGIN:STANDARD
DTSTART:20071104T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:20070311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
END:DAYLIGHT
END:VTIMEZONE";
最终目标是为所选时区设置会议请求的开始时间。 即我选择在 (UTC+02:00) 伊斯坦布尔举行 2 小时的会议,从下午 4:00 开始,它将在伊斯坦布尔时间(目前是土耳其标准时间)从下午 4:00 到下午 6:00 创建一个会议请求。
如果有帮助,我正在使用时区信息:
if (!IsPostBack)
{
System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> TimeZoneList = TimeZoneInfo.GetSystemTimeZones();
this.ddlTimezones.DataSource = TimeZoneList;
this.ddlTimezones.DataTextField = "DisplayName";
this.ddlTimezones.DataValueField = "Id";
this.ddlTimezones.DataBind();
}
这个:
//This string is used for timezones that observe DST
//RRULE:FREQ=YEARLY;BYDAY=[Week of Month][Day];BYMONTH=[Month]
private const string vTimeZoneTemplate1 = @"
BEGIN:VTIMEZONE
TZID:{0}
BEGIN:DAYLIGHT
DTSTART:{1}
RRULE:FREQ=YEARLY;BYDAY={9}{11};BYMONTH={7}
TZOFFSETFROM:{2}
TZOFFSETTO:{3}
TZNAME:{4}
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:{6}
RRULE:FREQ=YEARLY;BYDAY={10}{12};BYMONTH={8}
TZOFFSETFROM:{3}
TZOFFSETTO:{2}
TZNAME:{5}
END:STANDARD
END:VTIMEZONE";
//This string is used for timezones that do not observe DST; however, DAYLIGHT and STANDARD are required.
//Therefore used static dates and made no change to the UTCOffset
private const string vTimeZoneTemplate2 = @"
BEGIN:VTIMEZONE
TZID:{0}
BEGIN:DAYLIGHT
DTSTART:20070311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:{1}
TZOFFSETTO:{1}
TZNAME:{2}
END:DAYLIGHT
BEGIN:STANDARD
DTSTART:20071104T020000
RRULE:RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:{1}
TZOFFSETTO:{1}
TZNAME:{2}
END:STANDARD
END:VTIMEZONE";
而这个:
/**
* get the adjustment rules for the selected timezone.
* the adjustment rule is the Daylight Savings adjustment by default.
* using the i to note the most recent rule.
* DateStart and .DateEnd designate the begin and end of the rule.
* DaylightTransitionStart and End designate the begin and end date of DST.
* DayLightDelta is the difference between base UTC and the offset by DS, add to get DS value from Base.
**/
if (timezone.SupportsDaylightSavingTime)
{
TimeZoneInfo.AdjustmentRule[] adjustments = timezone.GetAdjustmentRules();
int i = adjustments.Length - 1;
string body = string.Format(vTimeZoneTemplate1
, timezone.Id
, adjustments[i].DateStart.ToString(DateTimeFormat)
, timezone.BaseUtcOffset
, adjustments[i].DaylightDelta.Add(timezone.BaseUtcOffset)
, timezone.DaylightName
, timezone.StandardName
, adjustments[i].DateEnd.ToString(DateTimeFormat)
, adjustments[i].DaylightTransitionStart.Month.ToString("MM")
, adjustments[i].DaylightTransitionEnd.Month.ToString("MM")
, adjustments[i].DaylightTransitionStart.Week.ToString()
, adjustments[i].DaylightTransitionEnd.Week.ToString()
, adjustments[i].DaylightTransitionStart.IsFixedDateRule
? adjustments[i].DaylightTransitionStart.Day.ToString().Substring(0, 2)
: adjustments[i].DaylightTransitionStart.DayOfWeek.ToString().Substring(0, 2)
, adjustments[i].DaylightTransitionEnd.IsFixedDateRule
? adjustments[i].DaylightTransitionEnd.Day.ToString().Substring(0, 2)
: adjustments[i].DaylightTransitionEnd.DayOfWeek.ToString().Substring(0, 2)) +
string.Format(vEventTemplate
, timezone.Id
, startDate
, endDate
, summary
, description
, Guid.NewGuid().ToString()
, sequence
, DateTime.Now.ToString(DateTimeFormat)
, vCalAttendees.ToString()
, organizer.FullName
, organizer.Email
, location
, string.IsNullOrEmpty(this.DescriptionHtml) ? string.Empty : string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", Email.WrapHTMLBody(this.DescriptionHtml, false, false))) +
(enableReminder ? vAlarmTemplate : string.Empty);
return string.Format(baseVCalTemplate, body);
}
else
{
string body = string.Format(vTimeZoneTemplate2
, timezone.Id
, timezone.BaseUtcOffset
, timezone.DisplayName) +
string.Format(vEventTemplate
, timezone.Id
, startDate
, endDate
, summary
, description
, Guid.NewGuid().ToString()
, sequence
, DateTime.Now.ToString(DateTimeFormat)
, vCalAttendees.ToString()
, organizer.FullName
, organizer.Email
, location
, string.IsNullOrEmpty(this.DescriptionHtml) ? string.Empty : string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", Email.WrapHTMLBody(this.DescriptionHtml, false, false))) +
(enableReminder ? vAlarmTemplate : string.Empty);
return string.Format(baseVCalTemplate, body);
}
}
为我工作。所以,我希望它也能帮助其他人。:)
有关当前信息,String.Format 和 TimeZoneInfo 的字段(如 BaseUtcOffset )应该对某些属性有所帮助
vTimeZoneTemplate = String.Format(
"BEGIN:VTIMEZONEn...{0}{1}...",
tz.BaseUtcOffset.Hours, tz.BaseUtcOffset.Minutes);
但我不确定您是否可以使用TimeZoneInfo收集足够的信息来构建完整的VTIMEZONE结构。