Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

Managing culture issues in .NET programming, a headache for programmer – DateTime object

alt text

Programming is fun and gives the sense of accomplishments to programmers and developers. First it is due to sheer joy of creation and solving business problems. As the child takes pride in mud pie, so the programmers and asp.net software companies do enjoy building things and solving the business problems.  Programming sounds easy however it requires a hell lot of thinking, thinking and thinking. Common areas programmer fall prey for are handling different culture issues in programming. There are various scenarios that one needs to address while developing application for multiple or different cultures. Some of the areas are:

  • Handling Date-Time values
  • Ideal solution for handling time zone.
  • String operations
  • Handling numeric values


What is DateTime Object?

DateTime object is used to express a date and time of the day in programming. This is used to perform calculation and comparisons of DateTime instance of same time zone.

There are different ways to instantiate a DateTime object.

  • DateTime {Name of class} = new DateTime(2016, 10, 10, 5, 05, 05) ;

    This is a DateTime constructor that is called to create a date with specific year, month, day, hour, minute and second.

  • DateTime {Name 1} = DateTime.Now;

    DateTime {Name 2} = DateTime.Utc;

    DateTime {Name 3} = DateTime.Today;

    Assigns the current date and time, the current (Coordinated Universal Time) UTC date and time and current date to the three new variables.

  • string {string name} = “10/10/2016 5:30:53 AM”

    DateTime {date name} = DateTime.Parse(dateString,

    System.Globalization.CultureInfo.InvariantCulture);

    This parses a date string and converts it to a DateTime value. The Parse, ParseExact, TryParse, and TryParseExact methods are used to convert string to its equivalent date and time values.


Why is it difficult to manage?

It is difficult to manage DateTime when we are performing calculation or comparison on DateTime object which are from different time zones.  Managing DateTime object of two different time zones is one of the challenging job c# software companies consider.  Common difficulties for software programmers in managing DateTime are below.

  • Comparison or calculation of DateTime object of two different time zones

    If one DateTime object contains machine local date/time and another object contains server date/time and suppose server is dealing with French time zone.

    Comparison on DateTime object may not yield a correct value in this case. Calculations like add date/time/days/ year also do not return accurate values and throws unexpected calculation errors.

    Solution is to have a date representation in Universal format (GMT) always – this may not be practical though in all cases. When date representation is in universal format, every day has fixed length and there are no time-zone offsets to deal with that makes it easy to perform any calculations or comparison.

    While performing any calculation between dates, it is recommended by asp.net software companies to convert the local time value to universal time first and then perform the calculations and convert back to achieve desired accuracy. Following is an illustration of conversin.

    DateTime {name} = DateTime.Parse("Oct 03, 2016 12:00:00 PM").ToUniversalTime();

    date = date.AddHours(3.0).ToLocalTime();

  • Converting DateTime value of machine time zone or date in string format to DateTime value of other time zone is difficult to handle for developers.

    Here, solution is to call the ‘Parse’ and ‘ToString()’ only after setting CultureInfo to CultureInfo.InvariantCulture. The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.

    string {DateInString} = "5/1/2008 8:30:52 AM";
    DateTime {date} = DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture);

    This technique ensures that the underlying time and date values do not change when the date is read or written by developers from different cultures.


Common mistakes by programmers

Following are common mistakes while dealing with DateTime objects.

  • Dealing with ‘Now’

    ‘Now’ method is commonly used to get the current time. The value returned by the ‘Now’ method is in the current machine time-zone context and cannot be treated as an immutable value.

    Converting times to be stored or sent between machines into UTC (Universal Time) is common practice using following method.

    DateTime {date} = DateTime.Now.ToUniversalTime();

    Resulted value would be off by an hour if called during extra hour occurring during the daylight saving.

    Solution is to use ‘UTCNow’

    Instead, call the DateTime.UtcNow function directly. This code will always have the proper 24-hour-day perspective, and then be safely converted to local time.

    DateTime {timeval} = DateTime.UtcNow;

  • Not using ‘InvariantCulture’ with DateTime

    While dealing with parses method to convert it string date value to DateTime value, it considers machine culture. Issue arises when a developer has an application that runs in any of the culture and have date time conversion in the app.

    For example, application has a DateTime picker to show a date with the specific format. In code behind parses a string date to DateTime value using following code snippet.

    string {dateString} = "10/1/2016 8:30:52 AM";

    DateTime {date} = DateTime.Parse(dateString);

    This scenario will generate an unexpected error while dealing with different culture.

    Solution is to use ‘InvariantCulture’ method. The following example uses the invariant culture to persist a DateTime value as a string.

    //Parse the stored string.

    DateTime {dt} = DateTime.Parse("Thur, 1 October 2016 20:34:16 GMT", CultureInfo.InvariantCulture);

    It is also easy to parse the string and display its value by using the formatting conventions of the French (France) culture.

    // create a French (France) CultureInfo object.

    CultureInfo {frFr} = new CultureInfo("fr-FR");

    // Displays the date formatted for the "fr-FR" culture.

    MessageBox.Show("Date formatted for the " + frFr.Name + " culture: " + dt.ToString("f", frFr));

    // Output: Date formatted for the fr-FR culture: mardi 1 octobre 2016 20:34:16

    The Parse, ParseExact, TryParse, and TryParseExact methods are used to convert string to its equivalent date and time value.

    You can also use DateTime.TryParse Method (String, DateTime) that converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded and does not throw an exception if the conversion fails.

Conclusion

  • Developer should understand that DateTIme comparison and calculation are correct and meaningful if they are representation of points of time of same time zone perspective.
  • In general, it is recommended to use a Universal (GMT) time value if possible so that one gets the required accuracy.
  • Always parses string date to DateTime value with using ‘InvariantCulture’ when dealing with different culture for DateTime format.