views:

892

answers:

2

Why does the ToUniversalTime function have no effect here;

DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Local);
dt = dt.ToUniversalTime(); // convert BST to UTC ?
dt.ToString();

"24/03/2009 01:00:00" ... wrong?

Is the same as..

DateTime dt = new DateTime(2009,3,24,1,0,0,DateTimeKind.Utc);
dt = dt.ToUniversalTime(); // nothing to do, already utc
dt.ToString();

"24/03/2009 01:00:00" ... correct.

I expected there to be an adjustment to the ToString() value of the first example, where by the DateTime specified as Local would result in a corresponding TimeZone calculation upon the call to ToUniversalTime() and the time in the UK should have resulted in "24/03/2009 00:00:00" as UTC.

However it appears like the specifying of the DateTimeKind in this way renders ToUniversalTime or ToLocalTime unable to make any calculation.

+4  A: 

Are you in the UK by any chance? Although we are now in daylight saving time, the date you specify in your code is before this switched over, so local and UTC time in the UK are the same. If you specify April as your month, then you will see a one hour difference.

David M
A: 

Cheers David M. Not had my breakfast. Indeed, when I repeat the test with dates that elapse the BST summer-time threshold, the behaviour is of course correct.

 DateTime dt = new DateTime(2009,4,24,1,0,0,DateTimeKind.Local); 
 dt = dt.ToUniversalTime(); // convert BST to UTC ? 
 dt.ToString(); // "24/04/2009 00:00:00" ... correct

And to confirm, the ToString() method appears to output based on the Kind property.

Terry Carvin