TimeSpan utc0 to timezone and some days

Hi i have some table with starttime end time and timezone

I get data in DateTime.Utc

http://prntscr.com/a84tr3

And how i can check if the date what comes is get caught in this timezome from DB and this day

0-Monday... 5-Friday

If we have 0-1 this means start time in 23:00 Sunday to 22:00 Monday

public static DateTime GetDateTimeWithExchangeTimeZoneSessionStart(DateTime tickDateTime, string exchangeTimeZone)
    {
        DateTime convertedDateTime = tickDateTime;
        switch (exchangeTimeZone)
        {
            case "5.5":
                convertedDateTime = convertedDateTime.AddHours(-5.5);
                break;
            case "4":
                convertedDateTime = convertedDateTime.AddHours(-4);
                break;
            case "-6":
                convertedDateTime = convertedDateTime.AddHours(6).AddDays(-1);
                break;
            case "0":
                break;

        }
        return convertedDateTime;
    }

But it's not good i think

Jon Skeet
people
quotationmark

Firstly, it's important to understand that what you've got isn't a time zone. It's a UTC offset (although the negation of what it would normally be...). A real time zone would need to indicate how that UTC offset varies over time (e.g. due to daylight saving time changes).

However, with the data you've got, it looks like all you need is:

double hours = double.Parse(exchangeTimeZone, CultureInfo.InvariantCulture);
TimeSpan offset = TimeSpan.FromHours(hours);
return tickDateTime - offset;

However:

  • It would be cleaner to use DateTimeOffset given that you really do have a DateTime and an offset
  • Be really, really careful around DateTime.Kind... basically, DateTime is somewhat broken.
  • (Shameless plug) Consider using my Noda Time project to make your code much clearer for this and all your other date/time code

people

See more on this question at Stackoverflow