I have a method that creates a UTC DateTime
from user input, using the GMT offset of their geographical position:
public static DateTime LocalToUtc
(int year, int month, int day, int hour, decimal gmtOffset) {
// argument validation here
var dateTime = new DateTime(year, month, day).AddHours(hour);
var dateTimeOffset =
new DateTimeOffset(dateTime, TimeSpan.FromHours(gmtOffset));
return dateTimeOffset.UtcDateTime;
}
The problem is that this function is off by an hour if it's daylight savings in the user's timezone.
So while my personal GMT offset is -8, the current timezone offset is -7 because of daylight savings.
How do I change the function above to account for daylight savings? Don't I somehow need to create some timezone object from the GMT offset and get its timezone offset?