Home > .NET, Vs2012 > ToUniversalTime() with UTC format problem under IIS Express

ToUniversalTime() with UTC format problem under IIS Express

2013/07/01

Working with dates coming from a GPS system i have discovered a strange problem with IIS Express.

I have a .NET 3.5 web service in Visual Studio 2012 running (was running…) under IIS Express; one method merge two DataTable , in the first DataTable i have dates referred to Greenwich fuse, instead the second DataTable has dates referred to the local (Rome) fuse.

So i must convert the second dates (Rome) in UTC dates: that is in the second table i have dates as 20130701 110012+00, instead in the first DataTable i have 20130701 100012+02; the firstdate must be 20130701 090012+02.

This because the field is a string and is requested to order by DESC into this field.

The dates are in a field named DataOraCrono and is a string type: so i wrote a classic foreach (DataRow row in … where to change the field as

DateTime utc = DateTime.UtcNow;
DateTime datCurr = DateTime.Now;
int timDiff = (utc - datCurr).Hours;
DateTime datCorrect2;
DateTime datTimeGmt;
// inside foreach
datTimeGmt = DateTime.ParseExact(row["DataOraCrono"].ToString(), "yyyyMMdd HHmmsszz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
datCorrect2 = datTimeGmt.AddHours(timDiff);
row["DataOraCrono"] = datCorrect2.ToUniversalTime().ToString("yyyyMMdd HHmmsszz");

The last two “z” means that i would format with the UTC difference.

But i got a strange problem: at runtime this instruction causes a DateTimeInvalidLocalFormat


But the strange thing was that i put a breakpoint and DON’T executing the line but evaluating it in the QuickWatch


It was working !

In the same solution i created a normal ASP.NET web application (always 3.5) and in the Page_Load this code works; but running under a IIS 7.5 site: this gave me a hint…

The problem was that the Web service project was using IIS Express: converting the project to a true IIS 7.5 Web Application the problem is vanished.

Advertisements
Categories: .NET, Vs2012
%d bloggers like this: