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

后端 未结 3 2099
执笔经年
执笔经年 2020-12-28 09:22

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

相关标签:
3条回答
  • 2020-12-28 09:37

    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!

    0 讨论(0)
  • 2020-12-28 09:41

    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!");
    }
    
    0 讨论(0)
  • 2020-12-28 09:47

    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)

    0 讨论(0)
提交回复
热议问题