I have a log file that contains date/time strings formatted like this:
2019/10/01 15:30
As you can see, the strings don't contain any timezone information. However, I do know the timezone that the time is represented within.
If I use this C# code on my own PC, which has its local timezone set correctly, the time is parsed as expected. I'm expecting to convert the local time to UTC for inserting into a database.
Time.ParseExact("2019/10/01 15:30", "yyyy/MM/dd HH:mm", provider, DateTimeStyles.AdjustToUniversal & DateTimeStyles.AssumeLocal);
However, if I run this code on one of my Docker hosts, which has its timezone set to UTC, the times are parsed as UTC. This makes sense, since on a Docker host using UTC AssumeLocal
will be the same as UTC.
Simply applying a manual offset isn't sufficient due to daylight saving time. Also, some of these logs are historical and go back to prior to the DST change, so even using an algorithm would be complex and it seems like it's something that should already be solved (i.e. given a date and timezone, determine if it's in DST or not and then adjust to UTC accordingly)
What I want to be able to do is to explicitly specify the timezone for the timestamp and have the framework use both the given timezone and the actual date to properly convert the time to UTC. I've looked at this question and this question but they all seem to depend on the system itself being set to the timezone that you want to assume the date is in.
The second question does seem to start addressing the issue of assuming a known timezone, but then the responses just use GetUtcOffset
on the parsed time to determine the amount of time to offset the time. This assumes that the time was already parsed in the desired timezone.