none
Where should I add TimeZone conversion information in a .net 4.5 console app RRS feed

  • Question

  • This is what I have:

    Loading data from databases, located on different serveres, that use different time zones (UTC, GMT etc).

    Manual converting loaded DateTime's using code similar to the below:

            private static DateTime ConvertTimeZoneToUserTime(DateTime time)
            {
                TimeZoneInfo tZ = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
                DateTime theUserTime = TimeZoneInfo.ConvertTime(time, tZ);
                return theUserTime;
            }

    My question is - is there an easier way to do this - eg. by adding the timezone info to the thread or similar - something that cause the manual stuff unnecessary ??

    BR

    Peter

    Monday, March 18, 2013 8:50 AM

Answers

  • All PC internally save the Time as UTC.  It is up to the application to display the internal time as local time.  Most Time functions use the TimeZone setting on the computer to convert the UTC to local time making the conversion unnecessary.  When storing a time that is in a string format you can specify the TimeZone is assumed to be local time unless you specify the time zone.  When loading Time from different servers, the standard transfer uses UTC so no conversion are required.  If the server is returning time not using UTC then a conversion is necessary.

    jdweng

    Monday, March 18, 2013 9:56 AM

All replies

  • All PC internally save the Time as UTC.  It is up to the application to display the internal time as local time.  Most Time functions use the TimeZone setting on the computer to convert the UTC to local time making the conversion unnecessary.  When storing a time that is in a string format you can specify the TimeZone is assumed to be local time unless you specify the time zone.  When loading Time from different servers, the standard transfer uses UTC so no conversion are required.  If the server is returning time not using UTC then a conversion is necessary.

    jdweng

    Monday, March 18, 2013 9:56 AM
  • Hi jdweng and thanks for your comments.

    When saying "the standard transfer uses UTC so no conversion are required", I read it as it should not be neccessary to do any conversion if server is running UTC, right?? But this is not what i see, please see the following test:

    I read an enity from our local CRM system (which is running UTC) and if I don't do any manual conversion, the test fail.

            [Test]
            public void TimeZoneTest_FromCrmUtcStandardTimeToLocalTime()
            {
                //Arrange
                string crmURL = ConfigurationManager.AppSettings["CrmUrl"];
                CrmConnection conn = CrmConnection.Parse(string.Format("Url=http://{0}", crmURL));
                new_budgetitems rate = null;
    
                using (OrganizationService service = new OrganizationService(conn))
                {
                    using (XrmServiceContext context = new XrmServiceContext(service))
                    {
                        rate = (from f in context.new_budgetitemsSet
                                where f.Id == new Guid("F6491B8C-3B5B-E211-85FC-0050569702CB")
                                select f).Single();
                    }
                }
                DateTime dateTimeBefore = new DateTime(2013, 02, 05, 23, 0, 0);
                DateTime dateTimeAfter = new DateTime(2013, 02, 06);
    
                //Act
                DateTime date = rate.new_validfrom.Value;
                DateTime converted = ConvertTimeZoneToLocalTime(date);
    
                //Assert
                Assert.That(date, Is.EqualTo(dateTimeBefore));
                Assert.That(converted, Is.EqualTo(dateTimeAfter));
            


    /Peter

    Monday, March 18, 2013 10:16 AM
  • I don't have enough information from the code to be able to isolate where the problem is located.  You will need to step through the code to determine where the problem is located.  I would add some watch itmes as youi debug to help determine which function is causing the issue.

    jdweng

    Monday, March 18, 2013 11:46 AM
  • Never mind about the code sample above. What i need is to understand the concept of time-zones in dotnet :-)

    Lets make is very simple.

                DateTime dt = new DateTime(2013, 03, 18, 09, 35, 00, DateTimeKind.Utc);
                string datetimeAsString = dt.ToString(); //the result is "18-03-2013 09:35:00"
    
    The result (in datetimeAsString) is still in UTC - should I expect it to be in UTC or local time ??

    /Peter

    Monday, March 18, 2013 12:03 PM
  • OK, I figured what is happening.  The DataTime object has a property called Kind that can be either UTC, Local, or unspecfied.  The 1st statement below sets the property to UTC, the second statement outputs the results as UTC (because the property is set to UTC) , and the 3rd statement converts the results to local time.  Since you are getting the results as UTC then I would set the property to UTC and then when you need to output the time to Local use the ToLocalTime() method.

    DateTime dt = new DateTime(2013, 03, 18, 09, 35, 00, DateTimeKind.Utc);
                string datetimeAsString = dt.ToString(); //the result is "18-03-2013 09:35:00"
                string datetimeAsStringlocal = dt.ToLocalTime().ToString(); //the result is "18-03-2013 09:35:00"


    jdweng



    Monday, March 18, 2013 12:54 PM
  • Thanks jdweng - so this is the answer to my question:

    The concept of time-zones in dotnet is to keep all datetime's in UCT and manually convert to local time whenever showing the datetime to the user  - it this how it is ??

    BR

    Peter

    Monday, March 18, 2013 1:04 PM
  • It is the way unix works and IEEE standards. It is the best way of handling time especially where a Server may be access from around the world.

    jdweng

    Monday, March 18, 2013 1:41 PM
  • Perhaps the DateTimeOffset structure is the one you want to deal with.

    I always have all of my programs deal with DateTime.UtcNow or DateTimeOffset.UtcNow, and then if something is printed to the end user convert it to local time.

    Monday, March 18, 2013 5:14 PM
  • This is also what i will do, I think !!
    Tuesday, March 19, 2013 7:01 AM