I am writing a program in C# to convert between times from different countries (something like this). After trekking through Stack Overflow, most people seem to be interested in converting using time zone codes (such as from say GMT to EDT or EST to BST). This strikes me as a little odd because you need to then go through another hurdle to take DST complications into account.
What would be far simpler (and more practical, at least for my purposes) is to simply specify the country/city/state you wish to convert time from/to. If instead we just focus on local time for now, I have created these 2 functions to convert from Local time to FileTime (essentially UTC) and back again.
// Example use: convert_LocalToFile("1/11/2011 00:00:00") = 129645792000000000
long convert_LocalToFile(string time)
{
DateTime dt = DateTime.Parse(time);
return dt.ToFileTime();
}
// Example use: convert_FileToLocal(129645792000000000) = "1/11/2011 00:00:00"
string convert_FileToLocal(long time)
{
return DateTime.FromFileTime(time).ToString();
}
However, it would be great if we solved this once and for all, and had functions which allowed you to specify the country/city too. The spec would be as follows:
// Example use 1: convert_AnyToFile("1/11/2011 00:00:00", "England") = 129645792000000000
// Example use 2: convert_AnyToFile("1/11/2011 00:00:00","New York") = 129645936000000000
long convert_AnyToFile(string time, string location) {
...
}
// Example use 1: convert_FileToAny(129645792000000000, "England") = "1/11/2011 00:00:00"
// Example use 2: convert_FileToAny(129645936000000000,"New York") = "1/11/2011 00:00:00"
string convert_FileToAny(long time, string location) {
...
}
So my question is two-fold: Can someone 'fill in' the above two empty functions to make them work, and also provide a way to get C# to list all the countries and cities that would be allowed as parameters.
------------------------EDIT: Instead of the country or city, I would also make do with the obscure (to me anyway) TZ codes as shown from this page: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
As has been pointed out, I would then need to find a data file to map from the tz database code to the country/city/region. In this case, substitute "string location" in the above 2 function templates with "string code". 'code' meaning the tz database time zone code (TZ), not the typical time zone code (such as EST or GMT).