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).
As has been pointed out in a similar question, one needs to be careful since Windows’ uses very misleading names (e.g.: the bizarre “Romance Standard Time” for Europe/Paris or “Eastern Standard Time” which strangely acts as ET and uses daylight saving time, when the official definition says otherwise. Not to mention US Eastern Standard Time).
Regarding mapping of cities/states/countries from Windows’ Time Zones, I haven’t yet found any data file for that, but I’ll keep looking. I’m guessing one may need to find data to convert from Windows time zone names to Olson Time Zones, and then from there to individual cities/states/country names. This link will help with the former:
http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html
With that in mind, I’ve filled in the empty functions from the main question (thanks to Jon and Yahia for the pointers). The first one is the master function and is the one that will be useful for most people. The other two are secondary functions which the first function uses.
I’ve checked them and they work for daylight saving time, although I bet Jon Skeet and others will be able to find some ahem, ‘minor’ flaws. For example, dates before around 1601 won’t work as Windows’ File Time doesn’t support that. It’s also recommended to wrap calls to these functions in try/catch statements of course to trap false date input etc.
Although it appears to work okay (under the limited tests I’ve done), before I ‘tick’ my own answer, I might wait for some feedback first as maybe someone can improve the above code.