I'm building a Discord bot using C# and .NET Core. This bot is supposed to help me convert times between timezones.
Currently, I have this class:
class Resources
{
public static readonly HashSet<string> Timezones = new HashSet<string>(TimeZoneInfo.GetSystemTimeZones().Select(tz => tz.Id));
public static TimeSpan idToUTCOffset(string id)
{
var t = TimeZoneInfo.FindSystemTimeZoneById(id);
return t.GetUtcOffset(DateTime.Now);
}
}
So I have a container which contains all timezone ID's and a function that can convert such an ID to an offset relative to the current UTC time.
The problem with this approach is that the contents of Timezones
is very dependent on the OS on which the bot is running.
On my windows computer, where I develop and test, I get a nice list with timezones: "Eastern Standard Time", "European Standard Time", etc.
On my raspberry pi, which runs on Raspbian, I get a different list, which appears to be a list of countries/cities which is much larger and much less readable than the list on my windows device.
I would like to get a compact list of all timezones and their details (Start/end of DST or an object that has a function that takes care of DST automatically), like the one I get on my windows device, but then I want it to be OS independent or at least it should work on my raspberry pi.
Is there a way to get a list of all timezones in a OS-independent way in .NET Core?
In the end, I want this to work on my raspberry pi, but it would be convenient if it works regardless of the OS.
EDIT:
Here's more context to make my question more clear:
The idea of my discord bot is that when somebody asks for the time with a command, the bot will return a table with the time, expressed in all relevant timezones.
Each member in my discord assigns themselves a role, which carries the name of the timezone in which they reside, so I know from every user in which timezone they reside, by the role that they are assigned.
The list needs to be as short as possible, so that the table of relevant times stays as short as possible. There are for example multiple people from Canada in my discord who reside in the same timezone, but not in the same city. If I have a list of each city in Canada, then I get a lot of redundant data in my table.
On top of that, discord supports a very limited amount of roles, so I need the list to be as small as possible.
Using TimeZoneConverter, the following will return consistent results on any platform:
This will give you the current UTC offset of any time zone. The id
can be either those from either OS:
TimeZoneInfo tz = TZConvert.GetTimeZoneInfo(id);
TimeSpan offset = tz.GetUtcOffset(DateTime.UtcNow);
This will list all of the Windows time zone IDs:
var list = TZConvert.KnownWindowsTimeZoneIds;
This will list all of the IANA time zone names:
var list = TZConvert.KnownIanaTimeZoneNames;
You might also be interested in using TimezoneNames to offer a human-readable list of display names, though it can't yet offer the exact Windows display name that you would see from TimeZoneInfo.DisplayName
on Windows. That functionality is on the backlog.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With