GenDeveloper

Technology Tips and Insights for CA Gen Developers

Gen Timestamps and Time Zones

March23

We are currently building a custom Web UI for a set of .NET servers developed with CA GEN by wrapping the generated proxies with custom C# code and exposing them to the new Web UI as web services. In my previous post Ticks and Milliseconds I described how we convert the .NET DateTime objects to millisecond values and back.

But even after we got the conversion working, we still saw problems with dates on the Web UI: They were always 5 or 6 hours behind compared to the dates in the database. These numbers looked familiar to me – they happen to be the offset of my local time zone (Central Time) to GMT (Greenwich Mean Time) for daylight savings and standard time. So here was another problem: time zones.

In C# Gen code, dates, times and timestamps are represented by DateTime objects without time zone information while JavaScript interprets millisecond dates as UTC (Coordinated Universal Time, based on GMT). Since my browser knows from my operating system where I’m located, it adjusted the dates by the offset to GMT.

This issue does not only apply to DateTime objects returned by Gen code – many database date types are not time zone aware (for example, the SQL Server 2005 DATETIME and Oracle 9i DATE types).

After some research, I found a solution. The Ticks-to-Millisecond conversion is described in my previous post.

  1. In the C# wrapper of the Gen proxy, add local time zone information to the DateTime object returned by Gen and then convert it to UTC:
    public static DateTime ToUTC(DateTime noTimeZone)
    {
         return new DateTime(noTimeZone.Ticks, DateTimeKind.Local)
                           .ToUniversalTime();
    }
  2. Convert UTC DateTime object to Milliseconds and send to the UI.
  3. Finally, JavaScript converts it back from the millisecond value to a Date object:
    var d = new Date(milliseconds);

When sending date values from the UI to the server, this sequence is reversed:

  1. Obtain UTC in milliseconds from JavaScript Date object and then send it to the .NET application:
    var millis = d.getTime();
  2. In the C# Gen proxy wrapper, convert milliseconds to Ticks and create a new DateTime object.
  3. Convert that DateTime object, which represents a UTC date, to local time – it can now be passed to a Gen Import View.
    public static DateTime UtcToLocal(DateTime utc)
    {
         return utc.ToLocalTime();
    }

Now all dates, times and timestamps are stored and internally handled in the local time zone of the server/database while the users see them in their own time zone.

Anke

posted under .NET, CA Gen | No Comments »