Hi i have some table with starttime end time and timezone
I get data in DateTime.Utc
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
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:
DateTimeOffset
given that you really do have a DateTime
and an offsetDateTime.Kind
... basically, DateTime
is somewhat broken. See more on this question at Stackoverflow