开发者

timespan and UTC

开发者 https://www.devze.com 2023-01-09 10:16 出处:网络
I have a Timespan variable, which has time in local time zone but for a database ( cold type 开发者_JAVA技巧time) I need to pass UTC . How do I do this?

I have a Timespan variable, which has time in local time zone but for a database ( cold type 开发者_JAVA技巧time) I need to pass UTC . How do I do this? Also I need to get UTC to Local time to populate Timespan variable on pageLoad. Thanks!!!


I suppose I would load the TimeSpan into a DateTime, then get the universal time from the DateTime and convert it back again.

var dt = new DateTime( timeSpan.Ticks );
var utc = dt.ToUniversalTime();


A problem I found with a couple of the previous answers is that creating a DateTime from ticks results in a date/time such as 0001-01-01 04:00:00. If you're in a timezone with a positive UTC offset when the framework attempts to subtract say 10 hours it goes below DateTime.MinValue and while it doesn't throw an exception you end up with a TimeSpan containing 00:00:00 instead of the correct result.

The answer by Ali Sepehri.Kh gets around this underflow by using .AddDays(1) however because the date is always January 1 for areas that observe DST you can end up with an incorrect result. Because I was only interested in timespans for the current day I ended up using a modified version of Ali's code that uses the current date as the base and adds the TimeSpan to it:

public static TimeSpan LocalTimeSpanToUTC(TimeSpan ts)
{
    DateTime dt = DateTime.Now.Date.Add(ts);
    DateTime dtUtc = dt.ToUniversalTime();
    TimeSpan tsUtc = dtUtc.TimeOfDay;
    return tsUtc;
}

public static TimeSpan UTCTimeSpanToLocal(TimeSpan tsUtc)
{
     DateTime dtUtc = DateTime.UtcNow.Date.Add(tsUtc);
     DateTime dt = dtUtc.ToLocalTime();
     TimeSpan ts = dt.TimeOfDay;
     return ts;
}


class TimeConversion
{
    public static TimeSpan LocalTimeSpanToUTC(TimeSpan ts)
    {
        DateTime dt = new DateTime(ts.Ticks).AddDays(1);
        DateTime dtUtc = dt.ToUniversalTime();
        TimeSpan tsUtc = dtUtc.TimeOfDay;

        return tsUtc;
    }

    public static TimeSpan UTCTimeSpanToLocal(TimeSpan tsUtc)
    {
        DateTime dtUtc = new DateTime(tsUtc.Ticks).AddDays(1);
        DateTime dt = dtUtc.ToLocalTime();
        TimeSpan ts = dt.TimeOfDay;

        return ts;
    }
}


TimeSpan LocalTimeToUTCTime(TimeSpan localTime)
{
    var dt = new DateTime(localTime.Ticks);
    var utc = dt.ToUniversalTime();
    return new TimeSpan(utc.Ticks);
}


Ran into this today. The best way to do this would be to add the TimeSpan to a DateTime without a time component.

var dateTimeLocal = DateTime.Today.Add(timeSpan);

The DateTimeKind for this will be set to DateTimeKind.Local by default. Set DateTimeKind to unspecified because this is not actually local time (which is wherever this code will be running) but rather the time in the original timezone:

var dateTimeUnspecified = DateTime.SpecifyKind(dateTimeLocal, DateTimeKind.Unspecified);

Then convert the DateTime to UTC with the source TimeZoneInfo (the timezone that the original time was meant to be in) (this is the second parameter sourceTimeZone):

var utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTimeUnspecified, sourceTimeZone);

Voila!

0

精彩评论

暂无评论...
验证码 换一张
取 消