[Mono-list] Possible bug in ConvertTimeToUtc(DateTime, TimeZoneInfo)?

KevinReay kevintreay at gmail.com
Fri Jul 25 15:43:48 EDT 2008


Ok, I tested the proposed changes locally (with changes/corrections) and they
seem to work, at least for me.

I've attached a (unified) diff for the changes I made, if anyone is
interested (also attached inline). Diff seems to solve problem described
above, and includes minor spelling/grammar tweak.

Thanks, 
Kevin

--- TimeZoneInfo.cs.old 2008-07-25 10:48:16.000000000 -0500
+++ TimeZoneInfo.cs     2008-07-25 10:53:07.000000000 -0500
@@ -222,10 +222,7 @@
                                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.Local)
-                               throw new ArgumentException ("Kind propery
of dateTime is Local but the sourceTimeZone does not equal
TimeZoneInfo.Local");
+                               throw new ArgumentException ("dateTime Kind
value is Utc but sourceTimeZone is not Utc");

                        if (sourceTimeZone.IsInvalidTime (dateTime))
                                throw new ArgumentException ("dateTime
parameter is an invalid time");
@@ -236,7 +233,7 @@
                        if (dateTime.Kind == DateTimeKind.Utc)
                                return dateTime;

-                       if (dateTime.Kind == DateTimeKind.Local)
+                       if (dateTime.Kind == DateTimeKind.Local &&
sourceTimeZone == TimeZoneInfo.Utc)
                                return ConvertTimeToUtc (dateTime);

                        if (sourceTimeZone.IsAmbiguousTime (dateTime) ||
!sourceTimeZone.IsDaylightSavingTime (dateTime))

http://www.nabble.com/file/p18658416/TimeZoneInfo.cs.diff
TimeZoneInfo.cs.diff 


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--tp18648344p18658416.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list