Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6757309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:38:01+00:00 2026-05-26T13:38:01+00:00

I am writing a program in C# to convert between times from different countries

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:38:02+00:00Added an answer on May 26, 2026 at 1:38 pm

    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.

    // Master conversion function
    // Example use 1: convertTimezone("1/11/2011 00:00:00", "GMT Standard Time", "Eastern Standard Time") = "31/10/2011 20:00:00"
    // Example use 2: convertTimezone("1/11/2011 00:00:00", "Eastern Standard Time", "GMT Standard Time") = "01/11/2011 04:00:00"
    // Example use 3: convertTimezone("1/10/2011 00:00:00", "Eastern Standard Time", "GMT Standard Time") = "01/10/2011 05:00:00"
    string convertTimezone(string time, string oldlocation, string newlocation)
    {
        long l = convertTime_AnyToFile(time, oldlocation);
        string newtime = convertTime_FileToAny(l, newlocation);
        return newtime;
    }
    
    // Example use 1: convertTime_AnyToFile("1/11/2011 00:00:00", "GMT Standard Time") =    129645792000000000
    // Example use 2: convertTime_AnyToFile("1/11/2011 00:00:00","Eastern Standard Time") = 129645936000000000
    long convertTime_AnyToFile(string time, string location)
    {
        DateTime a = DateTime.Parse(time  );
        DateTime b = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(a, location, "UTC");
        return b.ToFileTime();
    }
    
    // Example use 1: convertTime_FileToAny(129645792000000000, "GMT Standard Time") = "1/11/2011 00:00:00"
    // Example use 2: convertTime_FileToAny(129645936000000000,"Eastern Standard Time") = "1/11/2011 00:00:00"
    string convertTime_FileToAny(long time, string location)
    {
        DateTime a = DateTime.FromFileTimeUtc(time);
        DateTime b = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(a, location);
        return b.ToString();
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program that convert TM-2 degree to lat/lon on Android, but
I'm writing a C# program to convert a FoxPro database to XML, and everything
If you are writing a program that is executable from the command line, you
I'm writing a program to read from a POP3 mailbox and upload the email
I'm writing a Bison/Flex program to convert LaTeX into MathML. At the moment, dealing
I'm writing a small program to convert a standard definition 4:3 video to a
I'm writing a C++ program which reads a texture image file then convert and
I'm writing a VisualC++ program which have code invoke ffmpeg.exe to convert video file.
I am writing some code for driving Internet Explorer from a Perl 5 program
I am writing a program that stores data in a dictionary object, but this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.