Here is a more complete potential fix (not tested!): public static DateTime ConvertTimeToUtc (DateTime dateTime, TimeZoneInfo sourceTimeZone) { if (sourceTimeZone == null) throw new ArgumentNullException ("sourceTimeZone");
if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc) throw new ArgumentException ("Kind propery of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc"); if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Utc) throw new ArgumentException ("Kind propery of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local"); if (sourceTimeZone.IsInvalidTime (dateTime)) throw new ArgumentException ("dateTime parameter is an invalid time"); if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone == TimeZoneInfo.Utc) return dateTime; if (dateTime.Kind == DateTimeKind.Utc) return dateTime; if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Local) return ConvertTimeToUtc (dateTime); if (sourceTimeZone.IsAmbiguousTime (dateTime) || !sourceTimeZone.IsDaylightSavingTime (dateTime)) return DateTime.SpecifyKind (dateTime - sourceTimeZone.BaseUtcOffset, DateTimeKind.Utc); else { AdjustmentRule rule = sourceTimeZone.GetApplicableRule (dateTime); return DateTime.SpecifyKind (dateTime - sourceTimeZone.BaseUtcOffset - rule.DaylightDelta, DateTimeKind.Utc); } } Note the inclusion of the "sourceTimeZone == TimeZoneInfo.Local" test before the "return ConvertTimeToUtc (dateTime);" call. Thanks again, Kevin KevinReay wrote: > > Hi everyone, > > I think I may have spotted a bug in the > System.TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) method. I > could, of course, be wrong (clue-stick appreciated!). > > The following check appears to be wrong: > > if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != > TimeZoneInfo.Local) > throw new ArgumentException ("Kind propery of dateTime is Local but the > sourceTimeZone does not equal TimeZoneInfo.Local"); > > Shouldn't this code only check that if sourceTimeZone == TimeZoneInfo.Utc, > as the sourceTimeZone could be represent a local timezone while still not > being equal to TimeZoneInfo.Local (The system's local timezone)? > > As an example, the following code runs as expected under .Net/Windows: > > TimeZoneInfo _SpecifiedTimezone = > TimeZoneInfo.FindSystemTimeZoneById(SpecifiedTimeZone); > _NewMeeting.DateBegin = > TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(SpecifiedDate), > _SpecifiedTimezone); > > But fails with the following exception when run under mono: > > "Kind propery of dateTime is Local but the sourceTimeZone does not equal > TimeZoneInfo.Local" > > Thanks, > Kevin > -- View this message in context: http://www.nabble.com/Possible-bug-in-ConvertTimeToUtc%28DateTime%2C-TimeZoneInfo%29--tp18648344p18648612.html Sent from the Mono - General mailing list archive at Nabble.com. _______________________________________________ Mono-list maillist - Mono-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-list