C#: Making sure DateTime.Now returns a GMT + 1 time

情到浓时终转凉″ 提交于 2019-12-30 00:00:14

问题


I am using DateTime.Now to show something according to today's date, and when working locally (Malta, Europe) the times appear correctly (obviously because of the Time Zone) but ofcourse when I upload it to my hosting server (USA), DateTime.Now does not represent the correct time zone.

Therefore, in my code, how can I convert DateTime.Now to correctly return the time from a GMT + 1 timezone ?


回答1:


Use the TimeZoneInfo class found in System.Core;

You must set the DateTimeKind to DateTimeKind.Utc for this.

DateTime MyTime = new DateTime(1990, 12, 02, 19, 31, 30, DateTimeKind.Utc);

DateTime MyTimeInWesternEurope = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(MyTime, "W. Europe Standard Time");

Only if you're using .Net 3.5 though!




回答2:


It depends on what you mean by "a GMT + 1 timezone". Do you mean permanently UTC+1, or do you mean UTC+1 or UTC+2 depending on DST?

If you're using .NET 3.5, use TimeZoneInfo to get an appropriate time zone, then use:

// Store this statically somewhere
TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("...");
DateTime utc = DateTime.UtcNow;
DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone );

You'll need to work out the system ID for the Malta time zone, but you can do that easily by running this code locally:

Console.WriteLine(TimeZoneInfo.Local.Id);

Judging by your comments, this bit will be irrelevant, but just for others...

If you're not using .NET 3.5, you'll need to work out the daylight savings yourself. To be honest, the easiest way to do that is going to be a simple lookup table. Work out the DST changes for the next few years, then write a simple method to return the offset at a particular UTC time with that list hardcoded. You might just want a sorted List<DateTime> with the known changes in, and alternate between 1 and 2 hours until your date is after the last change:

// Be very careful when building this list, and make sure they're UTC times!
private static readonly IEnumerable<DateTime> DstChanges = ...;

static DateTime ConvertToLocalTime(DateTime utc)
{
    int hours = 1; // Or 2, depending on the first entry in your list
    foreach (DateTime dstChange in DstChanges)
    {
        if (utc < dstChange)
        {
            return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local);
        }
        hours = 3 - hours; // Alternate between 1 and 2
    }
    throw new ArgumentOutOfRangeException("I don't have enough DST data!");
}



回答3:


I don't think that you can set a property in your code that will make DateTime.Now return anything else than the current time of the computer in which the code executes. If you want to have a way of always getting another time, you will probably need to wrap in another function. You can can do the round-trip over UTC and add the desired offset:

private static DateTime GetMyTime()
{
    return DateTime.UtcNow.AddHours(1);
}

(code sample updated after Luke's comment on the inner workings of DateTime.Now)



来源:https://stackoverflow.com/questions/1108644/c-making-sure-datetime-now-returns-a-gmt-1-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!