Reputation: 11
I'm developing an windows service application and I need to work with daylight saving times, once I have my app server running in one timezone and the db server (actually a web service) running in another one.
Every time when a new data is recorded at database, it's used the current local date time, that is different that datetime of my app server, so, when I need to find for results at db, I need to increase/decrease an offset to the datetime to find using the correct datetime, but, when daylight saving time changes I have troubles, because I need to change this offset manually, and it is causing a bunch of problems since we have to have somebody monitoring the system.
I would not like to work with any kind of other tools, websites and so on to get this information, we just need to work with Microsoft stuffs.
I've have been searching for ways to work with TimeZones at .NET 2.0 but I didn't find anything, my question is, does somebody here have faced any situation like this ?
Upvotes: 0
Views: 285
Reputation: 241808
If you only need to work with the local time zone of the machine where your .NET code is running, then you can use the TimeZone
class, which has been in .NET since version 1.1.
However, I think you are asking about working with other time zones than the local time zone. This is handled with TimeZoneInfo
in .NET 3.5 and above, but is not available to .NET 2.0 applications.
The best solution (IMHO) for working with multiple time zones in a .NET 2.0 application is the TZ4NET Library. It correctly implements the IANA time zone database, and has been maintained over the years. However, it is a third-party library and you said you did not wish to use anything that didn't originate from Microsoft.
Unfortunately, the only option that remains is to manually read the time zone data from the Windows Registry and implement your own code for interpreting it. There is a good blog post here that describes how TimeZoneInfo
reads from the registry, and describes the registry keys and data involved. A complete implementation is too broad for this post.
Upvotes: 0
Reputation: 35
It would be better to use UTC value to store in database and convert to local or any other TimeZone in the app
DateTime.Now.ToUniversalTime() DateTime.SpecifyKind(dt, DateTimeKind.Local);
http://msdn.microsoft.com/en-us/library/system.datetime.utcnow.aspx
Or you can create LocaizedDateTime
========
using System;
using System.Collections.Generic;
public class MyClass
{
public static void RunSnippet()
{
LocalizedDateTime dt = LocalizedDateTime.FromUtc(DateTime.UtcNow);
Console.WriteLine(dt.LocalDateTime.ToString("dd/MM/yyyy hhmmss"));
Console.WriteLine(dt.UtcDateTime.ToString("dd/MM/yyyy hhmmss"));
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
public class LocalizedDateTime
{
private LocalizedDateTime(DateTime localDateTime)
{
LocalDateTime = DateTime.SpecifyKind(localDateTime, DateTimeKind.Local);
}
public DateTime UtcDateTime{
get{
return LocalDateTime.ToUniversalTime();
}
}
public DateTime LocalDateTime{get; private set;}
public static LocalizedDateTime FromLocal(DateTime localDateTime)
{
return new LocalizedDateTime(localDateTime);
}
public static LocalizedDateTime FromUtc(DateTime utcDateTime)
{
return new LocalizedDateTime(TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.Local));
}
}
Upvotes: 0
Reputation: 15185
I have been using the windows registry to obtain the offsets for a long time. All locales are stored there with offsets for upcoming years. This data is automatically updated when you apply service packs and whatnot.
Here is a link that details implementation of an older method to wrap the locale information in the windows registry - circa .net 2.0 .
Upvotes: 1